Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / sources / InplaceFormatterEditorAction.js
blob86d28e583eb2f0abd1294eef296341ad8048d08b
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.
6 /**
7 * @constructor
8 * @implements {WebInspector.SourcesView.EditorAction}
9 */
10 WebInspector.InplaceFormatterEditorAction = function()
14 WebInspector.InplaceFormatterEditorAction.prototype = {
15 /**
16 * @param {!WebInspector.Event} event
18 _editorSelected: function(event)
20 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data);
21 this._updateButton(uiSourceCode);
24 /**
25 * @param {!WebInspector.Event} event
27 _editorClosed: function(event)
29 var wasSelected = /** @type {boolean} */ (event.data.wasSelected);
30 if (wasSelected)
31 this._updateButton(null);
34 /**
35 * @param {?WebInspector.UISourceCode} uiSourceCode
37 _updateButton: function(uiSourceCode)
39 this._button.element.classList.toggle("hidden", !this._isFormattable(uiSourceCode));
42 /**
43 * @override
44 * @param {!WebInspector.SourcesView} sourcesView
45 * @return {!WebInspector.ToolbarButton}
47 button: function(sourcesView)
49 if (this._button)
50 return this._button;
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());
61 return this._button;
64 /**
65 * @param {?WebInspector.UISourceCode} uiSourceCode
66 * @return {boolean}
68 _isFormattable: function(uiSourceCode)
70 if (!uiSourceCode)
71 return false;
72 if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem)
73 return true;
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))
82 return;
84 if (uiSourceCode.isDirty())
85 contentLoaded.call(this, uiSourceCode.workingCopy());
86 else
87 uiSourceCode.requestContent(contentLoaded.bind(this));
89 /**
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));
99 /**
100 * @this {WebInspector.InplaceFormatterEditorAction}
101 * @param {string} formattedContent
102 * @param {!WebInspector.FormatterSourceMapping} formatterMapping
104 function innerCallback(formattedContent, formatterMapping)
106 if (uiSourceCode.workingCopy() === formattedContent)
107 return;
108 var sourceFrame = this._sourcesView.viewForFile(uiSourceCode);
109 var start = [0, 0];
110 if (sourceFrame) {
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]);