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

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

Issue 2493373002: DevTools: rename WebInspector into modules. (Closed)
Patch Set: for bots 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
(...skipping 12 matching lines...) Expand all
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 /** 30 /**
31 * @interface 31 * @interface
32 */ 32 */
33 WebInspector.Formatter = function() {}; 33 Sources.Formatter = function() {};
34 34
35 /** 35 /**
36 * @param {!WebInspector.ResourceType} contentType 36 * @param {!Common.ResourceType} contentType
37 * @param {string} mimeType 37 * @param {string} mimeType
38 * @param {string} content 38 * @param {string} content
39 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback 39 * @param {function(string, !Sources.FormatterSourceMapping)} callback
40 */ 40 */
41 WebInspector.Formatter.format = function(contentType, mimeType, content, callbac k) { 41 Sources.Formatter.format = function(contentType, mimeType, content, callback) {
42 if (contentType.isDocumentOrScriptOrStyleSheet()) 42 if (contentType.isDocumentOrScriptOrStyleSheet())
43 new WebInspector.ScriptFormatter(mimeType, content, callback); 43 new Sources.ScriptFormatter(mimeType, content, callback);
44 else 44 else
45 new WebInspector.IdentityFormatter(mimeType, content, callback); 45 new Sources.ScriptIdentityFormatter(mimeType, content, callback);
46 }; 46 };
47 47
48 /** 48 /**
49 * @param {!Array.<number>} lineEndings 49 * @param {!Array.<number>} lineEndings
50 * @param {number} lineNumber 50 * @param {number} lineNumber
51 * @param {number} columnNumber 51 * @param {number} columnNumber
52 * @return {number} 52 * @return {number}
53 */ 53 */
54 WebInspector.Formatter.locationToPosition = function(lineEndings, lineNumber, co lumnNumber) { 54 Sources.Formatter.locationToPosition = function(lineEndings, lineNumber, columnN umber) {
55 var position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0; 55 var position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0;
56 return position + columnNumber; 56 return position + columnNumber;
57 }; 57 };
58 58
59 /** 59 /**
60 * @param {!Array.<number>} lineEndings 60 * @param {!Array.<number>} lineEndings
61 * @param {number} position 61 * @param {number} position
62 * @return {!Array.<number>} 62 * @return {!Array.<number>}
63 */ 63 */
64 WebInspector.Formatter.positionToLocation = function(lineEndings, position) { 64 Sources.Formatter.positionToLocation = function(lineEndings, position) {
65 var lineNumber = lineEndings.upperBound(position - 1); 65 var lineNumber = lineEndings.upperBound(position - 1);
66 if (!lineNumber) 66 if (!lineNumber)
67 var columnNumber = position; 67 var columnNumber = position;
68 else 68 else
69 var columnNumber = position - lineEndings[lineNumber - 1] - 1; 69 var columnNumber = position - lineEndings[lineNumber - 1] - 1;
70 return [lineNumber, columnNumber]; 70 return [lineNumber, columnNumber];
71 }; 71 };
72 72
73 /** 73 /**
74 * @implements {WebInspector.Formatter} 74 * @implements {Sources.Formatter}
75 * @unrestricted 75 * @unrestricted
76 */ 76 */
77 WebInspector.ScriptFormatter = class { 77 Sources.ScriptFormatter = class {
78 /** 78 /**
79 * @param {string} mimeType 79 * @param {string} mimeType
80 * @param {string} content 80 * @param {string} content
81 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback 81 * @param {function(string, !Sources.FormatterSourceMapping)} callback
82 */ 82 */
83 constructor(mimeType, content, callback) { 83 constructor(mimeType, content, callback) {
84 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/ , ''); 84 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, '\n').replace(/^\uFEFF/ , '');
85 this._callback = callback; 85 this._callback = callback;
86 this._originalContent = content; 86 this._originalContent = content;
87 87
88 var parameters = { 88 var parameters = {
89 mimeType: mimeType, 89 mimeType: mimeType,
90 content: content, 90 content: content,
91 indentString: WebInspector.moduleSetting('textEditorIndent').get() 91 indentString: Common.moduleSetting('textEditorIndent').get()
92 }; 92 };
93 WebInspector.formatterWorkerPool.runTask('format', parameters).then(this._di dFormatContent.bind(this)); 93 Common.formatterWorkerPool.runTask('format', parameters).then(this._didForma tContent.bind(this));
94 } 94 }
95 95
96 /** 96 /**
97 * @param {?MessageEvent} event 97 * @param {?MessageEvent} event
98 */ 98 */
99 _didFormatContent(event) { 99 _didFormatContent(event) {
100 var formattedContent = ''; 100 var formattedContent = '';
101 var mapping = []; 101 var mapping = [];
102 if (event) { 102 if (event) {
103 formattedContent = event.data.content; 103 formattedContent = event.data.content;
104 mapping = event.data['mapping']; 104 mapping = event.data['mapping'];
105 } 105 }
106 var sourceMapping = new WebInspector.FormatterSourceMappingImpl( 106 var sourceMapping = new Sources.FormatterSourceMappingImpl(
107 this._originalContent.computeLineEndings(), formattedContent.computeLine Endings(), mapping); 107 this._originalContent.computeLineEndings(), formattedContent.computeLine Endings(), mapping);
108 this._callback(formattedContent, sourceMapping); 108 this._callback(formattedContent, sourceMapping);
109 } 109 }
110 }; 110 };
111 111
112 /** 112 /**
113 * @implements {WebInspector.Formatter} 113 * @implements {Sources.Formatter}
114 * @unrestricted 114 * @unrestricted
115 */ 115 */
116 WebInspector.IdentityFormatter = class { 116 Sources.ScriptIdentityFormatter = class {
117 /** 117 /**
118 * @param {string} mimeType 118 * @param {string} mimeType
119 * @param {string} content 119 * @param {string} content
120 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback 120 * @param {function(string, !Sources.FormatterSourceMapping)} callback
121 */ 121 */
122 constructor(mimeType, content, callback) { 122 constructor(mimeType, content, callback) {
123 callback(content, new WebInspector.IdentityFormatterSourceMapping()); 123 callback(content, new Sources.IdentityFormatterSourceMapping());
124 } 124 }
125 }; 125 };
126 126
127 /** 127 /**
128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}} 128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}}
129 */ 129 */
130 WebInspector.FormatterMappingPayload; 130 Sources.FormatterMappingPayload;
131 131
132 /** 132 /**
133 * @interface 133 * @interface
134 */ 134 */
135 WebInspector.FormatterSourceMapping = function() {}; 135 Sources.FormatterSourceMapping = function() {};
136 136
137 WebInspector.FormatterSourceMapping.prototype = { 137 Sources.FormatterSourceMapping.prototype = {
138 /** 138 /**
139 * @param {number} lineNumber 139 * @param {number} lineNumber
140 * @param {number=} columnNumber 140 * @param {number=} columnNumber
141 * @return {!Array.<number>} 141 * @return {!Array.<number>}
142 */ 142 */
143 originalToFormatted: function(lineNumber, columnNumber) {}, 143 originalToFormatted: function(lineNumber, columnNumber) {},
144 144
145 /** 145 /**
146 * @param {number} lineNumber 146 * @param {number} lineNumber
147 * @param {number=} columnNumber 147 * @param {number=} columnNumber
148 * @return {!Array.<number>} 148 * @return {!Array.<number>}
149 */ 149 */
150 formattedToOriginal: function(lineNumber, columnNumber) {} 150 formattedToOriginal: function(lineNumber, columnNumber) {}
151 }; 151 };
152 152
153 /** 153 /**
154 * @implements {WebInspector.FormatterSourceMapping} 154 * @implements {Sources.FormatterSourceMapping}
155 * @unrestricted 155 * @unrestricted
156 */ 156 */
157 WebInspector.IdentityFormatterSourceMapping = class { 157 Sources.IdentityFormatterSourceMapping = class {
158 /** 158 /**
159 * @override 159 * @override
160 * @param {number} lineNumber 160 * @param {number} lineNumber
161 * @param {number=} columnNumber 161 * @param {number=} columnNumber
162 * @return {!Array.<number>} 162 * @return {!Array.<number>}
163 */ 163 */
164 originalToFormatted(lineNumber, columnNumber) { 164 originalToFormatted(lineNumber, columnNumber) {
165 return [lineNumber, columnNumber || 0]; 165 return [lineNumber, columnNumber || 0];
166 } 166 }
167 167
168 /** 168 /**
169 * @override 169 * @override
170 * @param {number} lineNumber 170 * @param {number} lineNumber
171 * @param {number=} columnNumber 171 * @param {number=} columnNumber
172 * @return {!Array.<number>} 172 * @return {!Array.<number>}
173 */ 173 */
174 formattedToOriginal(lineNumber, columnNumber) { 174 formattedToOriginal(lineNumber, columnNumber) {
175 return [lineNumber, columnNumber || 0]; 175 return [lineNumber, columnNumber || 0];
176 } 176 }
177 }; 177 };
178 178
179 /** 179 /**
180 * @implements {WebInspector.FormatterSourceMapping} 180 * @implements {Sources.FormatterSourceMapping}
181 * @unrestricted 181 * @unrestricted
182 */ 182 */
183 WebInspector.FormatterSourceMappingImpl = class { 183 Sources.FormatterSourceMappingImpl = class {
184 /** 184 /**
185 * @param {!Array.<number>} originalLineEndings 185 * @param {!Array.<number>} originalLineEndings
186 * @param {!Array.<number>} formattedLineEndings 186 * @param {!Array.<number>} formattedLineEndings
187 * @param {!WebInspector.FormatterMappingPayload} mapping 187 * @param {!Sources.FormatterMappingPayload} mapping
188 */ 188 */
189 constructor(originalLineEndings, formattedLineEndings, mapping) { 189 constructor(originalLineEndings, formattedLineEndings, mapping) {
190 this._originalLineEndings = originalLineEndings; 190 this._originalLineEndings = originalLineEndings;
191 this._formattedLineEndings = formattedLineEndings; 191 this._formattedLineEndings = formattedLineEndings;
192 this._mapping = mapping; 192 this._mapping = mapping;
193 } 193 }
194 194
195 /** 195 /**
196 * @override 196 * @override
197 * @param {number} lineNumber 197 * @param {number} lineNumber
198 * @param {number=} columnNumber 198 * @param {number=} columnNumber
199 * @return {!Array.<number>} 199 * @return {!Array.<number>}
200 */ 200 */
201 originalToFormatted(lineNumber, columnNumber) { 201 originalToFormatted(lineNumber, columnNumber) {
202 var originalPosition = 202 var originalPosition =
203 WebInspector.Formatter.locationToPosition(this._originalLineEndings, lin eNumber, columnNumber || 0); 203 Sources.Formatter.locationToPosition(this._originalLineEndings, lineNumb er, columnNumber || 0);
204 var formattedPosition = 204 var formattedPosition =
205 this._convertPosition(this._mapping.original, this._mapping.formatted, o riginalPosition || 0); 205 this._convertPosition(this._mapping.original, this._mapping.formatted, o riginalPosition || 0);
206 return WebInspector.Formatter.positionToLocation(this._formattedLineEndings, formattedPosition); 206 return Sources.Formatter.positionToLocation(this._formattedLineEndings, form attedPosition);
207 } 207 }
208 208
209 /** 209 /**
210 * @override 210 * @override
211 * @param {number} lineNumber 211 * @param {number} lineNumber
212 * @param {number=} columnNumber 212 * @param {number=} columnNumber
213 * @return {!Array.<number>} 213 * @return {!Array.<number>}
214 */ 214 */
215 formattedToOriginal(lineNumber, columnNumber) { 215 formattedToOriginal(lineNumber, columnNumber) {
216 var formattedPosition = 216 var formattedPosition =
217 WebInspector.Formatter.locationToPosition(this._formattedLineEndings, li neNumber, columnNumber || 0); 217 Sources.Formatter.locationToPosition(this._formattedLineEndings, lineNum ber, columnNumber || 0);
218 var originalPosition = this._convertPosition(this._mapping.formatted, this._ mapping.original, formattedPosition); 218 var originalPosition = this._convertPosition(this._mapping.formatted, this._ mapping.original, formattedPosition);
219 return WebInspector.Formatter.positionToLocation(this._originalLineEndings, originalPosition || 0); 219 return Sources.Formatter.positionToLocation(this._originalLineEndings, origi nalPosition || 0);
220 } 220 }
221 221
222 /** 222 /**
223 * @param {!Array.<number>} positions1 223 * @param {!Array.<number>} positions1
224 * @param {!Array.<number>} positions2 224 * @param {!Array.<number>} positions2
225 * @param {number} position 225 * @param {number} position
226 * @return {number} 226 * @return {number}
227 */ 227 */
228 _convertPosition(positions1, positions2, position) { 228 _convertPosition(positions1, positions2, position) {
229 var index = positions1.upperBound(position) - 1; 229 var index = positions1.upperBound(position) - 1;
230 var convertedPosition = positions2[index] + position - positions1[index]; 230 var convertedPosition = positions2[index] + position - positions1[index];
231 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1]) 231 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1])
232 convertedPosition = positions2[index + 1]; 232 convertedPosition = positions2[index + 1];
233 return convertedPosition; 233 return convertedPosition;
234 } 234 }
235 }; 235 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698