Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2512)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/sources/ScriptFormatter.js

Issue 2466123002: DevTools: reformat front-end code to match chromium style. (Closed)
Patch Set: all done Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the 12 * in the documentation and/or other materials provided with the
13 * distribution. 13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its 14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from 15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission. 16 * this software without specific prior written permission.
17 * 17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30
31 /** 30 /**
32 * @interface 31 * @interface
33 */ 32 */
34 WebInspector.Formatter = function() 33 WebInspector.Formatter = function() {};
35 {
36 };
37 34
38 /** 35 /**
39 * @param {!WebInspector.ResourceType} contentType 36 * @param {!WebInspector.ResourceType} contentType
40 * @param {string} mimeType 37 * @param {string} mimeType
41 * @param {string} content 38 * @param {string} content
42 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback 39 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback
43 */ 40 */
44 WebInspector.Formatter.format = function(contentType, mimeType, content, callbac k) 41 WebInspector.Formatter.format = function(contentType, mimeType, content, callbac k) {
45 { 42 if (contentType.isDocumentOrScriptOrStyleSheet())
46 if (contentType.isDocumentOrScriptOrStyleSheet()) 43 new WebInspector.ScriptFormatter(mimeType, content, callback);
47 new WebInspector.ScriptFormatter(mimeType, content, callback); 44 else
48 else 45 new WebInspector.IdentityFormatter(mimeType, content, callback);
49 new WebInspector.IdentityFormatter(mimeType, content, callback);
50 }; 46 };
51 47
52 /** 48 /**
53 * @param {!Array.<number>} lineEndings 49 * @param {!Array.<number>} lineEndings
54 * @param {number} lineNumber 50 * @param {number} lineNumber
55 * @param {number} columnNumber 51 * @param {number} columnNumber
56 * @return {number} 52 * @return {number}
57 */ 53 */
58 WebInspector.Formatter.locationToPosition = function(lineEndings, lineNumber, co lumnNumber) 54 WebInspector.Formatter.locationToPosition = function(lineEndings, lineNumber, co lumnNumber) {
59 { 55 var position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0;
60 var position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0; 56 return position + columnNumber;
61 return position + columnNumber;
62 }; 57 };
63 58
64 /** 59 /**
65 * @param {!Array.<number>} lineEndings 60 * @param {!Array.<number>} lineEndings
66 * @param {number} position 61 * @param {number} position
67 * @return {!Array.<number>} 62 * @return {!Array.<number>}
68 */ 63 */
69 WebInspector.Formatter.positionToLocation = function(lineEndings, position) 64 WebInspector.Formatter.positionToLocation = function(lineEndings, position) {
70 { 65 var lineNumber = lineEndings.upperBound(position - 1);
71 var lineNumber = lineEndings.upperBound(position - 1); 66 if (!lineNumber)
72 if (!lineNumber) 67 var columnNumber = position;
73 var columnNumber = position; 68 else
74 else 69 var columnNumber = position - lineEndings[lineNumber - 1] - 1;
75 var columnNumber = position - lineEndings[lineNumber - 1] - 1; 70 return [lineNumber, columnNumber];
76 return [lineNumber, columnNumber];
77 }; 71 };
78 72
79 /** 73 /**
80 * @constructor
81 * @implements {WebInspector.Formatter} 74 * @implements {WebInspector.Formatter}
82 * @param {string} mimeType 75 * @unrestricted
83 * @param {string} content
84 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback
85 */ 76 */
86 WebInspector.ScriptFormatter = function(mimeType, content, callback) 77 WebInspector.ScriptFormatter = class {
87 { 78 /**
88 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/ , ""); 79 * @param {string} mimeType
80 * @param {string} content
81 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback
82 */
83 constructor(mimeType, content, callback) {
84 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/ , '');
89 this._callback = callback; 85 this._callback = callback;
90 this._originalContent = content; 86 this._originalContent = content;
91 87
92 var parameters = { 88 var parameters = {
93 mimeType: mimeType, 89 mimeType: mimeType,
94 content: content, 90 content: content,
95 indentString: WebInspector.moduleSetting("textEditorIndent").get() 91 indentString: WebInspector.moduleSetting('textEditorIndent').get()
96 }; 92 };
97 WebInspector.formatterWorkerPool.runTask("format", parameters) 93 WebInspector.formatterWorkerPool.runTask('format', parameters).then(this._di dFormatContent.bind(this));
98 .then(this._didFormatContent.bind(this)); 94 }
99 };
100 95
101 WebInspector.ScriptFormatter.prototype = { 96 /**
102 /** 97 * @param {?MessageEvent} event
103 * @param {?MessageEvent} event 98 */
104 */ 99 _didFormatContent(event) {
105 _didFormatContent: function(event) 100 var formattedContent = '';
106 { 101 var mapping = [];
107 var formattedContent = ""; 102 if (event) {
108 var mapping = []; 103 formattedContent = event.data.content;
109 if (event) { 104 mapping = event.data['mapping'];
110 formattedContent = event.data.content;
111 mapping = event.data["mapping"];
112 }
113 var sourceMapping = new WebInspector.FormatterSourceMappingImpl(this._or iginalContent.computeLineEndings(), formattedContent.computeLineEndings(), mappi ng);
114 this._callback(formattedContent, sourceMapping);
115 } 105 }
106 var sourceMapping = new WebInspector.FormatterSourceMappingImpl(
107 this._originalContent.computeLineEndings(), formattedContent.computeLine Endings(), mapping);
108 this._callback(formattedContent, sourceMapping);
109 }
116 }; 110 };
117 111
118 /** 112 /**
119 * @constructor
120 * @implements {WebInspector.Formatter} 113 * @implements {WebInspector.Formatter}
121 * @param {string} mimeType 114 * @unrestricted
122 * @param {string} content
123 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback
124 */ 115 */
125 WebInspector.IdentityFormatter = function(mimeType, content, callback) 116 WebInspector.IdentityFormatter = class {
126 { 117 /**
118 * @param {string} mimeType
119 * @param {string} content
120 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback
121 */
122 constructor(mimeType, content, callback) {
127 callback(content, new WebInspector.IdentityFormatterSourceMapping()); 123 callback(content, new WebInspector.IdentityFormatterSourceMapping());
124 }
128 }; 125 };
129 126
130 /** 127 /**
131 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}} 128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}}
132 */ 129 */
133 WebInspector.FormatterMappingPayload; 130 WebInspector.FormatterMappingPayload;
134 131
135 /** 132 /**
136 * @interface 133 * @interface
137 */ 134 */
138 WebInspector.FormatterSourceMapping = function() 135 WebInspector.FormatterSourceMapping = function() {};
139 {
140 };
141 136
142 WebInspector.FormatterSourceMapping.prototype = { 137 WebInspector.FormatterSourceMapping.prototype = {
143 /** 138 /**
144 * @param {number} lineNumber 139 * @param {number} lineNumber
145 * @param {number=} columnNumber 140 * @param {number=} columnNumber
146 * @return {!Array.<number>} 141 * @return {!Array.<number>}
147 */ 142 */
148 originalToFormatted: function(lineNumber, columnNumber) { }, 143 originalToFormatted: function(lineNumber, columnNumber) {},
149 144
150 /** 145 /**
151 * @param {number} lineNumber 146 * @param {number} lineNumber
152 * @param {number=} columnNumber 147 * @param {number=} columnNumber
153 * @return {!Array.<number>} 148 * @return {!Array.<number>}
154 */ 149 */
155 formattedToOriginal: function(lineNumber, columnNumber) { } 150 formattedToOriginal: function(lineNumber, columnNumber) {}
156 }; 151 };
157 152
158 /** 153 /**
159 * @constructor
160 * @implements {WebInspector.FormatterSourceMapping} 154 * @implements {WebInspector.FormatterSourceMapping}
155 * @unrestricted
161 */ 156 */
162 WebInspector.IdentityFormatterSourceMapping = function() 157 WebInspector.IdentityFormatterSourceMapping = class {
163 { 158 /**
164 }; 159 * @override
160 * @param {number} lineNumber
161 * @param {number=} columnNumber
162 * @return {!Array.<number>}
163 */
164 originalToFormatted(lineNumber, columnNumber) {
165 return [lineNumber, columnNumber || 0];
166 }
165 167
166 WebInspector.IdentityFormatterSourceMapping.prototype = { 168 /**
167 /** 169 * @override
168 * @override 170 * @param {number} lineNumber
169 * @param {number} lineNumber 171 * @param {number=} columnNumber
170 * @param {number=} columnNumber 172 * @return {!Array.<number>}
171 * @return {!Array.<number>} 173 */
172 */ 174 formattedToOriginal(lineNumber, columnNumber) {
173 originalToFormatted: function(lineNumber, columnNumber) 175 return [lineNumber, columnNumber || 0];
174 { 176 }
175 return [lineNumber, columnNumber || 0];
176 },
177
178 /**
179 * @override
180 * @param {number} lineNumber
181 * @param {number=} columnNumber
182 * @return {!Array.<number>}
183 */
184 formattedToOriginal: function(lineNumber, columnNumber)
185 {
186 return [lineNumber, columnNumber || 0];
187 }
188 }; 177 };
189 178
190 /** 179 /**
191 * @constructor
192 * @implements {WebInspector.FormatterSourceMapping} 180 * @implements {WebInspector.FormatterSourceMapping}
193 * @param {!Array.<number>} originalLineEndings 181 * @unrestricted
194 * @param {!Array.<number>} formattedLineEndings
195 * @param {!WebInspector.FormatterMappingPayload} mapping
196 */ 182 */
197 WebInspector.FormatterSourceMappingImpl = function(originalLineEndings, formatte dLineEndings, mapping) 183 WebInspector.FormatterSourceMappingImpl = class {
198 { 184 /**
185 * @param {!Array.<number>} originalLineEndings
186 * @param {!Array.<number>} formattedLineEndings
187 * @param {!WebInspector.FormatterMappingPayload} mapping
188 */
189 constructor(originalLineEndings, formattedLineEndings, mapping) {
199 this._originalLineEndings = originalLineEndings; 190 this._originalLineEndings = originalLineEndings;
200 this._formattedLineEndings = formattedLineEndings; 191 this._formattedLineEndings = formattedLineEndings;
201 this._mapping = mapping; 192 this._mapping = mapping;
193 }
194
195 /**
196 * @override
197 * @param {number} lineNumber
198 * @param {number=} columnNumber
199 * @return {!Array.<number>}
200 */
201 originalToFormatted(lineNumber, columnNumber) {
202 var originalPosition =
203 WebInspector.Formatter.locationToPosition(this._originalLineEndings, lin eNumber, columnNumber || 0);
204 var formattedPosition =
205 this._convertPosition(this._mapping.original, this._mapping.formatted, o riginalPosition || 0);
206 return WebInspector.Formatter.positionToLocation(this._formattedLineEndings, formattedPosition);
207 }
208
209 /**
210 * @override
211 * @param {number} lineNumber
212 * @param {number=} columnNumber
213 * @return {!Array.<number>}
214 */
215 formattedToOriginal(lineNumber, columnNumber) {
216 var formattedPosition =
217 WebInspector.Formatter.locationToPosition(this._formattedLineEndings, li neNumber, columnNumber || 0);
218 var originalPosition = this._convertPosition(this._mapping.formatted, this._ mapping.original, formattedPosition);
219 return WebInspector.Formatter.positionToLocation(this._originalLineEndings, originalPosition || 0);
220 }
221
222 /**
223 * @param {!Array.<number>} positions1
224 * @param {!Array.<number>} positions2
225 * @param {number} position
226 * @return {number}
227 */
228 _convertPosition(positions1, positions2, position) {
229 var index = positions1.upperBound(position) - 1;
230 var convertedPosition = positions2[index] + position - positions1[index];
231 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1])
232 convertedPosition = positions2[index + 1];
233 return convertedPosition;
234 }
202 }; 235 };
203
204 WebInspector.FormatterSourceMappingImpl.prototype = {
205 /**
206 * @override
207 * @param {number} lineNumber
208 * @param {number=} columnNumber
209 * @return {!Array.<number>}
210 */
211 originalToFormatted: function(lineNumber, columnNumber)
212 {
213 var originalPosition = WebInspector.Formatter.locationToPosition(this._o riginalLineEndings, lineNumber, columnNumber || 0);
214 var formattedPosition = this._convertPosition(this._mapping.original, th is._mapping.formatted, originalPosition || 0);
215 return WebInspector.Formatter.positionToLocation(this._formattedLineEndi ngs, formattedPosition);
216 },
217
218 /**
219 * @override
220 * @param {number} lineNumber
221 * @param {number=} columnNumber
222 * @return {!Array.<number>}
223 */
224 formattedToOriginal: function(lineNumber, columnNumber)
225 {
226 var formattedPosition = WebInspector.Formatter.locationToPosition(this._ formattedLineEndings, lineNumber, columnNumber || 0);
227 var originalPosition = this._convertPosition(this._mapping.formatted, th is._mapping.original, formattedPosition);
228 return WebInspector.Formatter.positionToLocation(this._originalLineEndin gs, originalPosition || 0);
229 },
230
231 /**
232 * @param {!Array.<number>} positions1
233 * @param {!Array.<number>} positions2
234 * @param {number} position
235 * @return {number}
236 */
237 _convertPosition: function(positions1, positions2, position)
238 {
239 var index = positions1.upperBound(position) - 1;
240 var convertedPosition = positions2[index] + position - positions1[index] ;
241 if (index < positions2.length - 1 && convertedPosition > positions2[inde x + 1])
242 convertedPosition = positions2[index + 1];
243 return convertedPosition;
244 }
245 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698