OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | |
5 /** | 4 /** |
6 * @constructor | |
7 * @implements {WebInspector.DebuggerSourceMapping} | 5 * @implements {WebInspector.DebuggerSourceMapping} |
8 * @param {!WebInspector.DebuggerModel} debuggerModel | 6 * @unrestricted |
9 * @param {!WebInspector.ScriptFormatterEditorAction} editorAction | |
10 */ | 7 */ |
11 WebInspector.FormatterScriptMapping = function(debuggerModel, editorAction) | 8 WebInspector.FormatterScriptMapping = class { |
12 { | 9 /** |
| 10 * @param {!WebInspector.DebuggerModel} debuggerModel |
| 11 * @param {!WebInspector.ScriptFormatterEditorAction} editorAction |
| 12 */ |
| 13 constructor(debuggerModel, editorAction) { |
13 this._debuggerModel = debuggerModel; | 14 this._debuggerModel = debuggerModel; |
14 this._editorAction = editorAction; | 15 this._editorAction = editorAction; |
15 }; | 16 } |
16 | 17 |
17 WebInspector.FormatterScriptMapping.prototype = { | 18 /** |
18 /** | 19 * @override |
19 * @override | 20 * @param {!WebInspector.DebuggerModel.Location} rawLocation |
20 * @param {!WebInspector.DebuggerModel.Location} rawLocation | 21 * @return {?WebInspector.UILocation} |
21 * @return {?WebInspector.UILocation} | 22 */ |
22 */ | 23 rawLocationToUILocation(rawLocation) { |
23 rawLocationToUILocation: function(rawLocation) | 24 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Location}
*/ (rawLocation); |
24 { | 25 var script = debuggerModelLocation.script(); |
25 var debuggerModelLocation = /** @type {!WebInspector.DebuggerModel.Locat
ion} */ (rawLocation); | 26 if (!script) |
26 var script = debuggerModelLocation.script(); | 27 return null; |
27 if (!script) | 28 var uiSourceCode = this._editorAction._uiSourceCodes.get(script); |
28 return null; | 29 if (!uiSourceCode) |
29 var uiSourceCode = this._editorAction._uiSourceCodes.get(script); | 30 return null; |
30 if (!uiSourceCode) | |
31 return null; | |
32 | 31 |
33 var formatData = this._editorAction._formatData.get(uiSourceCode); | 32 var formatData = this._editorAction._formatData.get(uiSourceCode); |
34 if (!formatData) | 33 if (!formatData) |
35 return null; | 34 return null; |
36 var mapping = formatData.mapping; | 35 var mapping = formatData.mapping; |
37 var lineNumber = debuggerModelLocation.lineNumber; | 36 var lineNumber = debuggerModelLocation.lineNumber; |
38 var columnNumber = debuggerModelLocation.columnNumber || 0; | 37 var columnNumber = debuggerModelLocation.columnNumber || 0; |
39 var formattedLocation = mapping.originalToFormatted(lineNumber, columnNu
mber); | 38 var formattedLocation = mapping.originalToFormatted(lineNumber, columnNumber
); |
40 return uiSourceCode.uiLocation(formattedLocation[0], formattedLocation[1
]); | 39 return uiSourceCode.uiLocation(formattedLocation[0], formattedLocation[1]); |
41 }, | 40 } |
42 | 41 |
43 /** | 42 /** |
44 * @override | 43 * @override |
45 * @param {!WebInspector.UISourceCode} uiSourceCode | 44 * @param {!WebInspector.UISourceCode} uiSourceCode |
46 * @param {number} lineNumber | 45 * @param {number} lineNumber |
47 * @param {number} columnNumber | 46 * @param {number} columnNumber |
48 * @return {?WebInspector.DebuggerModel.Location} | 47 * @return {?WebInspector.DebuggerModel.Location} |
49 */ | 48 */ |
50 uiLocationToRawLocation: function(uiSourceCode, lineNumber, columnNumber) | 49 uiLocationToRawLocation(uiSourceCode, lineNumber, columnNumber) { |
51 { | 50 var formatData = this._editorAction._formatData.get(uiSourceCode); |
52 var formatData = this._editorAction._formatData.get(uiSourceCode); | 51 if (!formatData) |
53 if (!formatData) | 52 return null; |
54 return null; | 53 var originalLocation = formatData.mapping.formattedToOriginal(lineNumber, co
lumnNumber); |
55 var originalLocation = formatData.mapping.formattedToOriginal(lineNumber
, columnNumber); | 54 for (var i = 0; i < formatData.scripts.length; ++i) { |
56 for (var i = 0; i < formatData.scripts.length; ++i) { | 55 if (formatData.scripts[i].debuggerModel === this._debuggerModel) |
57 if (formatData.scripts[i].debuggerModel === this._debuggerModel) | 56 return this._debuggerModel.createRawLocation(formatData.scripts[i], orig
inalLocation[0], originalLocation[1]); |
58 return this._debuggerModel.createRawLocation(formatData.scripts[
i], originalLocation[0], originalLocation[1]); | 57 } |
59 } | 58 return null; |
60 return null; | 59 } |
61 }, | |
62 | 60 |
63 /** | 61 /** |
64 * @override | 62 * @override |
65 * @return {boolean} | 63 * @return {boolean} |
66 */ | 64 */ |
67 isIdentity: function() | 65 isIdentity() { |
68 { | 66 return false; |
69 return false; | 67 } |
70 }, | |
71 | 68 |
72 /** | 69 /** |
73 * @override | 70 * @override |
74 * @param {!WebInspector.UISourceCode} uiSourceCode | 71 * @param {!WebInspector.UISourceCode} uiSourceCode |
75 * @param {number} lineNumber | 72 * @param {number} lineNumber |
76 * @return {boolean} | 73 * @return {boolean} |
77 */ | 74 */ |
78 uiLineHasMapping: function(uiSourceCode, lineNumber) | 75 uiLineHasMapping(uiSourceCode, lineNumber) { |
79 { | 76 return true; |
80 return true; | 77 } |
81 } | |
82 }; | 78 }; |
83 | 79 |
84 /** | 80 /** |
85 * @constructor | 81 * @unrestricted |
86 * @param {string} projectId | |
87 * @param {string} path | |
88 * @param {!WebInspector.FormatterSourceMapping} mapping | |
89 * @param {!Array.<!WebInspector.Script>} scripts | |
90 */ | 82 */ |
91 WebInspector.FormatterScriptMapping.FormatData = function(projectId, path, mappi
ng, scripts) | 83 WebInspector.FormatterScriptMapping.FormatData = class { |
92 { | 84 /** |
| 85 * @param {string} projectId |
| 86 * @param {string} path |
| 87 * @param {!WebInspector.FormatterSourceMapping} mapping |
| 88 * @param {!Array.<!WebInspector.Script>} scripts |
| 89 */ |
| 90 constructor(projectId, path, mapping, scripts) { |
93 this.projectId = projectId; | 91 this.projectId = projectId; |
94 this.path = path; | 92 this.path = path; |
95 this.mapping = mapping; | 93 this.mapping = mapping; |
96 this.scripts = scripts; | 94 this.scripts = scripts; |
| 95 } |
97 }; | 96 }; |
98 | 97 |
99 /** | 98 /** |
100 * @constructor | |
101 * @implements {WebInspector.SourcesView.EditorAction} | 99 * @implements {WebInspector.SourcesView.EditorAction} |
102 * @implements {WebInspector.TargetManager.Observer} | 100 * @implements {WebInspector.TargetManager.Observer} |
| 101 * @unrestricted |
103 */ | 102 */ |
104 WebInspector.ScriptFormatterEditorAction = function() | 103 WebInspector.ScriptFormatterEditorAction = class { |
105 { | 104 constructor() { |
106 this._projectId = "formatter:"; | 105 this._projectId = 'formatter:'; |
107 this._project = new WebInspector.ContentProviderBasedProject(WebInspector.wo
rkspace, this._projectId, WebInspector.projectTypes.Formatter, "formatter"); | 106 this._project = new WebInspector.ContentProviderBasedProject( |
| 107 WebInspector.workspace, this._projectId, WebInspector.projectTypes.Forma
tter, 'formatter'); |
108 | 108 |
109 /** @type {!Map.<!WebInspector.Script, !WebInspector.UISourceCode>} */ | 109 /** @type {!Map.<!WebInspector.Script, !WebInspector.UISourceCode>} */ |
110 this._uiSourceCodes = new Map(); | 110 this._uiSourceCodes = new Map(); |
111 /** @type {!Map.<string, string>} */ | 111 /** @type {!Map.<string, string>} */ |
112 this._formattedPaths = new Map(); | 112 this._formattedPaths = new Map(); |
113 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.FormatterScriptMa
pping.FormatData>} */ | 113 /** @type {!Map.<!WebInspector.UISourceCode, !WebInspector.FormatterScriptMa
pping.FormatData>} */ |
114 this._formatData = new Map(); | 114 this._formatData = new Map(); |
115 | 115 |
116 /** @type {!Set.<string>} */ | 116 /** @type {!Set.<string>} */ |
117 this._pathsToFormatOnLoad = new Set(); | 117 this._pathsToFormatOnLoad = new Set(); |
118 | 118 |
119 /** @type {!Map.<!WebInspector.Target, !WebInspector.FormatterScriptMapping>
} */ | 119 /** @type {!Map.<!WebInspector.Target, !WebInspector.FormatterScriptMapping>
} */ |
120 this._scriptMappingByTarget = new Map(); | 120 this._scriptMappingByTarget = new Map(); |
121 this._workspace = WebInspector.workspace; | 121 this._workspace = WebInspector.workspace; |
122 WebInspector.targetManager.observeTargets(this); | 122 WebInspector.targetManager.observeTargets(this); |
123 }; | 123 } |
124 | 124 |
125 WebInspector.ScriptFormatterEditorAction.prototype = { | 125 /** |
| 126 * @override |
| 127 * @param {!WebInspector.Target} target |
| 128 */ |
| 129 targetAdded(target) { |
| 130 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); |
| 131 if (!debuggerModel) |
| 132 return; |
| 133 this._scriptMappingByTarget.set(target, new WebInspector.FormatterScriptMapp
ing(debuggerModel, this)); |
| 134 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalObjec
tCleared, this._debuggerReset, this); |
| 135 } |
| 136 |
| 137 /** |
| 138 * @override |
| 139 * @param {!WebInspector.Target} target |
| 140 */ |
| 141 targetRemoved(target) { |
| 142 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); |
| 143 if (!debuggerModel) |
| 144 return; |
| 145 this._scriptMappingByTarget.remove(target); |
| 146 this._cleanForTarget(target); |
| 147 debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.GlobalOb
jectCleared, this._debuggerReset, this); |
| 148 } |
| 149 |
| 150 /** |
| 151 * @param {!WebInspector.Event} event |
| 152 */ |
| 153 _editorSelected(event) { |
| 154 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data); |
| 155 this._updateButton(uiSourceCode); |
| 156 |
| 157 var path = uiSourceCode.project().id() + ':' + uiSourceCode.url(); |
| 158 if (this._isFormatableScript(uiSourceCode) && this._pathsToFormatOnLoad.has(
path) && |
| 159 !this._formattedPaths.get(path)) |
| 160 this._formatUISourceCodeScript(uiSourceCode); |
| 161 } |
| 162 |
| 163 /** |
| 164 * @param {!WebInspector.Event} event |
| 165 */ |
| 166 _editorClosed(event) { |
| 167 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data.uiS
ourceCode); |
| 168 var wasSelected = /** @type {boolean} */ (event.data.wasSelected); |
| 169 |
| 170 if (wasSelected) |
| 171 this._updateButton(null); |
| 172 this._discardFormattedUISourceCodeScript(uiSourceCode); |
| 173 } |
| 174 |
| 175 /** |
| 176 * @param {?WebInspector.UISourceCode} uiSourceCode |
| 177 */ |
| 178 _updateButton(uiSourceCode) { |
| 179 this._button.element.classList.toggle('hidden', !this._isFormatableScript(ui
SourceCode)); |
| 180 } |
| 181 |
| 182 /** |
| 183 * @override |
| 184 * @param {!WebInspector.SourcesView} sourcesView |
| 185 * @return {!WebInspector.ToolbarButton} |
| 186 */ |
| 187 button(sourcesView) { |
| 188 if (this._button) |
| 189 return this._button; |
| 190 |
| 191 this._sourcesView = sourcesView; |
| 192 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorSel
ected, this._editorSelected.bind(this)); |
| 193 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorClo
sed, this._editorClosed.bind(this)); |
| 194 |
| 195 this._button = new WebInspector.ToolbarButton(WebInspector.UIString('Pretty
print'), 'format-toolbar-item'); |
| 196 this._button.addEventListener('click', this._toggleFormatScriptSource, this)
; |
| 197 this._updateButton(sourcesView.currentUISourceCode()); |
| 198 |
| 199 return this._button; |
| 200 } |
| 201 |
| 202 /** |
| 203 * @param {?WebInspector.UISourceCode} uiSourceCode |
| 204 * @return {boolean} |
| 205 */ |
| 206 _isFormatableScript(uiSourceCode) { |
| 207 if (!uiSourceCode) |
| 208 return false; |
| 209 if (WebInspector.persistence.binding(uiSourceCode)) |
| 210 return false; |
| 211 var supportedProjectTypes = [ |
| 212 WebInspector.projectTypes.Network, WebInspector.projectTypes.Debugger, Web
Inspector.projectTypes.ContentScripts |
| 213 ]; |
| 214 if (supportedProjectTypes.indexOf(uiSourceCode.project().type()) === -1) |
| 215 return false; |
| 216 return uiSourceCode.contentType().hasScripts(); |
| 217 } |
| 218 |
| 219 _toggleFormatScriptSource() { |
| 220 var uiSourceCode = this._sourcesView.currentUISourceCode(); |
| 221 if (this._isFormatableScript(uiSourceCode)) |
| 222 this._formatUISourceCodeScript(uiSourceCode); |
| 223 } |
| 224 |
| 225 /** |
| 226 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 227 * @param {!WebInspector.UISourceCode} formattedUISourceCode |
| 228 * @param {!WebInspector.FormatterSourceMapping} mapping |
| 229 * @private |
| 230 */ |
| 231 _showIfNeeded(uiSourceCode, formattedUISourceCode, mapping) { |
| 232 if (uiSourceCode !== this._sourcesView.currentUISourceCode()) |
| 233 return; |
| 234 var sourceFrame = this._sourcesView.viewForFile(uiSourceCode); |
| 235 var start = [0, 0]; |
| 236 if (sourceFrame) { |
| 237 var selection = sourceFrame.selection(); |
| 238 start = mapping.originalToFormatted(selection.startLine, selection.startCo
lumn); |
| 239 } |
| 240 this._sourcesView.showSourceLocation(formattedUISourceCode, start[0], start[
1]); |
| 241 this._updateButton(formattedUISourceCode); |
| 242 } |
| 243 |
| 244 /** |
| 245 * @param {!WebInspector.UISourceCode} formattedUISourceCode |
| 246 */ |
| 247 _discardFormattedUISourceCodeScript(formattedUISourceCode) { |
| 248 var formatData = this._formatData.get(formattedUISourceCode); |
| 249 if (!formatData) |
| 250 return; |
| 251 |
| 252 this._formatData.remove(formattedUISourceCode); |
| 253 var path = formatData.projectId + ':' + formatData.path; |
| 254 this._formattedPaths.remove(path); |
| 255 this._pathsToFormatOnLoad.delete(path); |
| 256 for (var i = 0; i < formatData.scripts.length; ++i) { |
| 257 this._uiSourceCodes.remove(formatData.scripts[i]); |
| 258 WebInspector.debuggerWorkspaceBinding.popSourceMapping(formatData.scripts[
i]); |
| 259 } |
| 260 this._project.removeFile(formattedUISourceCode.url()); |
| 261 } |
| 262 |
| 263 /** |
| 264 * @param {!WebInspector.Target} target |
| 265 */ |
| 266 _cleanForTarget(target) { |
| 267 var uiSourceCodes = this._formatData.keysArray(); |
| 268 for (var i = 0; i < uiSourceCodes.length; ++i) { |
| 269 WebInspector.debuggerWorkspaceBinding.setSourceMapping(target, uiSourceCod
es[i], null); |
| 270 var formatData = this._formatData.get(uiSourceCodes[i]); |
| 271 var scripts = []; |
| 272 for (var j = 0; j < formatData.scripts.length; ++j) { |
| 273 if (formatData.scripts[j].target() === target) |
| 274 this._uiSourceCodes.remove(formatData.scripts[j]); |
| 275 else |
| 276 scripts.push(formatData.scripts[j]); |
| 277 } |
| 278 |
| 279 if (scripts.length) |
| 280 formatData.scripts = scripts; |
| 281 else { |
| 282 this._formattedPaths.remove(formatData.projectId + ':' + formatData.path
); |
| 283 this._formatData.remove(uiSourceCodes[i]); |
| 284 this._project.removeFile(uiSourceCodes[i].url()); |
| 285 } |
| 286 } |
| 287 } |
| 288 |
| 289 /** |
| 290 * @param {!WebInspector.Event} event |
| 291 */ |
| 292 _debuggerReset(event) { |
| 293 var debuggerModel = /** @type {!WebInspector.DebuggerModel} */ (event.target
); |
| 294 this._cleanForTarget(debuggerModel.target()); |
| 295 } |
| 296 |
| 297 /** |
| 298 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 299 * @return {!Array.<!WebInspector.Script>} |
| 300 */ |
| 301 _scriptsForUISourceCode(uiSourceCode) { |
126 /** | 302 /** |
127 * @override | 303 * @param {!WebInspector.Script} script |
128 * @param {!WebInspector.Target} target | |
129 */ | |
130 targetAdded: function(target) | |
131 { | |
132 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); | |
133 if (!debuggerModel) | |
134 return; | |
135 this._scriptMappingByTarget.set(target, new WebInspector.FormatterScript
Mapping(debuggerModel, this)); | |
136 debuggerModel.addEventListener(WebInspector.DebuggerModel.Events.GlobalO
bjectCleared, this._debuggerReset, this); | |
137 }, | |
138 | |
139 /** | |
140 * @override | |
141 * @param {!WebInspector.Target} target | |
142 */ | |
143 targetRemoved: function(target) | |
144 { | |
145 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); | |
146 if (!debuggerModel) | |
147 return; | |
148 this._scriptMappingByTarget.remove(target); | |
149 this._cleanForTarget(target); | |
150 debuggerModel.removeEventListener(WebInspector.DebuggerModel.Events.Glob
alObjectCleared, this._debuggerReset, this); | |
151 }, | |
152 | |
153 /** | |
154 * @param {!WebInspector.Event} event | |
155 */ | |
156 _editorSelected: function(event) | |
157 { | |
158 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data
); | |
159 this._updateButton(uiSourceCode); | |
160 | |
161 var path = uiSourceCode.project().id() + ":" + uiSourceCode.url(); | |
162 if (this._isFormatableScript(uiSourceCode) && this._pathsToFormatOnLoad.
has(path) && !this._formattedPaths.get(path)) | |
163 this._formatUISourceCodeScript(uiSourceCode); | |
164 }, | |
165 | |
166 /** | |
167 * @param {!WebInspector.Event} event | |
168 */ | |
169 _editorClosed: function(event) | |
170 { | |
171 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data
.uiSourceCode); | |
172 var wasSelected = /** @type {boolean} */ (event.data.wasSelected); | |
173 | |
174 if (wasSelected) | |
175 this._updateButton(null); | |
176 this._discardFormattedUISourceCodeScript(uiSourceCode); | |
177 }, | |
178 | |
179 /** | |
180 * @param {?WebInspector.UISourceCode} uiSourceCode | |
181 */ | |
182 _updateButton: function(uiSourceCode) | |
183 { | |
184 this._button.element.classList.toggle("hidden", !this._isFormatableScrip
t(uiSourceCode)); | |
185 }, | |
186 | |
187 /** | |
188 * @override | |
189 * @param {!WebInspector.SourcesView} sourcesView | |
190 * @return {!WebInspector.ToolbarButton} | |
191 */ | |
192 button: function(sourcesView) | |
193 { | |
194 if (this._button) | |
195 return this._button; | |
196 | |
197 this._sourcesView = sourcesView; | |
198 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.Edito
rSelected, this._editorSelected.bind(this)); | |
199 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.Edito
rClosed, this._editorClosed.bind(this)); | |
200 | |
201 this._button = new WebInspector.ToolbarButton(WebInspector.UIString("Pre
tty print"), "format-toolbar-item"); | |
202 this._button.addEventListener("click", this._toggleFormatScriptSource, t
his); | |
203 this._updateButton(sourcesView.currentUISourceCode()); | |
204 | |
205 return this._button; | |
206 }, | |
207 | |
208 /** | |
209 * @param {?WebInspector.UISourceCode} uiSourceCode | |
210 * @return {boolean} | 304 * @return {boolean} |
211 */ | 305 */ |
212 _isFormatableScript: function(uiSourceCode) | 306 function isInlineScript(script) { |
213 { | 307 return script.isInlineScript() && !script.hasSourceURL; |
214 if (!uiSourceCode) | 308 } |
215 return false; | 309 |
216 if (WebInspector.persistence.binding(uiSourceCode)) | 310 if (uiSourceCode.contentType() === WebInspector.resourceTypes.Document) { |
217 return false; | 311 var scripts = []; |
218 var supportedProjectTypes = [WebInspector.projectTypes.Network, WebInspe
ctor.projectTypes.Debugger, WebInspector.projectTypes.ContentScripts]; | 312 var debuggerModels = WebInspector.DebuggerModel.instances(); |
219 if (supportedProjectTypes.indexOf(uiSourceCode.project().type()) === -1) | 313 for (var i = 0; i < debuggerModels.length; ++i) |
220 return false; | 314 scripts.pushAll(debuggerModels[i].scriptsForSourceURL(uiSourceCode.url()
)); |
221 return uiSourceCode.contentType().hasScripts(); | 315 return scripts.filter(isInlineScript); |
222 }, | 316 } |
223 | 317 if (uiSourceCode.contentType().isScript()) { |
224 _toggleFormatScriptSource: function() | 318 var rawLocations = WebInspector.debuggerWorkspaceBinding.uiLocationToRawLo
cations(uiSourceCode, 0, 0); |
225 { | 319 return rawLocations.map(function(rawLocation) { |
226 var uiSourceCode = this._sourcesView.currentUISourceCode(); | 320 return rawLocation.script(); |
227 if (this._isFormatableScript(uiSourceCode)) | 321 }); |
228 this._formatUISourceCodeScript(uiSourceCode); | 322 } |
229 }, | 323 return []; |
| 324 } |
| 325 |
| 326 /** |
| 327 * @param {!WebInspector.UISourceCode} uiSourceCode |
| 328 */ |
| 329 _formatUISourceCodeScript(uiSourceCode) { |
| 330 var formattedPath = this._formattedPaths.get(uiSourceCode.project().id() + '
:' + uiSourceCode.url()); |
| 331 if (formattedPath) { |
| 332 var uiSourceCodePath = formattedPath; |
| 333 var formattedUISourceCode = this._workspace.uiSourceCode(this._projectId,
uiSourceCodePath); |
| 334 var formatData = formattedUISourceCode ? this._formatData.get(formattedUIS
ourceCode) : null; |
| 335 if (formatData) |
| 336 this._showIfNeeded( |
| 337 uiSourceCode, /** @type {!WebInspector.UISourceCode} */ (formattedUI
SourceCode), formatData.mapping); |
| 338 return; |
| 339 } |
| 340 |
| 341 uiSourceCode.requestContent().then(contentLoaded.bind(this)); |
230 | 342 |
231 /** | 343 /** |
232 * @param {!WebInspector.UISourceCode} uiSourceCode | 344 * @this {WebInspector.ScriptFormatterEditorAction} |
233 * @param {!WebInspector.UISourceCode} formattedUISourceCode | 345 * @param {?string} content |
234 * @param {!WebInspector.FormatterSourceMapping} mapping | |
235 * @private | |
236 */ | 346 */ |
237 _showIfNeeded: function(uiSourceCode, formattedUISourceCode, mapping) | 347 function contentLoaded(content) { |
238 { | 348 var highlighterType = WebInspector.NetworkProject.uiSourceCodeMimeType(uiS
ourceCode); |
239 if (uiSourceCode !== this._sourcesView.currentUISourceCode()) | 349 WebInspector.Formatter.format( |
240 return; | 350 uiSourceCode.contentType(), highlighterType, content || '', innerCallb
ack.bind(this)); |
241 var sourceFrame = this._sourcesView.viewForFile(uiSourceCode); | 351 } |
242 var start = [0, 0]; | |
243 if (sourceFrame) { | |
244 var selection = sourceFrame.selection(); | |
245 start = mapping.originalToFormatted(selection.startLine, selection.s
tartColumn); | |
246 } | |
247 this._sourcesView.showSourceLocation(formattedUISourceCode, start[0], st
art[1]); | |
248 this._updateButton(formattedUISourceCode); | |
249 }, | |
250 | 352 |
251 /** | 353 /** |
252 * @param {!WebInspector.UISourceCode} formattedUISourceCode | 354 * @this {WebInspector.ScriptFormatterEditorAction} |
| 355 * @param {string} formattedContent |
| 356 * @param {!WebInspector.FormatterSourceMapping} formatterMapping |
253 */ | 357 */ |
254 _discardFormattedUISourceCodeScript: function(formattedUISourceCode) | 358 function innerCallback(formattedContent, formatterMapping) { |
255 { | 359 var scripts = this._scriptsForUISourceCode(uiSourceCode); |
256 var formatData = this._formatData.get(formattedUISourceCode); | 360 var formattedURL = uiSourceCode.url() + ':formatted'; |
257 if (!formatData) | 361 var contentProvider = |
258 return; | 362 WebInspector.StaticContentProvider.fromString(formattedURL, uiSourceCo
de.contentType(), formattedContent); |
259 | 363 var formattedUISourceCode = this._project.addContentProvider(formattedURL,
contentProvider); |
260 this._formatData.remove(formattedUISourceCode); | 364 var formattedPath = formattedUISourceCode.url(); |
261 var path = formatData.projectId + ":" + formatData.path; | 365 var formatData = new WebInspector.FormatterScriptMapping.FormatData( |
262 this._formattedPaths.remove(path); | 366 uiSourceCode.project().id(), uiSourceCode.url(), formatterMapping, scr
ipts); |
263 this._pathsToFormatOnLoad.delete(path); | 367 this._formatData.set(formattedUISourceCode, formatData); |
264 for (var i = 0; i < formatData.scripts.length; ++i) { | 368 var path = uiSourceCode.project().id() + ':' + uiSourceCode.url(); |
265 this._uiSourceCodes.remove(formatData.scripts[i]); | 369 this._formattedPaths.set(path, formattedPath); |
266 WebInspector.debuggerWorkspaceBinding.popSourceMapping(formatData.sc
ripts[i]); | 370 this._pathsToFormatOnLoad.add(path); |
267 } | 371 for (var i = 0; i < scripts.length; ++i) { |
268 this._project.removeFile(formattedUISourceCode.url()); | 372 this._uiSourceCodes.set(scripts[i], formattedUISourceCode); |
269 }, | 373 var scriptMapping = |
270 | 374 /** @type {!WebInspector.FormatterScriptMapping} */ (this._scriptMap
pingByTarget.get(scripts[i].target())); |
271 /** | 375 WebInspector.debuggerWorkspaceBinding.pushSourceMapping(scripts[i], scri
ptMapping); |
272 * @param {!WebInspector.Target} target | 376 } |
273 */ | 377 |
274 _cleanForTarget: function(target) | 378 var targets = WebInspector.targetManager.targets(); |
275 { | 379 for (var i = 0; i < targets.length; ++i) { |
276 var uiSourceCodes = this._formatData.keysArray(); | 380 var scriptMapping = |
277 for (var i = 0; i < uiSourceCodes.length; ++i) { | 381 /** @type {!WebInspector.FormatterScriptMapping} */ (this._scriptMap
pingByTarget.get(targets[i])); |
278 WebInspector.debuggerWorkspaceBinding.setSourceMapping(target, uiSou
rceCodes[i], null); | 382 WebInspector.debuggerWorkspaceBinding.setSourceMapping(targets[i], forma
ttedUISourceCode, scriptMapping); |
279 var formatData = this._formatData.get(uiSourceCodes[i]); | 383 } |
280 var scripts = []; | 384 this._showIfNeeded(uiSourceCode, formattedUISourceCode, formatterMapping); |
281 for (var j = 0; j < formatData.scripts.length; ++j) { | 385 } |
282 if (formatData.scripts[j].target() === target) | 386 } |
283 this._uiSourceCodes.remove(formatData.scripts[j]); | |
284 else | |
285 scripts.push(formatData.scripts[j]); | |
286 } | |
287 | |
288 if (scripts.length) | |
289 formatData.scripts = scripts; | |
290 else { | |
291 this._formattedPaths.remove(formatData.projectId + ":" + formatD
ata.path); | |
292 this._formatData.remove(uiSourceCodes[i]); | |
293 this._project.removeFile(uiSourceCodes[i].url()); | |
294 } | |
295 } | |
296 }, | |
297 | |
298 /** | |
299 * @param {!WebInspector.Event} event | |
300 */ | |
301 _debuggerReset: function(event) | |
302 { | |
303 var debuggerModel = /** @type {!WebInspector.DebuggerModel} */ (event.ta
rget); | |
304 this._cleanForTarget(debuggerModel.target()); | |
305 }, | |
306 | |
307 /** | |
308 * @param {!WebInspector.UISourceCode} uiSourceCode | |
309 * @return {!Array.<!WebInspector.Script>} | |
310 */ | |
311 _scriptsForUISourceCode: function(uiSourceCode) | |
312 { | |
313 /** | |
314 * @param {!WebInspector.Script} script | |
315 * @return {boolean} | |
316 */ | |
317 function isInlineScript(script) | |
318 { | |
319 return script.isInlineScript() && !script.hasSourceURL; | |
320 } | |
321 | |
322 if (uiSourceCode.contentType() === WebInspector.resourceTypes.Document)
{ | |
323 var scripts = []; | |
324 var debuggerModels = WebInspector.DebuggerModel.instances(); | |
325 for (var i = 0; i < debuggerModels.length; ++i) | |
326 scripts.pushAll(debuggerModels[i].scriptsForSourceURL(uiSourceCo
de.url())); | |
327 return scripts.filter(isInlineScript); | |
328 } | |
329 if (uiSourceCode.contentType().isScript()) { | |
330 var rawLocations = WebInspector.debuggerWorkspaceBinding.uiLocationT
oRawLocations(uiSourceCode, 0, 0); | |
331 return rawLocations.map(function(rawLocation) { return rawLocation.s
cript(); }); | |
332 } | |
333 return []; | |
334 }, | |
335 | |
336 /** | |
337 * @param {!WebInspector.UISourceCode} uiSourceCode | |
338 */ | |
339 _formatUISourceCodeScript: function(uiSourceCode) | |
340 { | |
341 var formattedPath = this._formattedPaths.get(uiSourceCode.project().id()
+ ":" + uiSourceCode.url()); | |
342 if (formattedPath) { | |
343 var uiSourceCodePath = formattedPath; | |
344 var formattedUISourceCode = this._workspace.uiSourceCode(this._proje
ctId, uiSourceCodePath); | |
345 var formatData = formattedUISourceCode ? this._formatData.get(format
tedUISourceCode) : null; | |
346 if (formatData) | |
347 this._showIfNeeded(uiSourceCode, /** @type {!WebInspector.UISour
ceCode} */ (formattedUISourceCode), formatData.mapping); | |
348 return; | |
349 } | |
350 | |
351 uiSourceCode.requestContent().then(contentLoaded.bind(this)); | |
352 | |
353 /** | |
354 * @this {WebInspector.ScriptFormatterEditorAction} | |
355 * @param {?string} content | |
356 */ | |
357 function contentLoaded(content) | |
358 { | |
359 var highlighterType = WebInspector.NetworkProject.uiSourceCodeMimeTy
pe(uiSourceCode); | |
360 WebInspector.Formatter.format(uiSourceCode.contentType(), highlighte
rType, content || "", innerCallback.bind(this)); | |
361 } | |
362 | |
363 /** | |
364 * @this {WebInspector.ScriptFormatterEditorAction} | |
365 * @param {string} formattedContent | |
366 * @param {!WebInspector.FormatterSourceMapping} formatterMapping | |
367 */ | |
368 function innerCallback(formattedContent, formatterMapping) | |
369 { | |
370 var scripts = this._scriptsForUISourceCode(uiSourceCode); | |
371 var formattedURL = uiSourceCode.url() + ":formatted"; | |
372 var contentProvider = WebInspector.StaticContentProvider.fromString(
formattedURL, uiSourceCode.contentType(), formattedContent); | |
373 var formattedUISourceCode = this._project.addContentProvider(formatt
edURL, contentProvider); | |
374 var formattedPath = formattedUISourceCode.url(); | |
375 var formatData = new WebInspector.FormatterScriptMapping.FormatData(
uiSourceCode.project().id(), uiSourceCode.url(), formatterMapping, scripts); | |
376 this._formatData.set(formattedUISourceCode, formatData); | |
377 var path = uiSourceCode.project().id() + ":" + uiSourceCode.url(); | |
378 this._formattedPaths.set(path, formattedPath); | |
379 this._pathsToFormatOnLoad.add(path); | |
380 for (var i = 0; i < scripts.length; ++i) { | |
381 this._uiSourceCodes.set(scripts[i], formattedUISourceCode); | |
382 var scriptMapping = /** @type {!WebInspector.FormatterScriptMapp
ing} */(this._scriptMappingByTarget.get(scripts[i].target())); | |
383 WebInspector.debuggerWorkspaceBinding.pushSourceMapping(scripts[
i], scriptMapping); | |
384 } | |
385 | |
386 var targets = WebInspector.targetManager.targets(); | |
387 for (var i = 0; i < targets.length; ++i) { | |
388 var scriptMapping = /** @type {!WebInspector.FormatterScriptMapp
ing} */(this._scriptMappingByTarget.get(targets[i])); | |
389 WebInspector.debuggerWorkspaceBinding.setSourceMapping(targets[i
], formattedUISourceCode, scriptMapping); | |
390 } | |
391 this._showIfNeeded(uiSourceCode, formattedUISourceCode, formatterMap
ping); | |
392 } | |
393 } | |
394 }; | 387 }; |
OLD | NEW |