2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {!WebInspector.Workspace} workspace
35 WebInspector.LiveEditSupport = function(workspace)
37 this._workspace = workspace;
38 this._projectId = "liveedit:";
39 this._projectDelegate = new WebInspector.DebuggerProjectDelegate(workspace, this._projectId, WebInspector.projectTypes.LiveEdit);
40 WebInspector.debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
41 this._debuggerReset();
44 WebInspector.LiveEditSupport.prototype = {
46 * @param {!WebInspector.UISourceCode} uiSourceCode
47 * @return {!WebInspector.UISourceCode}
49 uiSourceCodeForLiveEdit: function(uiSourceCode)
51 var rawLocation = uiSourceCode.uiLocationToRawLocation(WebInspector.targetManager.targets()[0], 0, 0);
52 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location} */ (rawLocation);
53 var script = debuggerModelLocation.script();
54 var uiLocation = script.rawLocationToUILocation(0, 0);
56 // FIXME: Support live editing of scripts mapped to some file.
57 if (uiLocation.uiSourceCode !== uiSourceCode)
58 return uiLocation.uiSourceCode;
59 if (this._uiSourceCodeForScriptId[script.scriptId])
60 return this._uiSourceCodeForScriptId[script.scriptId];
62 console.assert(!script.isInlineScript());
63 var path = this._projectDelegate.addScript(script);
64 var liveEditUISourceCode = this._workspace.uiSourceCode(this._projectId, path);
66 liveEditUISourceCode.addEventListener(WebInspector.UISourceCode.Events.WorkingCopyCommitted, this._workingCopyCommitted, this);
67 this._uiSourceCodeForScriptId[script.scriptId] = liveEditUISourceCode;
68 this._scriptIdForUISourceCode.put(liveEditUISourceCode, script.scriptId);
69 return liveEditUISourceCode;
72 _debuggerReset: function()
74 /** @type {!Object.<string, !WebInspector.UISourceCode>} */
75 this._uiSourceCodeForScriptId = {};
76 /** @type {!Map.<!WebInspector.UISourceCode, string>} */
77 this._scriptIdForUISourceCode = new Map();
78 this._projectDelegate.reset();
82 * @param {!WebInspector.Event} event
84 _workingCopyCommitted: function(event)
86 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.target);
87 var scriptId = /** @type {string} */ (this._scriptIdForUISourceCode.get(uiSourceCode));
88 WebInspector.debuggerModel.setScriptSource(scriptId, uiSourceCode.workingCopy(), innerCallback);
91 * @param {?string} error
92 * @param {!DebuggerAgent.SetScriptSourceError=} errorData
94 function innerCallback(error, errorData)
97 var script = WebInspector.debuggerModel.scriptForId(scriptId);
98 WebInspector.LiveEditSupport.logDetailedError(error, errorData, script);
101 WebInspector.LiveEditSupport.logSuccess();
107 * @param {?string} error
108 * @param {!DebuggerAgent.SetScriptSourceError=} errorData
109 * @param {!WebInspector.Script=} contextScript
111 WebInspector.LiveEditSupport.logDetailedError = function(error, errorData, contextScript)
113 var warningLevel = WebInspector.ConsoleMessage.MessageLevel.Warning;
116 WebInspector.messageSink.addMessage(WebInspector.UIString("LiveEdit failed: %s", error), warningLevel);
119 var compileError = errorData.compileError;
121 var location = contextScript ? WebInspector.UIString(" at %s:%d:%d", contextScript.sourceURL, compileError.lineNumber, compileError.columnNumber) : "";
122 var message = WebInspector.UIString("LiveEdit compile failed: %s%s", compileError.message, location);
123 WebInspector.messageSink.addErrorMessage(message);
125 WebInspector.messageSink.addMessage(WebInspector.UIString("Unknown LiveEdit error: %s; %s", JSON.stringify(errorData), error), warningLevel);
129 WebInspector.LiveEditSupport.logSuccess = function()
131 WebInspector.messageSink.addMessage(WebInspector.UIString("Recompilation and update succeeded."));
134 /** @type {!WebInspector.LiveEditSupport} */
135 WebInspector.liveEditSupport;