| Index: third_party/WebKit/Source/devtools/front_end/sources/ScriptFormatterEditorAction.js
|
| diff --git a/third_party/WebKit/Source/devtools/front_end/sources/ScriptFormatterEditorAction.js b/third_party/WebKit/Source/devtools/front_end/sources/ScriptFormatterEditorAction.js
|
| index eea6a9377fbc44783269ebc854db90bdcaa71aa0..87f3301675b08c347142ea7b0550ef3403621d56 100644
|
| --- a/third_party/WebKit/Source/devtools/front_end/sources/ScriptFormatterEditorAction.js
|
| +++ b/third_party/WebKit/Source/devtools/front_end/sources/ScriptFormatterEditorAction.js
|
| @@ -1,42 +1,25 @@
|
| // Copyright 2014 The Chromium Authors. All rights reserved.
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
| +
|
| /**
|
| * @implements {Bindings.DebuggerSourceMapping}
|
| - * @unrestricted
|
| */
|
| Sources.FormatterScriptMapping = class {
|
| /**
|
| - * @param {!SDK.DebuggerModel} debuggerModel
|
| - * @param {!Sources.ScriptFormatterEditorAction} editorAction
|
| - */
|
| - constructor(debuggerModel, editorAction) {
|
| - this._debuggerModel = debuggerModel;
|
| - this._editorAction = editorAction;
|
| - }
|
| -
|
| - /**
|
| * @override
|
| * @param {!SDK.DebuggerModel.Location} rawLocation
|
| * @return {?Workspace.UILocation}
|
| */
|
| rawLocationToUILocation(rawLocation) {
|
| - var debuggerModelLocation = /** @type {!SDK.DebuggerModel.Location} */ (rawLocation);
|
| - var script = debuggerModelLocation.script();
|
| - if (!script)
|
| - return null;
|
| - var uiSourceCode = this._editorAction._uiSourceCodes.get(script);
|
| - if (!uiSourceCode)
|
| - return null;
|
| -
|
| - var formatData = this._editorAction._formatData.get(uiSourceCode);
|
| + var script = rawLocation.script();
|
| + var formatData = script && Sources.SourceFormatData._for(script);
|
| if (!formatData)
|
| return null;
|
| - var mapping = formatData.mapping;
|
| - var lineNumber = debuggerModelLocation.lineNumber;
|
| - var columnNumber = debuggerModelLocation.columnNumber || 0;
|
| - var formattedLocation = mapping.originalToFormatted(lineNumber, columnNumber);
|
| - return uiSourceCode.uiLocation(formattedLocation[0], formattedLocation[1]);
|
| + var lineNumber = rawLocation.lineNumber;
|
| + var columnNumber = rawLocation.columnNumber || 0;
|
| + var formattedLocation = formatData.mapping.originalToFormatted(lineNumber, columnNumber);
|
| + return formatData.formattedSourceCode.uiLocation(formattedLocation[0], formattedLocation[1]);
|
| }
|
|
|
| /**
|
| @@ -47,15 +30,14 @@ Sources.FormatterScriptMapping = class {
|
| * @return {?SDK.DebuggerModel.Location}
|
| */
|
| uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber) {
|
| - var formatData = this._editorAction._formatData.get(uiSourceCode);
|
| + var formatData = Sources.SourceFormatData._for(uiSourceCode);
|
| if (!formatData)
|
| return null;
|
| var originalLocation = formatData.mapping.formattedToOriginal(lineNumber, columnNumber);
|
| - for (var i = 0; i < formatData.scripts.length; ++i) {
|
| - if (formatData.scripts[i].debuggerModel === this._debuggerModel)
|
| - return this._debuggerModel.createRawLocation(formatData.scripts[i], originalLocation[0], originalLocation[1]);
|
| - }
|
| - return null;
|
| + var scripts = Sources.ScriptFormatterEditorAction._scriptsForUISourceCode(formatData.originalSourceCode);
|
| + if (!scripts.length)
|
| + return null;
|
| + return scripts[0].debuggerModel.createRawLocation(scripts[0], originalLocation[0], originalLocation[1]);
|
| }
|
|
|
| /**
|
| @@ -77,27 +59,35 @@ Sources.FormatterScriptMapping = class {
|
| }
|
| };
|
|
|
| -/**
|
| - * @unrestricted
|
| - */
|
| -Sources.FormatterScriptMapping.FormatData = class {
|
| +Sources.SourceFormatData = class {
|
| /**
|
| - * @param {string} projectId
|
| - * @param {string} path
|
| + * @param {!Workspace.UISourceCode} originalSourceCode
|
| + * @param {!Workspace.UISourceCode} formattedSourceCode
|
| * @param {!Sources.FormatterSourceMapping} mapping
|
| - * @param {!Array.<!SDK.Script>} scripts
|
| */
|
| - constructor(projectId, path, mapping, scripts) {
|
| - this.projectId = projectId;
|
| - this.path = path;
|
| + constructor(originalSourceCode, formattedSourceCode, mapping) {
|
| + this.originalSourceCode = originalSourceCode;
|
| + this.formattedSourceCode = formattedSourceCode;
|
| this.mapping = mapping;
|
| - this.scripts = scripts;
|
| + }
|
| +
|
| + originalPath() {
|
| + return this.originalSourceCode.project().id() + ':' + this.originalSourceCode.url();
|
| + }
|
| +
|
| + /**
|
| + * @param {!Object} object
|
| + * @return {?Sources.SourceFormatData}
|
| + */
|
| + static _for(object) {
|
| + return object[Sources.SourceFormatData._formatDataSymbol];
|
| }
|
| };
|
|
|
| +Sources.SourceFormatData._formatDataSymbol = Symbol('formatData');
|
| +
|
| /**
|
| * @implements {Sources.SourcesView.EditorAction}
|
| - * @implements {SDK.SDKModelObserver<!SDK.DebuggerModel>}
|
| * @unrestricted
|
| */
|
| Sources.ScriptFormatterEditorAction = class {
|
| @@ -107,39 +97,23 @@ Sources.ScriptFormatterEditorAction = class {
|
| Workspace.workspace, this._projectId, Workspace.projectTypes.Formatter, 'formatter',
|
| true /* isServiceProject */);
|
|
|
| - /** @type {!Map.<!SDK.Script, !Workspace.UISourceCode>} */
|
| - this._uiSourceCodes = new Map();
|
| - /** @type {!Map.<string, string>} */
|
| + /** @type {!Map<string, !Workspace.UISourceCode>} */
|
| this._formattedPaths = new Map();
|
| - /** @type {!Map.<!Workspace.UISourceCode, !Sources.FormatterScriptMapping.FormatData>} */
|
| - this._formatData = new Map();
|
| -
|
| - /** @type {!Set.<string>} */
|
| + /** @type {!Set<string>} */
|
| this._pathsToFormatOnLoad = new Set();
|
| -
|
| - /** @type {!Map.<!SDK.DebuggerModel, !Sources.FormatterScriptMapping>} */
|
| - this._scriptMappingByDebuggerModel = new Map();
|
| - this._workspace = Workspace.workspace;
|
| - SDK.targetManager.observeModels(SDK.DebuggerModel, this);
|
| - }
|
| -
|
| - /**
|
| - * @override
|
| - * @param {!SDK.DebuggerModel} debuggerModel
|
| - */
|
| - modelAdded(debuggerModel) {
|
| - this._scriptMappingByDebuggerModel.set(debuggerModel, new Sources.FormatterScriptMapping(debuggerModel, this));
|
| - debuggerModel.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
|
| + this._scriptMapping = new Sources.FormatterScriptMapping();
|
| + Workspace.workspace.addEventListener(
|
| + Workspace.Workspace.Events.UISourceCodeRemoved, this._onUISourceCodeRemoved, this);
|
| }
|
|
|
| /**
|
| - * @override
|
| - * @param {!SDK.DebuggerModel} debuggerModel
|
| + * @param {!Common.Event} event
|
| */
|
| - modelRemoved(debuggerModel) {
|
| - this._scriptMappingByDebuggerModel.remove(debuggerModel);
|
| - this._cleanForModel(debuggerModel);
|
| - debuggerModel.removeEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this);
|
| + _onUISourceCodeRemoved(event) {
|
| + var uiSourceCode = /** @type {!Workspace.UISourceCode} */ (event.data);
|
| + var formattedUISourceCode = this._formattedPaths.get(uiSourceCode.project().id() + ':' + uiSourceCode.url());
|
| + if (formattedUISourceCode)
|
| + this._discardFormattedUISourceCodeScript(formattedUISourceCode, false);
|
| }
|
|
|
| /**
|
| @@ -164,7 +138,7 @@ Sources.ScriptFormatterEditorAction = class {
|
|
|
| if (wasSelected)
|
| this._updateButton(null);
|
| - this._discardFormattedUISourceCodeScript(uiSourceCode);
|
| + this._discardFormattedUISourceCodeScript(uiSourceCode, true);
|
| }
|
|
|
| /**
|
| @@ -240,76 +214,41 @@ Sources.ScriptFormatterEditorAction = class {
|
|
|
| /**
|
| * @param {!Workspace.UISourceCode} formattedUISourceCode
|
| + * @param {boolean} userAction
|
| */
|
| - _discardFormattedUISourceCodeScript(formattedUISourceCode) {
|
| - var formatData = this._formatData.get(formattedUISourceCode);
|
| + _discardFormattedUISourceCodeScript(formattedUISourceCode, userAction) {
|
| + var formatData = Sources.SourceFormatData._for(formattedUISourceCode);
|
| if (!formatData)
|
| return;
|
|
|
| - this._formatData.remove(formattedUISourceCode);
|
| - var path = formatData.projectId + ':' + formatData.path;
|
| + var path = formatData.originalPath();
|
| this._formattedPaths.remove(path);
|
| - this._pathsToFormatOnLoad.delete(path);
|
| - for (var i = 0; i < formatData.scripts.length; ++i) {
|
| - this._uiSourceCodes.remove(formatData.scripts[i]);
|
| - Bindings.debuggerWorkspaceBinding.popSourceMapping(formatData.scripts[i]);
|
| + delete formattedUISourceCode[Sources.SourceFormatData._formatDataSymbol];
|
| + if (userAction)
|
| + this._pathsToFormatOnLoad.delete(path);
|
| + var scripts = Sources.ScriptFormatterEditorAction._scriptsForUISourceCode(formatData.originalSourceCode);
|
| + for (var script of scripts) {
|
| + delete script[Sources.SourceFormatData._formatDataSymbol];
|
| + Bindings.debuggerWorkspaceBinding.popSourceMapping(script);
|
| }
|
| + if (scripts[0])
|
| + Bindings.debuggerWorkspaceBinding.setSourceMapping(scripts[0].debuggerModel, formattedUISourceCode, null);
|
| this._project.removeFile(formattedUISourceCode.url());
|
| }
|
|
|
| /**
|
| - * @param {!SDK.DebuggerModel} debuggerModel
|
| - */
|
| - _cleanForModel(debuggerModel) {
|
| - var uiSourceCodes = this._formatData.keysArray();
|
| - for (var i = 0; i < uiSourceCodes.length; ++i) {
|
| - Bindings.debuggerWorkspaceBinding.setSourceMapping(debuggerModel, uiSourceCodes[i], null);
|
| - var formatData = this._formatData.get(uiSourceCodes[i]);
|
| - var scripts = [];
|
| - for (var j = 0; j < formatData.scripts.length; ++j) {
|
| - if (formatData.scripts[j].debuggerModel === debuggerModel)
|
| - this._uiSourceCodes.remove(formatData.scripts[j]);
|
| - else
|
| - scripts.push(formatData.scripts[j]);
|
| - }
|
| -
|
| - if (scripts.length) {
|
| - formatData.scripts = scripts;
|
| - } else {
|
| - this._formattedPaths.remove(formatData.projectId + ':' + formatData.path);
|
| - this._formatData.remove(uiSourceCodes[i]);
|
| - this._project.removeFile(uiSourceCodes[i].url());
|
| - }
|
| - }
|
| - }
|
| -
|
| - /**
|
| - * @param {!Common.Event} event
|
| - */
|
| - _debuggerReset(event) {
|
| - var debuggerModel = /** @type {!SDK.DebuggerModel} */ (event.data);
|
| - this._cleanForModel(debuggerModel);
|
| - }
|
| -
|
| - /**
|
| * @param {!Workspace.UISourceCode} uiSourceCode
|
| - * @return {!Array.<!SDK.Script>}
|
| + * @return {!Array<!SDK.Script>}
|
| */
|
| - _scriptsForUISourceCode(uiSourceCode) {
|
| - /**
|
| - * @param {!SDK.Script} script
|
| - * @return {boolean}
|
| - */
|
| - function isInlineScript(script) {
|
| - return script.isInlineScript() && !script.hasSourceURL;
|
| - }
|
| -
|
| + static _scriptsForUISourceCode(uiSourceCode) {
|
| if (uiSourceCode.contentType() === Common.resourceTypes.Document) {
|
| - var scripts = [];
|
| - var debuggerModels = SDK.targetManager.models(SDK.DebuggerModel);
|
| - for (var i = 0; i < debuggerModels.length; ++i)
|
| - scripts.pushAll(debuggerModels[i].scriptsForSourceURL(uiSourceCode.url()));
|
| - return scripts.filter(isInlineScript);
|
| + var target = Bindings.NetworkProject.targetForUISourceCode(uiSourceCode);
|
| + var debuggerModel = target && target.model(SDK.DebuggerModel);
|
| + if (debuggerModel) {
|
| + var scripts = debuggerModel.scriptsForSourceURL(uiSourceCode.url())
|
| + .filter(script => script.isInlineScript() && !script.hasSourceURL);
|
| + return scripts;
|
| + }
|
| }
|
| if (uiSourceCode.contentType().isScript()) {
|
| var rawLocation = Bindings.debuggerWorkspaceBinding.uiLocationToRawLocation(uiSourceCode, 0, 0);
|
| @@ -323,16 +262,14 @@ Sources.ScriptFormatterEditorAction = class {
|
| * @param {!Workspace.UISourceCode} uiSourceCode
|
| */
|
| _formatUISourceCodeScript(uiSourceCode) {
|
| - var formattedPath = this._formattedPaths.get(uiSourceCode.project().id() + ':' + uiSourceCode.url());
|
| - if (formattedPath) {
|
| - var uiSourceCodePath = formattedPath;
|
| - var formattedUISourceCode = this._workspace.uiSourceCode(this._projectId, uiSourceCodePath);
|
| - var formatData = formattedUISourceCode ? this._formatData.get(formattedUISourceCode) : null;
|
| + var formattedUISourceCode = this._formattedPaths.get(uiSourceCode.project().id() + ':' + uiSourceCode.url());
|
| + if (formattedUISourceCode) {
|
| + var formatData = Sources.SourceFormatData._for(formattedUISourceCode);
|
| if (formatData) {
|
| this._showIfNeeded(
|
| uiSourceCode, /** @type {!Workspace.UISourceCode} */ (formattedUISourceCode), formatData.mapping);
|
| + return;
|
| }
|
| - return;
|
| }
|
|
|
| uiSourceCode.requestContent().then(contentLoaded.bind(this));
|
| @@ -352,33 +289,28 @@ Sources.ScriptFormatterEditorAction = class {
|
| * @param {!Sources.FormatterSourceMapping} formatterMapping
|
| */
|
| function innerCallback(formattedContent, formatterMapping) {
|
| - var scripts = this._scriptsForUISourceCode(uiSourceCode);
|
| var formattedURL = uiSourceCode.url() + ':formatted';
|
| var contentProvider =
|
| Common.StaticContentProvider.fromString(formattedURL, uiSourceCode.contentType(), formattedContent);
|
| var formattedUISourceCode = this._project.addContentProvider(formattedURL, contentProvider);
|
| - var formattedPath = formattedUISourceCode.url();
|
| - var formatData = new Sources.FormatterScriptMapping.FormatData(
|
| - uiSourceCode.project().id(), uiSourceCode.url(), formatterMapping, scripts);
|
| - this._formatData.set(formattedUISourceCode, formatData);
|
| - var path = uiSourceCode.project().id() + ':' + uiSourceCode.url();
|
| - this._formattedPaths.set(path, formattedPath);
|
| + var formatData = new Sources.SourceFormatData(uiSourceCode, formattedUISourceCode, formatterMapping);
|
| + formattedUISourceCode[Sources.SourceFormatData._formatDataSymbol] = formatData;
|
| +
|
| + var path = formatData.originalPath();
|
| + this._formattedPaths.set(path, formattedUISourceCode);
|
| this._pathsToFormatOnLoad.add(path);
|
| - for (var i = 0; i < scripts.length; ++i) {
|
| - this._uiSourceCodes.set(scripts[i], formattedUISourceCode);
|
| - var scriptMapping =
|
| - /** @type {!Sources.FormatterScriptMapping} */ (
|
| - this._scriptMappingByDebuggerModel.get(scripts[i].debuggerModel));
|
| - Bindings.debuggerWorkspaceBinding.pushSourceMapping(scripts[i], scriptMapping);
|
| - }
|
|
|
| - var debuggerModels = SDK.targetManager.models(SDK.DebuggerModel);
|
| - for (var i = 0; i < debuggerModels.length; ++i) {
|
| - var scriptMapping =
|
| - /** @type {!Sources.FormatterScriptMapping} */ (this._scriptMappingByDebuggerModel.get(debuggerModels[i]));
|
| - Bindings.debuggerWorkspaceBinding.setSourceMapping(debuggerModels[i], formattedUISourceCode, scriptMapping);
|
| + var scripts = Sources.ScriptFormatterEditorAction._scriptsForUISourceCode(uiSourceCode);
|
| + if (!scripts)
|
| + return;
|
| + for (var script of scripts) {
|
| + script[Sources.SourceFormatData._formatDataSymbol] = formatData;
|
| + Bindings.debuggerWorkspaceBinding.pushSourceMapping(script, this._scriptMapping);
|
| }
|
|
|
| + Bindings.debuggerWorkspaceBinding.setSourceMapping(
|
| + scripts[0].debuggerModel, formattedUISourceCode, this._scriptMapping);
|
| +
|
| for (var decoration of uiSourceCode.allDecorations()) {
|
| var range = decoration.range();
|
| var startLocation = formatterMapping.originalToFormatted(range.startLine, range.startColumn);
|
|
|