Skip to content

Commit 14f59e9

Browse files
committed
feat: ignore colors in commented lines, works in any file type
1 parent bfac013 commit 14f59e9

File tree

1 file changed

+19
-15
lines changed
  • src/extensionsIntegrated/CSSColorPreview

1 file changed

+19
-15
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,26 @@ define(function (require, exports, module) {
6969
for (let i = 0; i < nLen; i++) {
7070
let lineText = editor.getLine(i);
7171

72-
if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
72+
// to skip commented lines
73+
// The second parameter in the getToken() method, `true`, enables precise tokenization.
74+
// Without setting `precise` to `true`, multi-line comments are not handled correctly.
75+
const token = editor.getToken({line: i}, true);
76+
if(token.type === "comment") {
7377
continue;
74-
} else {
75-
let regx = /:[^;]*;/g;
76-
77-
lineText = lineText.match(regx);
78-
if (lineText) {
79-
let tempColors = lineText[0].match(COLOR_REGEX);
80-
// Support up to 4 colors
81-
if (tempColors && tempColors.length > 0) {
82-
let colors = tempColors.slice(0, 4);
83-
aColors.push({
84-
lineNumber: i,
85-
colorValues: colors
86-
});
87-
}
78+
}
79+
80+
let regx = /:[^;]*;/g;
81+
82+
lineText = lineText.match(regx);
83+
if (lineText) {
84+
let tempColors = lineText[0].match(COLOR_REGEX);
85+
// Support up to 4 colors
86+
if (tempColors && tempColors.length > 0) {
87+
let colors = tempColors.slice(0, 4);
88+
aColors.push({
89+
lineNumber: i,
90+
colorValues: colors
91+
});
8892
}
8993
}
9094
}

0 commit comments

Comments
 (0)