OLD | NEW |
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 Loading... |
39 | 39 |
40 WebInspector.JavaScriptFormatter.prototype = { | 40 WebInspector.JavaScriptFormatter.prototype = { |
41 /** | 41 /** |
42 * @param {string} text | 42 * @param {string} text |
43 * @param {!Array<number>} lineEndings | 43 * @param {!Array<number>} lineEndings |
44 * @param {number} fromOffset | 44 * @param {number} fromOffset |
45 * @param {number} toOffset | 45 * @param {number} toOffset |
46 */ | 46 */ |
47 format: function(text, lineEndings, fromOffset, toOffset) | 47 format: function(text, lineEndings, fromOffset, toOffset) |
48 { | 48 { |
49 this._lineOffset = lineEndings.lowerBound(fromOffset); | |
50 this._fromOffset = fromOffset; | 49 this._fromOffset = fromOffset; |
51 this._toOffset = toOffset; | 50 this._toOffset = toOffset; |
52 this._content = text.substring(this._fromOffset, this._toOffset); | 51 this._content = text.substring(this._fromOffset, this._toOffset); |
53 this._tokenizer = new WebInspector.AcornTokenizer(this._content); | 52 this._tokenizer = new WebInspector.AcornTokenizer(this._content); |
54 var ast = acorn.parse(this._content, { ranges: false, ecmaVersion: 6 }); | 53 var ast = acorn.parse(this._content, { ranges: false, ecmaVersion: 6 }); |
55 var walker = new WebInspector.ESTreeWalker(this._beforeVisit.bind(this),
this._afterVisit.bind(this)); | 54 var walker = new WebInspector.ESTreeWalker(this._beforeVisit.bind(this),
this._afterVisit.bind(this)); |
56 walker.walk(ast); | 55 walker.walk(ast); |
57 }, | 56 }, |
58 | 57 |
59 /** | 58 /** |
60 * @param {?Acorn.TokenOrComment} token | 59 * @param {?Acorn.TokenOrComment} token |
61 * @param {string} format | 60 * @param {string} format |
62 */ | 61 */ |
63 _push: function(token, format) | 62 _push: function(token, format) |
64 { | 63 { |
65 for (var i = 0; i < format.length; ++i) { | 64 for (var i = 0; i < format.length; ++i) { |
66 if (format[i] === "s") | 65 if (format[i] === "s") |
67 this._builder.addSoftSpace(); | 66 this._builder.addSoftSpace(); |
68 else if (format[i] === "S") | 67 else if (format[i] === "S") |
69 this._builder.addHardSpace(); | 68 this._builder.addHardSpace(); |
70 else if (format[i] === "n") | 69 else if (format[i] === "n") |
71 this._builder.addNewLine(); | 70 this._builder.addNewLine(); |
72 else if (format[i] === ">") | 71 else if (format[i] === ">") |
73 this._builder.increaseNestingLevel(); | 72 this._builder.increaseNestingLevel(); |
74 else if (format[i] === "<") | 73 else if (format[i] === "<") |
75 this._builder.decreaseNestingLevel(); | 74 this._builder.decreaseNestingLevel(); |
76 else if (format[i] === "t") | 75 else if (format[i] === "t") |
77 this._builder.addToken(this._content.substring(token.start, toke
n.end), this._fromOffset + token.start, this._lineOffset + this._tokenizer.token
LineStart(), this._lineOffset + this._tokenizer.tokenLineEnd()); | 76 this._builder.addToken(this._content.substring(token.start, toke
n.end), this._fromOffset + token.start); |
78 } | 77 } |
79 }, | 78 }, |
80 | 79 |
81 /** | 80 /** |
82 * @param {!ESTree.Node} node | 81 * @param {!ESTree.Node} node |
83 */ | 82 */ |
84 _beforeVisit: function(node) | 83 _beforeVisit: function(node) |
85 { | 84 { |
86 if (!node.parent) | 85 if (!node.parent) |
87 return; | 86 return; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 * @param {!ESTree.Node} node | 124 * @param {!ESTree.Node} node |
126 * @param {!Acorn.TokenOrComment} token | 125 * @param {!Acorn.TokenOrComment} token |
127 * @return {string} | 126 * @return {string} |
128 */ | 127 */ |
129 _formatToken: function(node, token) | 128 _formatToken: function(node, token) |
130 { | 129 { |
131 var AT = WebInspector.AcornTokenizer; | 130 var AT = WebInspector.AcornTokenizer; |
132 if (AT.lineComment(token)) | 131 if (AT.lineComment(token)) |
133 return "tn"; | 132 return "tn"; |
134 if (AT.blockComment(token)) | 133 if (AT.blockComment(token)) |
135 return "t"; | 134 return "tn"; |
136 if (node.type === "ContinueStatement" || node.type === "BreakStatement")
{ | 135 if (node.type === "ContinueStatement" || node.type === "BreakStatement")
{ |
137 return node.label && AT.keyword(token) ? "ts" : "t"; | 136 return node.label && AT.keyword(token) ? "ts" : "t"; |
138 } else if (node.type === "Identifier") { | 137 } else if (node.type === "Identifier") { |
139 return "t"; | 138 return "t"; |
140 } else if (node.type === "ReturnStatement") { | 139 } else if (node.type === "ReturnStatement") { |
141 if (AT.punctuator(token, ";")) | 140 if (AT.punctuator(token, ";")) |
142 return "t"; | 141 return "t"; |
143 return node.argument ? "ts" : "t"; | 142 return node.argument ? "ts" : "t"; |
144 } else if (node.type === "Property") { | 143 } else if (node.type === "Property") { |
145 if (AT.punctuator(token, ":")) | 144 if (AT.punctuator(token, ":")) |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 if (node.consequent.type !== "BlockStatement") | 297 if (node.consequent.type !== "BlockStatement") |
299 return "<"; | 298 return "<"; |
300 } | 299 } |
301 } else if (node.type === "BreakStatement" || node.type === "ThrowStateme
nt" || node.type === "ReturnStatement" || node.type === "ExpressionStatement") { | 300 } else if (node.type === "BreakStatement" || node.type === "ThrowStateme
nt" || node.type === "ReturnStatement" || node.type === "ExpressionStatement") { |
302 return "n"; | 301 return "n"; |
303 } | 302 } |
304 return ""; | 303 return ""; |
305 } | 304 } |
306 } | 305 } |
307 | 306 |
OLD | NEW |