| 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 18 matching lines...) Expand all Loading... |
| 29 */ | 29 */ |
| 30 /** | 30 /** |
| 31 * @interface | 31 * @interface |
| 32 */ | 32 */ |
| 33 Sources.Formatter = function() {}; | 33 Sources.Formatter = function() {}; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * @param {!Common.ResourceType} contentType | 36 * @param {!Common.ResourceType} contentType |
| 37 * @param {string} mimeType | 37 * @param {string} mimeType |
| 38 * @param {string} content | 38 * @param {string} content |
| 39 * @param {function(string, !Sources.FormatterSourceMapping)} callback | 39 * @param {function(string, !Workspace.UISourceCode.SourceMapping)} callback |
| 40 */ | 40 */ |
| 41 Sources.Formatter.format = function(contentType, mimeType, content, callback) { | 41 Sources.Formatter.format = function(contentType, mimeType, content, callback) { |
| 42 if (contentType.isDocumentOrScriptOrStyleSheet()) | 42 if (contentType.isDocumentOrScriptOrStyleSheet()) |
| 43 new Sources.ScriptFormatter(mimeType, content, callback); | 43 new Sources.ScriptFormatter(mimeType, content, callback); |
| 44 else | 44 else |
| 45 new Sources.ScriptIdentityFormatter(mimeType, content, callback); | 45 new Sources.ScriptIdentityFormatter(mimeType, content, callback); |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 /** | 48 /** |
| 49 * @param {!Array.<number>} lineEndings | 49 * @param {!Array.<number>} lineEndings |
| (...skipping 21 matching lines...) Expand all Loading... |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 /** | 73 /** |
| 74 * @implements {Sources.Formatter} | 74 * @implements {Sources.Formatter} |
| 75 * @unrestricted | 75 * @unrestricted |
| 76 */ | 76 */ |
| 77 Sources.ScriptFormatter = class { | 77 Sources.ScriptFormatter = class { |
| 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, !Workspace.UISourceCode.SourceMapping)} 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 var parameters = { |
| 89 mimeType: mimeType, | 89 mimeType: mimeType, |
| 90 content: content, | 90 content: content, |
| 91 indentString: Common.moduleSetting('textEditorIndent').get() | 91 indentString: Common.moduleSetting('textEditorIndent').get() |
| 92 }; | 92 }; |
| 93 Common.formatterWorkerPool.runTask('format', parameters).then(this._didForma
tContent.bind(this)); | 93 Common.formatterWorkerPool.runTask('format', parameters).then(this._didForma
tContent.bind(this)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 /** | 96 /** |
| 97 * @param {?MessageEvent} event | 97 * @param {?MessageEvent} event |
| 98 */ | 98 */ |
| 99 _didFormatContent(event) { | 99 _didFormatContent(event) { |
| 100 var formattedContent = ''; | 100 var formattedContent = ''; |
| 101 var mapping = []; | 101 var mapping = []; |
| 102 if (event) { | 102 if (event) { |
| 103 formattedContent = event.data.content; | 103 formattedContent = event.data.content; |
| 104 mapping = event.data['mapping']; | 104 mapping = event.data['mapping']; |
| 105 } | 105 } |
| 106 var sourceMapping = new Sources.FormatterSourceMappingImpl( | 106 var sourceMapping = new Workspace.UISourceCode.SourceMappingImpl( |
| 107 this._originalContent.computeLineEndings(), formattedContent.computeLine
Endings(), mapping); | 107 this._originalContent.computeLineEndings(), formattedContent.computeLine
Endings(), mapping); |
| 108 this._callback(formattedContent, sourceMapping); | 108 this._callback(formattedContent, sourceMapping); |
| 109 } | 109 } |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 /** | 112 /** |
| 113 * @implements {Sources.Formatter} | 113 * @implements {Sources.Formatter} |
| 114 * @unrestricted | 114 * @unrestricted |
| 115 */ | 115 */ |
| 116 Sources.ScriptIdentityFormatter = class { | 116 Sources.ScriptIdentityFormatter = class { |
| 117 /** | 117 /** |
| 118 * @param {string} mimeType | 118 * @param {string} mimeType |
| 119 * @param {string} content | 119 * @param {string} content |
| 120 * @param {function(string, !Sources.FormatterSourceMapping)} callback | 120 * @param {function(string, !Workspace.UISourceCode.SourceMapping)} callback |
| 121 */ | 121 */ |
| 122 constructor(mimeType, content, callback) { | 122 constructor(mimeType, content, callback) { |
| 123 callback(content, new Sources.IdentityFormatterSourceMapping()); | 123 callback(content, new Sources.IdentityFormatterSourceMapping()); |
| 124 } | 124 } |
| 125 }; | 125 }; |
| 126 | 126 |
| 127 /** | 127 /** |
| 128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}} | 128 * @typedef {{original: !Array.<number>, formatted: !Array.<number>}} |
| 129 */ | 129 */ |
| 130 Sources.FormatterMappingPayload; | 130 Sources.FormatterMappingPayload; |
| 131 | 131 |
| 132 /** | 132 /** |
| 133 * @interface | 133 * @implements {Workspace.UISourceCode.SourceMapping} |
| 134 */ | |
| 135 Sources.FormatterSourceMapping = function() {}; | |
| 136 | |
| 137 Sources.FormatterSourceMapping.prototype = { | |
| 138 /** | |
| 139 * @param {number} lineNumber | |
| 140 * @param {number=} columnNumber | |
| 141 * @return {!Array.<number>} | |
| 142 */ | |
| 143 originalToFormatted: function(lineNumber, columnNumber) {}, | |
| 144 | |
| 145 /** | |
| 146 * @param {number} lineNumber | |
| 147 * @param {number=} columnNumber | |
| 148 * @return {!Array.<number>} | |
| 149 */ | |
| 150 formattedToOriginal: function(lineNumber, columnNumber) {} | |
| 151 }; | |
| 152 | |
| 153 /** | |
| 154 * @implements {Sources.FormatterSourceMapping} | |
| 155 * @unrestricted | 134 * @unrestricted |
| 156 */ | 135 */ |
| 157 Sources.IdentityFormatterSourceMapping = class { | 136 Sources.IdentityFormatterSourceMapping = class { |
| 158 /** | 137 /** |
| 159 * @override | 138 * @override |
| 160 * @param {number} lineNumber | 139 * @param {number} lineNumber |
| 161 * @param {number=} columnNumber | 140 * @param {number=} columnNumber |
| 162 * @return {!Array.<number>} | 141 * @return {!Array.<number>} |
| 163 */ | 142 */ |
| 164 originalToFormatted(lineNumber, columnNumber) { | 143 originalToFormatted(lineNumber, columnNumber) { |
| 165 return [lineNumber, columnNumber || 0]; | 144 return [lineNumber, columnNumber || 0]; |
| 166 } | 145 } |
| 167 | 146 |
| 168 /** | 147 /** |
| 169 * @override | 148 * @override |
| 170 * @param {number} lineNumber | 149 * @param {number} lineNumber |
| 171 * @param {number=} columnNumber | 150 * @param {number=} columnNumber |
| 172 * @return {!Array.<number>} | 151 * @return {!Array.<number>} |
| 173 */ | 152 */ |
| 174 formattedToOriginal(lineNumber, columnNumber) { | 153 formattedToOriginal(lineNumber, columnNumber) { |
| 175 return [lineNumber, columnNumber || 0]; | 154 return [lineNumber, columnNumber || 0]; |
| 176 } | 155 } |
| 177 }; | 156 }; |
| 178 | 157 |
| 179 /** | 158 /** |
| 180 * @implements {Sources.FormatterSourceMapping} | 159 * @implements {Workspace.UISourceCode.SourceMapping} |
| 181 * @unrestricted | 160 * @unrestricted |
| 182 */ | 161 */ |
| 183 Sources.FormatterSourceMappingImpl = class { | 162 Workspace.UISourceCode.SourceMappingImpl = class { |
| 184 /** | 163 /** |
| 185 * @param {!Array.<number>} originalLineEndings | 164 * @param {!Array.<number>} originalLineEndings |
| 186 * @param {!Array.<number>} formattedLineEndings | 165 * @param {!Array.<number>} formattedLineEndings |
| 187 * @param {!Sources.FormatterMappingPayload} mapping | 166 * @param {!Sources.FormatterMappingPayload} mapping |
| 188 */ | 167 */ |
| 189 constructor(originalLineEndings, formattedLineEndings, mapping) { | 168 constructor(originalLineEndings, formattedLineEndings, mapping) { |
| 190 this._originalLineEndings = originalLineEndings; | 169 this._originalLineEndings = originalLineEndings; |
| 191 this._formattedLineEndings = formattedLineEndings; | 170 this._formattedLineEndings = formattedLineEndings; |
| 192 this._mapping = mapping; | 171 this._mapping = mapping; |
| 193 } | 172 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 * @return {number} | 205 * @return {number} |
| 227 */ | 206 */ |
| 228 _convertPosition(positions1, positions2, position) { | 207 _convertPosition(positions1, positions2, position) { |
| 229 var index = positions1.upperBound(position) - 1; | 208 var index = positions1.upperBound(position) - 1; |
| 230 var convertedPosition = positions2[index] + position - positions1[index]; | 209 var convertedPosition = positions2[index] + position - positions1[index]; |
| 231 if (index < positions2.length - 1 && convertedPosition > positions2[index +
1]) | 210 if (index < positions2.length - 1 && convertedPosition > positions2[index +
1]) |
| 232 convertedPosition = positions2[index + 1]; | 211 convertedPosition = positions2[index + 1]; |
| 233 return convertedPosition; | 212 return convertedPosition; |
| 234 } | 213 } |
| 235 }; | 214 }; |
| OLD | NEW |