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

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: rebaseline 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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 {?{content: string, mapping: !{original: !Array<number>, formatted: !Array<number>}}} data
dgozman 2016/12/14 19:33:00 FormatResult?
lushnikov 2016/12/14 23:36:56 Done.
98 */ 94 */
99 _didFormatContent(event) { 95 _didFormatContent(data) {
100 var formattedContent = ''; 96 if (!data)
101 var mapping = []; 97 data = {content: '', mapping: {original: [], formatted: []}};
102 if (event) { 98
103 formattedContent = event.data.content;
104 mapping = event.data['mapping'];
105 }
106 var sourceMapping = new Sources.FormatterSourceMappingImpl( 99 var sourceMapping = new Sources.FormatterSourceMappingImpl(
107 this._originalContent.computeLineEndings(), formattedContent.computeLine Endings(), mapping); 100 this._originalContent.computeLineEndings(), data.content.computeLineEndi ngs(), data.mapping);
108 this._callback(formattedContent, sourceMapping); 101 this._callback(data.content, sourceMapping);
109 } 102 }
110 }; 103 };
111 104
112 /** 105 /**
113 * @implements {Sources.Formatter} 106 * @implements {Sources.Formatter}
114 * @unrestricted 107 * @unrestricted
115 */ 108 */
116 Sources.ScriptIdentityFormatter = class { 109 Sources.ScriptIdentityFormatter = class {
117 /** 110 /**
118 * @param {string} mimeType 111 * @param {string} mimeType
119 * @param {string} content 112 * @param {string} content
120 * @param {function(string, !Sources.FormatterSourceMapping)} callback 113 * @param {function(string, !Sources.FormatterSourceMapping)} callback
121 */ 114 */
122 constructor(mimeType, content, callback) { 115 constructor(mimeType, content, callback) {
123 callback(content, new Sources.IdentityFormatterSourceMapping()); 116 callback(content, new Sources.IdentityFormatterSourceMapping());
124 } 117 }
125 }; 118 };
126 119
127 /** 120 /**
128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}}
129 */
130 Sources.FormatterMappingPayload;
131
132 /**
133 * @interface 121 * @interface
134 */ 122 */
135 Sources.FormatterSourceMapping = function() {}; 123 Sources.FormatterSourceMapping = function() {};
136 124
137 Sources.FormatterSourceMapping.prototype = { 125 Sources.FormatterSourceMapping.prototype = {
138 /** 126 /**
139 * @param {number} lineNumber 127 * @param {number} lineNumber
140 * @param {number=} columnNumber 128 * @param {number=} columnNumber
141 * @return {!Array.<number>} 129 * @return {!Array.<number>}
142 */ 130 */
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 }; 165 };
178 166
179 /** 167 /**
180 * @implements {Sources.FormatterSourceMapping} 168 * @implements {Sources.FormatterSourceMapping}
181 * @unrestricted 169 * @unrestricted
182 */ 170 */
183 Sources.FormatterSourceMappingImpl = class { 171 Sources.FormatterSourceMappingImpl = class {
184 /** 172 /**
185 * @param {!Array.<number>} originalLineEndings 173 * @param {!Array.<number>} originalLineEndings
186 * @param {!Array.<number>} formattedLineEndings 174 * @param {!Array.<number>} formattedLineEndings
187 * @param {!Sources.FormatterMappingPayload} mapping 175 * @param {!Common.FormatterWorkerPool.FormatMapping} mapping
188 */ 176 */
189 constructor(originalLineEndings, formattedLineEndings, mapping) { 177 constructor(originalLineEndings, formattedLineEndings, mapping) {
190 this._originalLineEndings = originalLineEndings; 178 this._originalLineEndings = originalLineEndings;
191 this._formattedLineEndings = formattedLineEndings; 179 this._formattedLineEndings = formattedLineEndings;
192 this._mapping = mapping; 180 this._mapping = mapping;
193 } 181 }
194 182
195 /** 183 /**
196 * @override 184 * @override
197 * @param {number} lineNumber 185 * @param {number} lineNumber
(...skipping 28 matching lines...) Expand all
226 * @return {number} 214 * @return {number}
227 */ 215 */
228 _convertPosition(positions1, positions2, position) { 216 _convertPosition(positions1, positions2, position) {
229 var index = positions1.upperBound(position) - 1; 217 var index = positions1.upperBound(position) - 1;
230 var convertedPosition = positions2[index] + position - positions1[index]; 218 var convertedPosition = positions2[index] + position - positions1[index];
231 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1]) 219 if (index < positions2.length - 1 && convertedPosition > positions2[index + 1])
232 convertedPosition = positions2[index + 1]; 220 convertedPosition = positions2[index + 1];
233 return convertedPosition; 221 return convertedPosition;
234 } 222 }
235 }; 223 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698