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 15 matching lines...) Expand all Loading... |
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 | 30 |
31 /** | 31 /** |
32 * @interface | 32 * @interface |
33 */ | 33 */ |
34 WebInspector.Formatter = function() | 34 WebInspector.Formatter = function() |
35 { | 35 { |
36 } | 36 }; |
37 | 37 |
38 /** | 38 /** |
39 * @param {!WebInspector.ResourceType} contentType | 39 * @param {!WebInspector.ResourceType} contentType |
40 * @param {string} mimeType | 40 * @param {string} mimeType |
41 * @param {string} content | 41 * @param {string} content |
42 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback | 42 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback |
43 */ | 43 */ |
44 WebInspector.Formatter.format = function(contentType, mimeType, content, callbac
k) | 44 WebInspector.Formatter.format = function(contentType, mimeType, content, callbac
k) |
45 { | 45 { |
46 if (contentType.isDocumentOrScriptOrStyleSheet()) | 46 if (contentType.isDocumentOrScriptOrStyleSheet()) |
47 new WebInspector.ScriptFormatter(mimeType, content, callback); | 47 new WebInspector.ScriptFormatter(mimeType, content, callback); |
48 else | 48 else |
49 new WebInspector.IdentityFormatter(mimeType, content, callback); | 49 new WebInspector.IdentityFormatter(mimeType, content, callback); |
50 } | 50 }; |
51 | 51 |
52 /** | 52 /** |
53 * @param {!Array.<number>} lineEndings | 53 * @param {!Array.<number>} lineEndings |
54 * @param {number} lineNumber | 54 * @param {number} lineNumber |
55 * @param {number} columnNumber | 55 * @param {number} columnNumber |
56 * @return {number} | 56 * @return {number} |
57 */ | 57 */ |
58 WebInspector.Formatter.locationToPosition = function(lineEndings, lineNumber, co
lumnNumber) | 58 WebInspector.Formatter.locationToPosition = function(lineEndings, lineNumber, co
lumnNumber) |
59 { | 59 { |
60 var position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0; | 60 var position = lineNumber ? lineEndings[lineNumber - 1] + 1 : 0; |
61 return position + columnNumber; | 61 return position + columnNumber; |
62 } | 62 }; |
63 | 63 |
64 /** | 64 /** |
65 * @param {!Array.<number>} lineEndings | 65 * @param {!Array.<number>} lineEndings |
66 * @param {number} position | 66 * @param {number} position |
67 * @return {!Array.<number>} | 67 * @return {!Array.<number>} |
68 */ | 68 */ |
69 WebInspector.Formatter.positionToLocation = function(lineEndings, position) | 69 WebInspector.Formatter.positionToLocation = function(lineEndings, position) |
70 { | 70 { |
71 var lineNumber = lineEndings.upperBound(position - 1); | 71 var lineNumber = lineEndings.upperBound(position - 1); |
72 if (!lineNumber) | 72 if (!lineNumber) |
73 var columnNumber = position; | 73 var columnNumber = position; |
74 else | 74 else |
75 var columnNumber = position - lineEndings[lineNumber - 1] - 1; | 75 var columnNumber = position - lineEndings[lineNumber - 1] - 1; |
76 return [lineNumber, columnNumber]; | 76 return [lineNumber, columnNumber]; |
77 } | 77 }; |
78 | 78 |
79 /** | 79 /** |
80 * @constructor | 80 * @constructor |
81 * @implements {WebInspector.Formatter} | 81 * @implements {WebInspector.Formatter} |
82 * @param {string} mimeType | 82 * @param {string} mimeType |
83 * @param {string} content | 83 * @param {string} content |
84 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback | 84 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback |
85 */ | 85 */ |
86 WebInspector.ScriptFormatter = function(mimeType, content, callback) | 86 WebInspector.ScriptFormatter = function(mimeType, content, callback) |
87 { | 87 { |
88 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/
, ""); | 88 content = content.replace(/\r\n?|[\n\u2028\u2029]/g, "\n").replace(/^\uFEFF/
, ""); |
89 this._callback = callback; | 89 this._callback = callback; |
90 this._originalContent = content; | 90 this._originalContent = content; |
91 | 91 |
92 var parameters = { | 92 var parameters = { |
93 mimeType: mimeType, | 93 mimeType: mimeType, |
94 content: content, | 94 content: content, |
95 indentString: WebInspector.moduleSetting("textEditorIndent").get() | 95 indentString: WebInspector.moduleSetting("textEditorIndent").get() |
96 }; | 96 }; |
97 WebInspector.formatterWorkerPool.runTask("format", parameters) | 97 WebInspector.formatterWorkerPool.runTask("format", parameters) |
98 .then(this._didFormatContent.bind(this)); | 98 .then(this._didFormatContent.bind(this)); |
99 } | 99 }; |
100 | 100 |
101 WebInspector.ScriptFormatter.prototype = { | 101 WebInspector.ScriptFormatter.prototype = { |
102 /** | 102 /** |
103 * @param {?MessageEvent} event | 103 * @param {?MessageEvent} event |
104 */ | 104 */ |
105 _didFormatContent: function(event) | 105 _didFormatContent: function(event) |
106 { | 106 { |
107 var formattedContent = ""; | 107 var formattedContent = ""; |
108 var mapping = []; | 108 var mapping = []; |
109 if (event) { | 109 if (event) { |
110 formattedContent = event.data.content; | 110 formattedContent = event.data.content; |
111 mapping = event.data["mapping"]; | 111 mapping = event.data["mapping"]; |
112 } | 112 } |
113 var sourceMapping = new WebInspector.FormatterSourceMappingImpl(this._or
iginalContent.computeLineEndings(), formattedContent.computeLineEndings(), mappi
ng); | 113 var sourceMapping = new WebInspector.FormatterSourceMappingImpl(this._or
iginalContent.computeLineEndings(), formattedContent.computeLineEndings(), mappi
ng); |
114 this._callback(formattedContent, sourceMapping); | 114 this._callback(formattedContent, sourceMapping); |
115 } | 115 } |
116 } | 116 }; |
117 | 117 |
118 /** | 118 /** |
119 * @constructor | 119 * @constructor |
120 * @implements {WebInspector.Formatter} | 120 * @implements {WebInspector.Formatter} |
121 * @param {string} mimeType | 121 * @param {string} mimeType |
122 * @param {string} content | 122 * @param {string} content |
123 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback | 123 * @param {function(string, !WebInspector.FormatterSourceMapping)} callback |
124 */ | 124 */ |
125 WebInspector.IdentityFormatter = function(mimeType, content, callback) | 125 WebInspector.IdentityFormatter = function(mimeType, content, callback) |
126 { | 126 { |
127 callback(content, new WebInspector.IdentityFormatterSourceMapping()); | 127 callback(content, new WebInspector.IdentityFormatterSourceMapping()); |
128 } | 128 }; |
129 | 129 |
130 /** | 130 /** |
131 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}} | 131 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}} |
132 */ | 132 */ |
133 WebInspector.FormatterMappingPayload; | 133 WebInspector.FormatterMappingPayload; |
134 | 134 |
135 /** | 135 /** |
136 * @interface | 136 * @interface |
137 */ | 137 */ |
138 WebInspector.FormatterSourceMapping = function() | 138 WebInspector.FormatterSourceMapping = function() |
139 { | 139 { |
140 } | 140 }; |
141 | 141 |
142 WebInspector.FormatterSourceMapping.prototype = { | 142 WebInspector.FormatterSourceMapping.prototype = { |
143 /** | 143 /** |
144 * @param {number} lineNumber | 144 * @param {number} lineNumber |
145 * @param {number=} columnNumber | 145 * @param {number=} columnNumber |
146 * @return {!Array.<number>} | 146 * @return {!Array.<number>} |
147 */ | 147 */ |
148 originalToFormatted: function(lineNumber, columnNumber) { }, | 148 originalToFormatted: function(lineNumber, columnNumber) { }, |
149 | 149 |
150 /** | 150 /** |
151 * @param {number} lineNumber | 151 * @param {number} lineNumber |
152 * @param {number=} columnNumber | 152 * @param {number=} columnNumber |
153 * @return {!Array.<number>} | 153 * @return {!Array.<number>} |
154 */ | 154 */ |
155 formattedToOriginal: function(lineNumber, columnNumber) { } | 155 formattedToOriginal: function(lineNumber, columnNumber) { } |
156 } | 156 }; |
157 | 157 |
158 /** | 158 /** |
159 * @constructor | 159 * @constructor |
160 * @implements {WebInspector.FormatterSourceMapping} | 160 * @implements {WebInspector.FormatterSourceMapping} |
161 */ | 161 */ |
162 WebInspector.IdentityFormatterSourceMapping = function() | 162 WebInspector.IdentityFormatterSourceMapping = function() |
163 { | 163 { |
164 } | 164 }; |
165 | 165 |
166 WebInspector.IdentityFormatterSourceMapping.prototype = { | 166 WebInspector.IdentityFormatterSourceMapping.prototype = { |
167 /** | 167 /** |
168 * @override | 168 * @override |
169 * @param {number} lineNumber | 169 * @param {number} lineNumber |
170 * @param {number=} columnNumber | 170 * @param {number=} columnNumber |
171 * @return {!Array.<number>} | 171 * @return {!Array.<number>} |
172 */ | 172 */ |
173 originalToFormatted: function(lineNumber, columnNumber) | 173 originalToFormatted: function(lineNumber, columnNumber) |
174 { | 174 { |
175 return [lineNumber, columnNumber || 0]; | 175 return [lineNumber, columnNumber || 0]; |
176 }, | 176 }, |
177 | 177 |
178 /** | 178 /** |
179 * @override | 179 * @override |
180 * @param {number} lineNumber | 180 * @param {number} lineNumber |
181 * @param {number=} columnNumber | 181 * @param {number=} columnNumber |
182 * @return {!Array.<number>} | 182 * @return {!Array.<number>} |
183 */ | 183 */ |
184 formattedToOriginal: function(lineNumber, columnNumber) | 184 formattedToOriginal: function(lineNumber, columnNumber) |
185 { | 185 { |
186 return [lineNumber, columnNumber || 0]; | 186 return [lineNumber, columnNumber || 0]; |
187 } | 187 } |
188 } | 188 }; |
189 | 189 |
190 /** | 190 /** |
191 * @constructor | 191 * @constructor |
192 * @implements {WebInspector.FormatterSourceMapping} | 192 * @implements {WebInspector.FormatterSourceMapping} |
193 * @param {!Array.<number>} originalLineEndings | 193 * @param {!Array.<number>} originalLineEndings |
194 * @param {!Array.<number>} formattedLineEndings | 194 * @param {!Array.<number>} formattedLineEndings |
195 * @param {!WebInspector.FormatterMappingPayload} mapping | 195 * @param {!WebInspector.FormatterMappingPayload} mapping |
196 */ | 196 */ |
197 WebInspector.FormatterSourceMappingImpl = function(originalLineEndings, formatte
dLineEndings, mapping) | 197 WebInspector.FormatterSourceMappingImpl = function(originalLineEndings, formatte
dLineEndings, mapping) |
198 { | 198 { |
199 this._originalLineEndings = originalLineEndings; | 199 this._originalLineEndings = originalLineEndings; |
200 this._formattedLineEndings = formattedLineEndings; | 200 this._formattedLineEndings = formattedLineEndings; |
201 this._mapping = mapping; | 201 this._mapping = mapping; |
202 } | 202 }; |
203 | 203 |
204 WebInspector.FormatterSourceMappingImpl.prototype = { | 204 WebInspector.FormatterSourceMappingImpl.prototype = { |
205 /** | 205 /** |
206 * @override | 206 * @override |
207 * @param {number} lineNumber | 207 * @param {number} lineNumber |
208 * @param {number=} columnNumber | 208 * @param {number=} columnNumber |
209 * @return {!Array.<number>} | 209 * @return {!Array.<number>} |
210 */ | 210 */ |
211 originalToFormatted: function(lineNumber, columnNumber) | 211 originalToFormatted: function(lineNumber, columnNumber) |
212 { | 212 { |
(...skipping 22 matching lines...) Expand all Loading... |
235 * @return {number} | 235 * @return {number} |
236 */ | 236 */ |
237 _convertPosition: function(positions1, positions2, position) | 237 _convertPosition: function(positions1, positions2, position) |
238 { | 238 { |
239 var index = positions1.upperBound(position) - 1; | 239 var index = positions1.upperBound(position) - 1; |
240 var convertedPosition = positions2[index] + position - positions1[index]
; | 240 var convertedPosition = positions2[index] + position - positions1[index]
; |
241 if (index < positions2.length - 1 && convertedPosition > positions2[inde
x + 1]) | 241 if (index < positions2.length - 1 && convertedPosition > positions2[inde
x + 1]) |
242 convertedPosition = positions2[index + 1]; | 242 convertedPosition = positions2[index + 1]; |
243 return convertedPosition; | 243 return convertedPosition; |
244 } | 244 } |
245 } | 245 }; |
OLD | NEW |