OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @constructor | 6 * @constructor |
7 * @param {string} indentString | 7 * @param {string} indentString |
8 */ | 8 */ |
9 WebInspector.FormattedContentBuilder = function(indentString) | 9 WebInspector.FormattedContentBuilder = function(indentString) |
10 { | 10 { |
11 this._lastOriginalPosition = 0; | 11 this._lastOriginalPosition = 0; |
12 | 12 |
13 this._formattedContent = []; | 13 this._formattedContent = []; |
14 this._formattedContentLength = 0; | 14 this._formattedContentLength = 0; |
15 this._lastFormattedPosition = 0; | 15 this._lastFormattedPosition = 0; |
16 | 16 |
17 /** @type {!{original: !Array.<number>, formatted: !Array.<number>}} */ | 17 /** @type {!{original: !Array.<number>, formatted: !Array.<number>}} */ |
18 this._mapping = { original: [0], formatted: [0] }; | 18 this._mapping = { original: [0], formatted: [0] }; |
19 | 19 |
20 this._lineNumber = 0; | |
21 this._nestingLevel = 0; | 20 this._nestingLevel = 0; |
22 this._indentString = indentString; | 21 this._indentString = indentString; |
23 /** @type {!Map<number, string>} */ | 22 /** @type {!Map<number, string>} */ |
24 this._cachedIndents = new Map(); | 23 this._cachedIndents = new Map(); |
25 | 24 |
26 this._newLines = 0; | 25 this._newLines = 0; |
27 this._softSpace = false; | 26 this._softSpace = false; |
28 this._hardSpaces = 0; | 27 this._hardSpaces = 0; |
29 } | 28 } |
30 | 29 |
31 WebInspector.FormattedContentBuilder.prototype = { | 30 WebInspector.FormattedContentBuilder.prototype = { |
32 /** | 31 /** |
33 * @param {string} token | 32 * @param {string} token |
34 * @param {number} startPosition | 33 * @param {number} offset |
35 * @param {number} startLine | |
36 * @param {number} endLine | |
37 */ | 34 */ |
38 addToken: function(token, startPosition, startLine, endLine) | 35 addToken: function(token, offset) |
39 { | 36 { |
40 if (this._lineNumber < startLine) | |
41 this._newLines = Math.max(this._newLines, startLine - this._lineNumb
er); | |
42 | |
43 var last = this._formattedContent.peekLast(); | 37 var last = this._formattedContent.peekLast(); |
44 if (last && /\w/.test(last[last.length - 1]) && /\w/.test(token)) | 38 if (last && /\w/.test(last[last.length - 1]) && /\w/.test(token)) |
45 this.addSoftSpace(); | 39 this.addSoftSpace(); |
46 | 40 |
47 this._appendFormatting(); | 41 this._appendFormatting(); |
48 | 42 |
49 // Insert token. | 43 // Insert token. |
50 this._addMappingIfNeeded(startPosition); | 44 this._addMappingIfNeeded(offset); |
51 this._addText(token); | 45 this._addText(token); |
52 this._lineNumber = endLine; | |
53 }, | 46 }, |
54 | 47 |
55 addSoftSpace: function() | 48 addSoftSpace: function() |
56 { | 49 { |
57 if (!this._hardSpaces) | 50 if (!this._hardSpaces) |
58 this._softSpace = true; | 51 this._softSpace = true; |
59 }, | 52 }, |
60 | 53 |
61 addHardSpace: function() | 54 addHardSpace: function() |
62 { | 55 { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 { | 148 { |
156 if (originalPosition - this._lastOriginalPosition === this._formattedCon
tentLength - this._lastFormattedPosition) | 149 if (originalPosition - this._lastOriginalPosition === this._formattedCon
tentLength - this._lastFormattedPosition) |
157 return; | 150 return; |
158 this._mapping.original.push(originalPosition); | 151 this._mapping.original.push(originalPosition); |
159 this._lastOriginalPosition = originalPosition; | 152 this._lastOriginalPosition = originalPosition; |
160 this._mapping.formatted.push(this._formattedContentLength); | 153 this._mapping.formatted.push(this._formattedContentLength); |
161 this._lastFormattedPosition = this._formattedContentLength; | 154 this._lastFormattedPosition = this._formattedContentLength; |
162 } | 155 } |
163 } | 156 } |
164 | 157 |
OLD | NEW |