Chromium Code Reviews| 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..599feb7a29cdb5050baf79246a128b9ed88353af 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 && script[Sources.ScriptFormatterEditorAction._formatDataSymbol]; |
|
pfeldman
2017/04/06 00:46:26
type cast it?
|
| 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; |
|
pfeldman
2017/04/06 00:46:26
inline those two
|
| + 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 = uiSourceCode[Sources.ScriptFormatterEditorAction._formatDataSymbol]; |
|
lushnikov
2017/04/05 18:29:48
don't you want to save script here instead?
|
| 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); |
|
pfeldman
2017/04/05 18:23:36
You are formatting UISourceCodes, not scripts. For
lushnikov
2017/04/05 18:29:48
let's bind formatted UISourceCode to a single scri
|
| + if (!scripts.length) |
| + return null; |
| + return scripts[0].debuggerModel.createRawLocation(scripts[0], originalLocation[0], originalLocation[1]); |
|
pfeldman
2017/04/06 00:46:26
We'll burn in hell for this.
|
| } |
| /** |
| @@ -77,21 +59,20 @@ Sources.FormatterScriptMapping = class { |
| } |
| }; |
| -/** |
| - * @unrestricted |
| - */ |
| Sources.FormatterScriptMapping.FormatData = 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(); |
| } |
| }; |
| @@ -107,19 +88,12 @@ 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, string>} */ |
| 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(); |
| + this._scriptMapping = new Sources.FormatterScriptMapping(); |
| - /** @type {!Map.<!SDK.DebuggerModel, !Sources.FormatterScriptMapping>} */ |
| - this._scriptMappingByDebuggerModel = new Map(); |
| - this._workspace = Workspace.workspace; |
| SDK.targetManager.observeModels(SDK.DebuggerModel, this); |
| } |
| @@ -128,7 +102,6 @@ Sources.ScriptFormatterEditorAction = class { |
| * @param {!SDK.DebuggerModel} debuggerModel |
| */ |
| modelAdded(debuggerModel) { |
| - this._scriptMappingByDebuggerModel.set(debuggerModel, new Sources.FormatterScriptMapping(debuggerModel, this)); |
| debuggerModel.addEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this); |
| } |
| @@ -137,7 +110,6 @@ Sources.ScriptFormatterEditorAction = class { |
| * @param {!SDK.DebuggerModel} debuggerModel |
| */ |
| modelRemoved(debuggerModel) { |
| - this._scriptMappingByDebuggerModel.remove(debuggerModel); |
| this._cleanForModel(debuggerModel); |
| debuggerModel.removeEventListener(SDK.DebuggerModel.Events.GlobalObjectCleared, this._debuggerReset, this); |
| } |
| @@ -242,17 +214,17 @@ Sources.ScriptFormatterEditorAction = class { |
| * @param {!Workspace.UISourceCode} formattedUISourceCode |
| */ |
| _discardFormattedUISourceCodeScript(formattedUISourceCode) { |
| - var formatData = this._formatData.get(formattedUISourceCode); |
| + var formatData = formattedUISourceCode[Sources.ScriptFormatterEditorAction._formatDataSymbol]; |
| 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]); |
| + var scripts = Sources.ScriptFormatterEditorAction._scriptsForUISourceCode(formatData.originalSourceCode); |
| + for (var script of scripts) { |
| + script[Sources.ScriptFormatterEditorAction._formatDataSymbol] = null; |
| + Bindings.debuggerWorkspaceBinding.popSourceMapping(script); |
| } |
| this._project.removeFile(formattedUISourceCode.url()); |
| } |
| @@ -261,25 +233,14 @@ Sources.ScriptFormatterEditorAction = class { |
| * @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()); |
| - } |
| + for (var script of Object.values(debuggerModel.scripts)) { |
| + var formatData = script[Sources.ScriptFormatterEditorAction._formatDataSymbol]; |
|
pfeldman
2017/04/06 00:46:26
cast
|
| + if (!formatData) |
| + continue; |
| + delete script[Sources.ScriptFormatterEditorAction._formatDataSymbol]; |
| + Bindings.debuggerWorkspaceBinding.setSourceMapping(debuggerModel, formatData.formattedUISourceCode, null); |
| + this._formattedPaths.remove(formatData.originalPath()); |
| + this._project.removeFile(formatData.formattedSourceCodes.url()); |
| } |
| } |
| @@ -293,23 +254,17 @@ Sources.ScriptFormatterEditorAction = class { |
| /** |
| * @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); |
| @@ -325,14 +280,14 @@ Sources.ScriptFormatterEditorAction = class { |
| _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 = Workspace.workspace.uiSourceCode(this._projectId, formattedPath); |
| + var formatData = |
| + formattedUISourceCode && formattedUISourceCode[Sources.ScriptFormatterEditorAction._formatDataSymbol]; |
| if (formatData) { |
| this._showIfNeeded( |
| uiSourceCode, /** @type {!Workspace.UISourceCode} */ (formattedUISourceCode), formatData.mapping); |
| + return; |
| } |
| - return; |
| } |
| uiSourceCode.requestContent().then(contentLoaded.bind(this)); |
| @@ -352,33 +307,29 @@ 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.FormatterScriptMapping.FormatData(uiSourceCode, formattedUISourceCode, formatterMapping); |
| + formattedUISourceCode[Sources.ScriptFormatterEditorAction._formatDataSymbol] = formatData; |
| + |
| + var path = formatData.originalPath(); |
| + this._formattedPaths.set(path, formattedUISourceCode.url()); |
| 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.ScriptFormatterEditorAction._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); |
| @@ -393,3 +344,5 @@ Sources.ScriptFormatterEditorAction = class { |
| } |
| } |
| }; |
| + |
| +Sources.ScriptFormatterEditorAction._formatDataSymbol = Symbol('formatData'); |