OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 if (!token) { | 83 if (!token) { |
84 if (selection.startColumn > 0) | 84 if (selection.startColumn > 0) |
85 token = this.textEditor.tokenAtTextPosition(selection.startLine, selecti
on.startColumn - 1); | 85 token = this.textEditor.tokenAtTextPosition(selection.startLine, selecti
on.startColumn - 1); |
86 if (!token) | 86 if (!token) |
87 return false; | 87 return false; |
88 } | 88 } |
89 if (token.type !== 'css-number') | 89 if (token.type !== 'css-number') |
90 return false; | 90 return false; |
91 | 91 |
92 var cssUnitRange = | 92 var cssUnitRange = |
93 new Common.TextRange(selection.startLine, token.startColumn, selection.s
tartLine, token.endColumn); | 93 new TextUtils.TextRange(selection.startLine, token.startColumn, selectio
n.startLine, token.endColumn); |
94 var cssUnitText = this.textEditor.text(cssUnitRange); | 94 var cssUnitText = this.textEditor.text(cssUnitRange); |
95 var newUnitText = this._modifyUnit(cssUnitText, change); | 95 var newUnitText = this._modifyUnit(cssUnitText, change); |
96 if (!newUnitText) | 96 if (!newUnitText) |
97 return false; | 97 return false; |
98 this.textEditor.editRange(cssUnitRange, newUnitText); | 98 this.textEditor.editRange(cssUnitRange, newUnitText); |
99 selection.startColumn = token.startColumn; | 99 selection.startColumn = token.startColumn; |
100 selection.endColumn = selection.startColumn + newUnitText.length; | 100 selection.endColumn = selection.startColumn + newUnitText.length; |
101 this.textEditor.setSelection(selection); | 101 this.textEditor.setSelection(selection); |
102 return true; | 102 return true; |
103 } | 103 } |
104 | 104 |
105 /** | 105 /** |
106 * @param {number} startLine | 106 * @param {number} startLine |
107 * @param {number} endLine | 107 * @param {number} endLine |
108 */ | 108 */ |
109 _updateSwatches(startLine, endLine) { | 109 _updateSwatches(startLine, endLine) { |
110 var swatches = []; | 110 var swatches = []; |
111 var swatchPositions = []; | 111 var swatchPositions = []; |
112 | 112 |
113 var regexes = | 113 var regexes = |
114 [SDK.CSSMetadata.VariableRegex, SDK.CSSMetadata.URLRegex, UI.Geometry.Cu
bicBezier.Regex, Common.Color.Regex]; | 114 [SDK.CSSMetadata.VariableRegex, SDK.CSSMetadata.URLRegex, UI.Geometry.Cu
bicBezier.Regex, Common.Color.Regex]; |
115 var handlers = new Map(); | 115 var handlers = new Map(); |
116 handlers.set(Common.Color.Regex, this._createColorSwatch.bind(this)); | 116 handlers.set(Common.Color.Regex, this._createColorSwatch.bind(this)); |
117 handlers.set(UI.Geometry.CubicBezier.Regex, this._createBezierSwatch.bind(th
is)); | 117 handlers.set(UI.Geometry.CubicBezier.Regex, this._createBezierSwatch.bind(th
is)); |
118 | 118 |
119 for (var lineNumber = startLine; lineNumber <= endLine; lineNumber++) { | 119 for (var lineNumber = startLine; lineNumber <= endLine; lineNumber++) { |
120 var line = this.textEditor.line(lineNumber).substring(0, Sources.CSSSource
Frame.maxSwatchProcessingLength); | 120 var line = this.textEditor.line(lineNumber).substring(0, Sources.CSSSource
Frame.maxSwatchProcessingLength); |
121 var results = Common.TextUtils.splitStringByRegexes(line, regexes); | 121 var results = TextUtils.TextUtils.splitStringByRegexes(line, regexes); |
122 for (var i = 0; i < results.length; i++) { | 122 for (var i = 0; i < results.length; i++) { |
123 var result = results[i]; | 123 var result = results[i]; |
124 if (result.regexIndex === -1 || !handlers.has(regexes[result.regexIndex]
)) | 124 if (result.regexIndex === -1 || !handlers.has(regexes[result.regexIndex]
)) |
125 continue; | 125 continue; |
126 var delimiters = /[\s:;,(){}]/; | 126 var delimiters = /[\s:;,(){}]/; |
127 var positionBefore = result.position - 1; | 127 var positionBefore = result.position - 1; |
128 var positionAfter = result.position + result.value.length; | 128 var positionAfter = result.position + result.value.length; |
129 if (positionBefore >= 0 && !delimiters.test(line.charAt(positionBefore))
|| | 129 if (positionBefore >= 0 && !delimiters.test(line.charAt(positionBefore))
|| |
130 positionAfter < line.length && !delimiters.test(line.charAt(position
After))) | 130 positionAfter < line.length && !delimiters.test(line.charAt(position
After))) |
131 continue; | 131 continue; |
132 var swatch = handlers.get(regexes[result.regexIndex])(result.value); | 132 var swatch = handlers.get(regexes[result.regexIndex])(result.value); |
133 if (!swatch) | 133 if (!swatch) |
134 continue; | 134 continue; |
135 swatches.push(swatch); | 135 swatches.push(swatch); |
136 swatchPositions.push(Common.TextRange.createFromLocation(lineNumber, res
ult.position)); | 136 swatchPositions.push(TextUtils.TextRange.createFromLocation(lineNumber,
result.position)); |
137 } | 137 } |
138 } | 138 } |
139 this.textEditor.operation(putSwatchesInline.bind(this)); | 139 this.textEditor.operation(putSwatchesInline.bind(this)); |
140 | 140 |
141 /** | 141 /** |
142 * @this {Sources.CSSSourceFrame} | 142 * @this {Sources.CSSSourceFrame} |
143 */ | 143 */ |
144 function putSwatchesInline() { | 144 function putSwatchesInline() { |
145 var clearRange = new Common.TextRange(startLine, 0, endLine, this.textEdit
or.line(endLine).length); | 145 var clearRange = new TextUtils.TextRange(startLine, 0, endLine, this.textE
ditor.line(endLine).length); |
146 this.textEditor.bookmarks(clearRange, Sources.CSSSourceFrame.SwatchBookmar
k).forEach(marker => marker.clear()); | 146 this.textEditor.bookmarks(clearRange, Sources.CSSSourceFrame.SwatchBookmar
k).forEach(marker => marker.clear()); |
147 | 147 |
148 for (var i = 0; i < swatches.length; i++) { | 148 for (var i = 0; i < swatches.length; i++) { |
149 var swatch = swatches[i]; | 149 var swatch = swatches[i]; |
150 var swatchPosition = swatchPositions[i]; | 150 var swatchPosition = swatchPositions[i]; |
151 var bookmark = this.textEditor.addBookmark( | 151 var bookmark = this.textEditor.addBookmark( |
152 swatchPosition.startLine, swatchPosition.startColumn, swatch, Source
s.CSSSourceFrame.SwatchBookmark); | 152 swatchPosition.startLine, swatchPosition.startColumn, swatch, Source
s.CSSSourceFrame.SwatchBookmark); |
153 swatch[Sources.CSSSourceFrame.SwatchBookmark] = bookmark; | 153 swatch[Sources.CSSSourceFrame.SwatchBookmark] = bookmark; |
154 } | 154 } |
155 } | 155 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 * @override | 286 * @override |
287 */ | 287 */ |
288 onTextEditorContentSet() { | 288 onTextEditorContentSet() { |
289 super.onTextEditorContentSet(); | 289 super.onTextEditorContentSet(); |
290 if (!this._muteSwatchProcessing) | 290 if (!this._muteSwatchProcessing) |
291 this._updateSwatches(0, this.textEditor.linesCount - 1); | 291 this._updateSwatches(0, this.textEditor.linesCount - 1); |
292 } | 292 } |
293 | 293 |
294 /** | 294 /** |
295 * @override | 295 * @override |
296 * @param {!Common.TextRange} oldRange | 296 * @param {!TextUtils.TextRange} oldRange |
297 * @param {!Common.TextRange} newRange | 297 * @param {!TextUtils.TextRange} newRange |
298 */ | 298 */ |
299 onTextChanged(oldRange, newRange) { | 299 onTextChanged(oldRange, newRange) { |
300 super.onTextChanged(oldRange, newRange); | 300 super.onTextChanged(oldRange, newRange); |
301 if (!this._muteSwatchProcessing) | 301 if (!this._muteSwatchProcessing) |
302 this._updateSwatches(newRange.startLine, newRange.endLine); | 302 this._updateSwatches(newRange.startLine, newRange.endLine); |
303 } | 303 } |
304 | 304 |
305 /** | 305 /** |
306 * @param {string} char | 306 * @param {string} char |
307 * @return {boolean} | 307 * @return {boolean} |
308 */ | 308 */ |
309 _isWordChar(char) { | 309 _isWordChar(char) { |
310 return Common.TextUtils.isWordChar(char) || char === '.' || char === '-' ||
char === '$'; | 310 return TextUtils.TextUtils.isWordChar(char) || char === '.' || char === '-'
|| char === '$'; |
311 } | 311 } |
312 | 312 |
313 /** | 313 /** |
314 * @param {!Common.TextRange} prefixRange | 314 * @param {!TextUtils.TextRange} prefixRange |
315 * @param {!Common.TextRange} substituteRange | 315 * @param {!TextUtils.TextRange} substituteRange |
316 * @return {?Promise.<!UI.SuggestBox.Suggestions>} | 316 * @return {?Promise.<!UI.SuggestBox.Suggestions>} |
317 */ | 317 */ |
318 _cssSuggestions(prefixRange, substituteRange) { | 318 _cssSuggestions(prefixRange, substituteRange) { |
319 var prefix = this.textEditor.text(prefixRange); | 319 var prefix = this.textEditor.text(prefixRange); |
320 if (prefix.startsWith('$')) | 320 if (prefix.startsWith('$')) |
321 return null; | 321 return null; |
322 | 322 |
323 var propertyToken = this._backtrackPropertyToken(prefixRange.startLine, pref
ixRange.startColumn - 1); | 323 var propertyToken = this._backtrackPropertyToken(prefixRange.startLine, pref
ixRange.startColumn - 1); |
324 if (!propertyToken) | 324 if (!propertyToken) |
325 return null; | 325 return null; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 tokenPosition = token.startColumn - 1; | 359 tokenPosition = token.startColumn - 1; |
360 } | 360 } |
361 return null; | 361 return null; |
362 } | 362 } |
363 }; | 363 }; |
364 | 364 |
365 /** @type {number} */ | 365 /** @type {number} */ |
366 Sources.CSSSourceFrame.maxSwatchProcessingLength = 300; | 366 Sources.CSSSourceFrame.maxSwatchProcessingLength = 300; |
367 /** @type {symbol} */ | 367 /** @type {symbol} */ |
368 Sources.CSSSourceFrame.SwatchBookmark = Symbol('swatch'); | 368 Sources.CSSSourceFrame.SwatchBookmark = Symbol('swatch'); |
OLD | NEW |