2 // Copyright 2014 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
8 * @implements {WebInspector.SourcesView.EditorAction}
10 WebInspector
.InplaceFormatterEditorAction = function()
14 WebInspector
.InplaceFormatterEditorAction
.prototype = {
16 * @param {!WebInspector.Event} event
18 _editorSelected: function(event
)
20 var uiSourceCode
= /** @type {!WebInspector.UISourceCode} */ (event
.data
);
21 this._updateButton(uiSourceCode
);
25 * @param {!WebInspector.Event} event
27 _editorClosed: function(event
)
29 var wasSelected
= /** @type {boolean} */ (event
.data
.wasSelected
);
31 this._updateButton(null);
35 * @param {?WebInspector.UISourceCode} uiSourceCode
37 _updateButton: function(uiSourceCode
)
39 this._button
.element
.classList
.toggle("hidden", !this._isFormattable(uiSourceCode
));
44 * @param {!WebInspector.SourcesView} sourcesView
45 * @return {!WebInspector.ToolbarButton}
47 button: function(sourcesView
)
52 this._sourcesView
= sourcesView
;
53 this._sourcesView
.addEventListener(WebInspector
.SourcesView
.Events
.EditorSelected
, this._editorSelected
.bind(this));
54 this._sourcesView
.addEventListener(WebInspector
.SourcesView
.Events
.EditorClosed
, this._editorClosed
.bind(this));
56 this._button
= new WebInspector
.ToolbarButton(WebInspector
.UIString("Format"), "format-toolbar-item");
57 this._button
.setToggled(false);
58 this._button
.addEventListener("click", this._formatSourceInPlace
, this);
59 this._updateButton(sourcesView
.currentUISourceCode());
65 * @param {?WebInspector.UISourceCode} uiSourceCode
68 _isFormattable: function(uiSourceCode
)
72 if (uiSourceCode
.project().type() === WebInspector
.projectTypes
.FileSystem
)
74 return uiSourceCode
.contentType() === WebInspector
.resourceTypes
.Stylesheet
75 || uiSourceCode
.project().type() === WebInspector
.projectTypes
.Snippets
;
78 _formatSourceInPlace: function()
80 var uiSourceCode
= this._sourcesView
.currentUISourceCode();
81 if (!this._isFormattable(uiSourceCode
))
84 if (uiSourceCode
.isDirty())
85 contentLoaded
.call(this, uiSourceCode
.workingCopy());
87 uiSourceCode
.requestContent(contentLoaded
.bind(this));
90 * @this {WebInspector.InplaceFormatterEditorAction}
91 * @param {?string} content
93 function contentLoaded(content
)
95 var highlighterType
= WebInspector
.SourcesView
.uiSourceCodeHighlighterType(uiSourceCode
);
96 WebInspector
.Formatter
.format(uiSourceCode
.contentType(), highlighterType
, content
|| "", innerCallback
.bind(this));
100 * @this {WebInspector.InplaceFormatterEditorAction}
101 * @param {string} formattedContent
102 * @param {!WebInspector.FormatterSourceMapping} formatterMapping
104 function innerCallback(formattedContent
, formatterMapping
)
106 if (uiSourceCode
.workingCopy() === formattedContent
)
108 var sourceFrame
= this._sourcesView
.viewForFile(uiSourceCode
);
111 var selection
= sourceFrame
.selection();
112 start
= formatterMapping
.originalToFormatted(selection
.startLine
, selection
.startColumn
);
114 uiSourceCode
.setWorkingCopy(formattedContent
);
115 this._sourcesView
.showSourceLocation(uiSourceCode
, start
[0], start
[1]);