Skip to content

Commit 604f1cb

Browse files
committed
fix: less and css variables like --blue-desk incorrectly treated as colors
1 parent 61e31fa commit 604f1cb

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

docs/API-Reference/editor/Editor.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const Editor = brackets.getModule("editor/Editor")
107107
* [.getLanguageForPosition()](#Editor+getLanguageForPosition) ⇒ <code>Language</code>
108108
* [.getModeForDocument()](#Editor+getModeForDocument) ⇒ <code>Object</code> \| <code>String</code>
109109
* [.updateLayout([forceRefresh])](#Editor+updateLayout)
110-
* [.setGutterMarker(lineNumber, gutterName, marker)](#Editor+setGutterMarker)
110+
* [.setGutterMarker(lineNumber, gutterName, marker)](#Editor+setGutterMarker) ⇒ <code>Object</code>
111111
* [.getGutterMarker(lineNumber, gutterName)](#Editor+getGutterMarker)
112112
* [.clearGutterMarker(lineNumber, gutterName)](#Editor+clearGutterMarker)
113113
* [.isGutterActive(gutterName)](#Editor+isGutterActive)
@@ -1237,10 +1237,12 @@ should not be used on inline editors
12371237

12381238
<a name="Editor+setGutterMarker"></a>
12391239

1240-
### editor.setGutterMarker(lineNumber, gutterName, marker)
1240+
### editor.setGutterMarker(lineNumber, gutterName, marker) ⇒ <code>Object</code>
12411241
Sets the marker for the specified gutter on the specified line number
12421242

12431243
**Kind**: instance method of [<code>Editor</code>](#Editor)
1244+
**Returns**: <code>Object</code> - lineHandle this can be used to track the gutter line as the line number
1245+
changes as the user edits code.
12441246

12451247
| Param | Type | Description |
12461248
| --- | --- | --- |

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ define(function (require, exports, module) {
230230
// Add listener for all editor changes
231231
EditorManager.on("activeEditorChange", function (event, newEditor, oldEditor) {
232232
if (newEditor && newEditor.isGutterActive(GUTTER_NAME)) {
233+
console.time("xxxxxxxxxxx");
233234
newEditor.off("cursorActivity.colorPreview");
234235
newEditor.on("cursorActivity.colorPreview", _cursorActivity);
235236
// Unbind the previous editor's change event if it exists
@@ -240,6 +241,7 @@ define(function (require, exports, module) {
240241
newEditor.on("change", onChanged);
241242
showColorMarks();
242243
_cursorActivity(null, newEditor);
244+
console.timeEnd("xxxxxxxxxxx");
243245
}
244246
});
245247

@@ -317,6 +319,16 @@ define(function (require, exports, module) {
317319
return token.type !== "comment";
318320
}
319321

322+
function isAlphanumeric(char) {
323+
return /^[a-z0-9-@$]$/i.test(char);
324+
}
325+
function _isColor(segment, colorInSegment, colorIndex) {
326+
const previousChar = colorIndex === 0 ? "" : segment.charAt(colorIndex-1);
327+
const endIndex = colorIndex + colorInSegment.length;
328+
const nextChar = endIndex === segment.length ? "" : segment.charAt(endIndex);
329+
return !isAlphanumeric(previousChar) && !isAlphanumeric(nextChar);
330+
}
331+
320332
/**
321333
* Detects valid colors in a given line of text
322334
*
@@ -329,7 +341,7 @@ define(function (require, exports, module) {
329341
const languageID = editor.document.getLanguage().getId();
330342

331343
// to make sure that code doesn't break when lineText is null.
332-
if (!lineText) {
344+
if (!lineText || lineText.length > 1000) { // too long lines we cant scan, maybe minified?
333345
return [];
334346
}
335347

@@ -345,6 +357,10 @@ define(function (require, exports, module) {
345357

346358
colorMatches.forEach(colorMatch => {
347359
const colorIndex = lineMatch.index + colorMatch.index;
360+
// this will also allow color name like vars eg: --red-main or @heading-green. we need to omit those
361+
if(!_isColor(lineMatch[0], colorMatch[0], colorMatch.index)) {
362+
return;
363+
}
348364

349365
// Check if the color is within a comment
350366
const token = editor.getToken({ line: lineNumber, ch: colorIndex }, true);

test/spec/CSSColorPreview-test-files/base.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
.class-5 {color: #b7ff00 green #3e4395;}
1515
.class-6 {color: #ff0090 #802095 #954e3e #454e3e;}
1616
.class-6 {color: #ff0090 #802095 #954e3e #454e3e #150e3e;}
17+
18+
.class-vars {
19+
color: var(--slight-red); background-color: @blue-light; color: var(--red); background-color: @blue;
20+
}

test/spec/Extn-CSSColorPreview-integ-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ define(function (require, exports, module) {
306306
it(`should deleting at end of file and appending should bring back color ${fileName}`, async function () {
307307
const editor = await init();
308308

309-
__PR.setCursors(["12:1-19:1"]);
309+
__PR.setCursors(["12:1-33:1"]);
310310
__PR.keydown(["BACK_SPACE"]); // this will delete the colors at end of file
311311
__PR.validateEqual(editor.lineCount(), 12);
312312
await __PR.undo();

0 commit comments

Comments
 (0)