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

Side by Side Diff: Source/devtools/front_end/ScriptFormatterWorker.js

Issue 22638008: DevTools: Use CodeMirror modes instead of highlight tokenizers (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: address comments Created 7 years, 4 months 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 | Annotate | Revision Log
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 importScripts("utilities.js");
31 importScripts("cm/headlesscodemirror.js");
32 importScripts("cm/css.js");
33 importScripts("cm/javascript.js");
34 importScripts("cm/xml.js");
35 importScripts("cm/htmlmixed.js");
36 WebInspector = {};
37 importScripts("CodeMirrorUtils.js");
30 38
31 onmessage = function(event) { 39 onmessage = function(event) {
32 if (!event.data.method) 40 if (!event.data.method)
33 return; 41 return;
34 42
35 self[event.data.method](event.data.params); 43 self[event.data.method](event.data.params);
36 }; 44 };
37 45
38 function format(params) 46 function format(params)
39 { 47 {
(...skipping 30 matching lines...) Expand all
70 var outlineChunk = []; 78 var outlineChunk = [];
71 var previousIdentifier = null; 79 var previousIdentifier = null;
72 var previousToken = null; 80 var previousToken = null;
73 var previousTokenType = null; 81 var previousTokenType = null;
74 var currentChunk = 1; 82 var currentChunk = 1;
75 var processedChunkCharacters = 0; 83 var processedChunkCharacters = 0;
76 var addedFunction = false; 84 var addedFunction = false;
77 var isReadingArguments = false; 85 var isReadingArguments = false;
78 var argumentsText = ""; 86 var argumentsText = "";
79 var currentFunction = null; 87 var currentFunction = null;
80 var scriptTokenizer = new WebInspector.SourceJavaScriptTokenizer(); 88 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/javascrip t");
81 scriptTokenizer.condition = scriptTokenizer.createInitialCondition();
82
83 for (var i = 0; i < lines.length; ++i) { 89 for (var i = 0; i < lines.length; ++i) {
84 var line = lines[i]; 90 var line = lines[i];
85 var column = 0; 91 tokenizer(line, function(tokenValue, tokenType, column, newColumn) {
vsevik 2013/08/14 16:26:41 extract
86 scriptTokenizer.line = line; 92 tokenType = tokenType ? WebInspector.CodeMirrorUtils.convertTokenTyp e(tokenType) : null;
87 do {
88 var newColumn = scriptTokenizer.nextToken(column);
89 var tokenType = scriptTokenizer.tokenType;
90 var tokenValue = line.substring(column, newColumn);
91 if (tokenType === "javascript-ident") { 93 if (tokenType === "javascript-ident") {
92 previousIdentifier = tokenValue; 94 previousIdentifier = tokenValue;
93 if (tokenValue && previousToken === "function") { 95 if (tokenValue && previousToken === "function") {
94 // A named function: "function f...". 96 // A named function: "function f...".
95 currentFunction = { line: i, column: column, name: tokenValu e }; 97 currentFunction = { line: i, column: column, name: tokenValu e };
96 addedFunction = true; 98 addedFunction = true;
97 previousIdentifier = null; 99 previousIdentifier = null;
98 } 100 }
99 } else if (tokenType === "javascript-keyword") { 101 } else if (tokenType === "javascript-keyword") {
100 if (tokenValue === "function") { 102 if (tokenValue === "function") {
(...skipping 19 matching lines...) Expand all
120 argumentsText = ""; 122 argumentsText = "";
121 outlineChunk.push(currentFunction); 123 outlineChunk.push(currentFunction);
122 } 124 }
123 125
124 if (tokenValue.trim().length) { 126 if (tokenValue.trim().length) {
125 // Skip whitespace tokens. 127 // Skip whitespace tokens.
126 previousToken = tokenValue; 128 previousToken = tokenValue;
127 previousTokenType = tokenType; 129 previousTokenType = tokenType;
128 } 130 }
129 processedChunkCharacters += newColumn - column; 131 processedChunkCharacters += newColumn - column;
130 column = newColumn;
131 132
132 if (processedChunkCharacters >= chunkSize) { 133 if (processedChunkCharacters >= chunkSize) {
133 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur rentChunk++ }); 134 postMessage({ chunk: outlineChunk, total: chunkCount, index: cur rentChunk++ });
134 outlineChunk = []; 135 outlineChunk = [];
135 processedChunkCharacters = 0; 136 processedChunkCharacters = 0;
136 } 137 }
137 } while (column < line.length); 138 });
138 } 139 }
139 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount }); 140 postMessage({ chunk: outlineChunk, total: chunkCount, index: chunkCount });
140 } 141 }
141 142
142 function formatScript(content, mapping, offset, formattedOffset, indentString) 143 function formatScript(content, mapping, offset, formattedOffset, indentString)
143 { 144 {
144 var formattedContent; 145 var formattedContent;
145 try { 146 try {
146 var tokenizer = new Tokenizer(content); 147 var tokenizer = new Tokenizer(content);
147 var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString); 148 var builder = new FormattedContentBuilder(tokenizer.content(), mapping, offset, formattedOffset, indentString);
148 var formatter = new JavaScriptFormatter(tokenizer, builder); 149 var formatter = new JavaScriptFormatter(tokenizer, builder);
149 formatter.format(); 150 formatter.format();
150 formattedContent = builder.content(); 151 formattedContent = builder.content();
151 } catch (e) { 152 } catch (e) {
152 formattedContent = content; 153 formattedContent = content;
153 } 154 }
154 return formattedContent; 155 return formattedContent;
155 } 156 }
156 157
157 WebInspector = {};
158
159 Array.prototype.keySet = function() 158 Array.prototype.keySet = function()
160 { 159 {
161 var keys = {}; 160 var keys = {};
162 for (var i = 0; i < this.length; ++i) 161 for (var i = 0; i < this.length; ++i)
163 keys[this[i]] = true; 162 keys[this[i]] = true;
164 return keys; 163 return keys;
165 }; 164 };
166 165
167 importScripts("SourceTokenizer.js");
168 importScripts("SourceHTMLTokenizer.js");
169 importScripts("SourceJavaScriptTokenizer.js");
170
171 HTMLScriptFormatter = function(indentString) 166 HTMLScriptFormatter = function(indentString)
172 { 167 {
173 WebInspector.SourceHTMLTokenizer.call(this);
174 this._indentString = indentString; 168 this._indentString = indentString;
175 } 169 }
176 170
177 HTMLScriptFormatter.prototype = { 171 HTMLScriptFormatter.prototype = {
178 format: function(content) 172 format: function(content)
179 { 173 {
180 this.line = content; 174 this.line = content;
181 this._content = content; 175 this._content = content;
182 this._formattedContent = ""; 176 this._formattedContent = "";
183 this._mapping = { original: [0], formatted: [0] }; 177 this._mapping = { original: [0], formatted: [0] };
184 this._position = 0; 178 this._position = 0;
185 179
186 var cursor = 0; 180 var scriptOpened = false;
187 while (cursor < this._content.length) 181 var tokenizer = WebInspector.CodeMirrorUtils.createTokenizer("text/html" );
188 cursor = this.nextToken(cursor); 182 tokenizer(content, function(tokenValue, tokenType, tokenStart, tokenEnd) {
vsevik 2013/08/14 16:26:41 please extract function, { on the next line
183 if (tokenValue === "<script" && tokenType === "xml-tag") {
184 scriptOpened = true;
185 } else if (scriptOpened && tokenValue === ">" && tokenType === "xml- tag") {
186 scriptOpened = false;
187 this.scriptStarted(tokenEnd);
188 } else if (tokenValue === "</script" && tokenType === "xml-tag") {
189 this.scriptEnded(tokenStart);
190 }
191 }.bind(this));
189 192
190 this._formattedContent += this._content.substring(this._position); 193 this._formattedContent += this._content.substring(this._position);
191 return { content: this._formattedContent, mapping: this._mapping }; 194 return { content: this._formattedContent, mapping: this._mapping };
192 }, 195 },
193 196
194 scriptStarted: function(cursor) 197 scriptStarted: function(cursor)
195 { 198 {
196 this._formattedContent += this._content.substring(this._position, cursor ); 199 this._formattedContent += this._content.substring(this._position, cursor );
197 this._formattedContent += "\n"; 200 this._formattedContent += "\n";
198 this._position = cursor; 201 this._position = cursor;
199 }, 202 },
200 203
201 scriptEnded: function(cursor) 204 scriptEnded: function(cursor)
202 { 205 {
203 if (cursor === this._position) 206 if (cursor === this._position)
204 return; 207 return;
205 208
206 var scriptContent = this._content.substring(this._position, cursor); 209 var scriptContent = this._content.substring(this._position, cursor);
207 this._mapping.original.push(this._position); 210 this._mapping.original.push(this._position);
208 this._mapping.formatted.push(this._formattedContent.length); 211 this._mapping.formatted.push(this._formattedContent.length);
209 var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length, this._indentString); 212 var formattedScriptContent = formatScript(scriptContent, this._mapping, this._position, this._formattedContent.length, this._indentString);
210 213
211 this._formattedContent += formattedScriptContent; 214 this._formattedContent += formattedScriptContent;
212 this._position = cursor; 215 this._position = cursor;
213 }, 216 },
214
215 styleSheetStarted: function(cursor)
216 {
217 },
218
219 styleSheetEnded: function(cursor)
220 {
221 },
222
223 __proto__: WebInspector.SourceHTMLTokenizer.prototype
224 } 217 }
225 218
226 function require() 219 function require()
227 { 220 {
228 return parse; 221 return parse;
229 } 222 }
230 223
231 var exports = {}; 224 var exports = {};
232 importScripts("UglifyJS/parse-js.js"); 225 importScripts("UglifyJS/parse-js.js");
233 var parse = exports; 226 var parse = exports;
234 227
235 importScripts("JavaScriptFormatter.js"); 228 importScripts("JavaScriptFormatter.js");
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698