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

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

Issue 2570263002: DevTools: introduce API for the Common.FormatterWorkerPool. (Closed)
Patch Set: address comments Created 4 years 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 28 matching lines...) Expand all
39 * @param {function(string, !Sources.FormatterSourceMapping)} callback 39 * @param {function(string, !Sources.FormatterSourceMapping)} callback
40 */ 40 */
41 Sources.Formatter.format = function(contentType, mimeType, content, callback) { 41 Sources.Formatter.format = function(contentType, mimeType, content, callback) {
42 if (contentType.isDocumentOrScriptOrStyleSheet()) 42 if (contentType.isDocumentOrScriptOrStyleSheet())
43 new Sources.ScriptFormatter(mimeType, content, callback); 43 new Sources.ScriptFormatter(mimeType, content, callback);
44 else 44 else
45 new Sources.ScriptIdentityFormatter(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 Sources.Formatter.locationToPosition = function(lineEndings, lineNumber, columnN umber) { 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 Sources.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 {Sources.Formatter} 74 * @implements {Sources.Formatter}
75 * @unrestricted 75 * @unrestricted
76 */ 76 */
77 Sources.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, !Sources.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 Common.formatterWorkerPool.format(mimeType, content, Common.moduleSetting('t extEditorIndent').get())
89 mimeType: mimeType, 89 .then(this._didFormatContent.bind(this));
90 content: content,
91 indentString: Common.moduleSetting('textEditorIndent').get()
92 };
93 Common.formatterWorkerPool.runTask('format', parameters).then(this._didForma tContent.bind(this));
94 } 90 }
95 91
96 /** 92 /**
97 * @param {?MessageEvent} event 93 * @param {!Common.FormatterWorkerPool.FormatResult} formatResult
98 */ 94 */
99 _didFormatContent(event) { 95 _didFormatContent(formatResult) {
100 var formattedContent = '';
101 var mapping = [];
102 if (event) {
103 formattedContent = event.data.content;
104 mapping = event.data['mapping'];
105 }
106 var sourceMapping = new Sources.FormatterSourceMappingImpl( 96 var sourceMapping = new Sources.FormatterSourceMappingImpl(
107 this._originalContent.computeLineEndings(), formattedContent.computeLine Endings(), mapping); 97 this._originalContent.computeLineEndings(), formatResult.content.compute LineEndings(), formatResult.mapping);
108 this._callback(formattedContent, sourceMapping); 98 this._callback(formatResult.content, sourceMapping);
109 } 99 }
110 }; 100 };
111 101
112 /** 102 /**
113 * @implements {Sources.Formatter} 103 * @implements {Sources.Formatter}
114 * @unrestricted 104 * @unrestricted
115 */ 105 */
116 Sources.ScriptIdentityFormatter = class { 106 Sources.ScriptIdentityFormatter = class {
117 /** 107 /**
118 * @param {string} mimeType 108 * @param {string} mimeType
119 * @param {string} content 109 * @param {string} content
120 * @param {function(string, !Sources.FormatterSourceMapping)} callback 110 * @param {function(string, !Sources.FormatterSourceMapping)} callback
121 */ 111 */
122 constructor(mimeType, content, callback) { 112 constructor(mimeType, content, callback) {
123 callback(content, new Sources.IdentityFormatterSourceMapping()); 113 callback(content, new Sources.IdentityFormatterSourceMapping());
124 } 114 }
125 }; 115 };
126 116
127 /** 117 /**
128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}}
129 */
130 Sources.FormatterMappingPayload;
131
132 /**
133 * @interface 118 * @interface
134 */ 119 */
135 Sources.FormatterSourceMapping = function() {}; 120 Sources.FormatterSourceMapping = function() {};
136 121
137 Sources.FormatterSourceMapping.prototype = { 122 Sources.FormatterSourceMapping.prototype = {
138 /** 123 /**
139 * @param {number} lineNumber 124 * @param {number} lineNumber
140 * @param {number=} columnNumber 125 * @param {number=} columnNumber
141 * @return {!Array.<number>} 126 * @return {!Array.<number>}
142 */ 127 */
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 }; 162 };
178 163
179 /** 164 /**
180 * @implements {Sources.FormatterSourceMapping} 165 * @implements {Sources.FormatterSourceMapping}
181 * @unrestricted 166 * @unrestricted
182 */ 167 */
183 Sources.FormatterSourceMappingImpl = class { 168 Sources.FormatterSourceMappingImpl = class {
184 /** 169 /**
185 * @param {!Array.<number>} originalLineEndings 170 * @param {!Array.<number>} originalLineEndings
186 * @param {!Array.<number>} formattedLineEndings 171 * @param {!Array.<number>} formattedLineEndings
187 * @param {!Sources.FormatterMappingPayload} mapping 172 * @param {!Common.FormatterWorkerPool.FormatMapping} mapping
188 */ 173 */
189 constructor(originalLineEndings, formattedLineEndings, mapping) { 174 constructor(originalLineEndings, formattedLineEndings, mapping) {
190 this._originalLineEndings = originalLineEndings; 175 this._originalLineEndings = originalLineEndings;
191 this._formattedLineEndings = formattedLineEndings; 176 this._formattedLineEndings = formattedLineEndings;
192 this._mapping = mapping; 177 this._mapping = mapping;
193 } 178 }
194 179
195 /** 180 /**
196 * @override 181 * @override
197 * @param {number} lineNumber 182 * @param {number} lineNumber
(...skipping 28 matching lines...) Expand all
226 * @return {number} 211 * @return {number}
227 */ 212 */
228 _convertPosition(positions1, positions2, position) { 213 _convertPosition(positions1, positions2, position) {
229 var index = positions1.upperBound(position) - 1; 214 var index = positions1.upperBound(position) - 1;
230 var convertedPosition = positions2[index] + position - positions1[index]; 215 var convertedPosition = positions2[index] + position - positions1[index];
231 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1]) 216 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1])
232 convertedPosition = positions2[index + 1]; 217 convertedPosition = positions2[index + 1];
233 return convertedPosition; 218 return convertedPosition;
234 } 219 }
235 }; 220 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698