Skip to content

Commit 9071c73

Browse files
committed
fix: resolved issues with CSSColorPreview
1 parent ec7064d commit 9071c73

File tree

1 file changed

+128
-42
lines changed
  • src/extensionsIntegrated/CSSColorPreview

1 file changed

+128
-42
lines changed

src/extensionsIntegrated/CSSColorPreview/main.js

Lines changed: 128 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -53,44 +53,6 @@ define(function (require, exports, module) {
5353
description: Strings.DESCRIPTION_CSS_COLOR_PREVIEW
5454
});
5555

56-
/**
57-
* Responsible to get all the colors and their respective line numbers.
58-
*
59-
* @param {Editor} editor
60-
* @return {Array.<Object>} an array of objects with all the line nos and,
61-
* the colors to be added on those lines
62-
*/
63-
function _getAllColorsAndLineNums(editor) {
64-
65-
const nLen = editor.lineCount();
66-
const aColors = [];
67-
68-
// match colors and push into an array
69-
for (let i = 0; i < nLen; i++) {
70-
let lineText = editor.getLine(i);
71-
72-
if ((lineText.indexOf('/*') !== -1) || (lineText.indexOf('*/') !== -1)) {
73-
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-
}
88-
}
89-
}
90-
}
91-
92-
return aColors;
93-
}
9456

9557
/**
9658
* Gets all the colors that are to be displayed
@@ -168,15 +130,22 @@ define(function (require, exports, module) {
168130

169131
/**
170132
* To display the color marks on the gutter
133+
*
171134
* @param {activeEditor} editor
172135
* @param {Array.<object>} _results An array of objects which stores
173136
* all the line numbers and the colors to be displayed on that line.
137+
* @param {Boolean} update marks whether this function is called when some lines
138+
* are updated or when the whole file is re-updated. Defaults to false.
174139
*/
175-
function showGutters(editor, _results) {
140+
function showGutters(editor, _results, update = false) {
176141
if (editor && enabled) {
177142
initGutter(editor);
178143
const cm = editor._codeMirror;
179-
editor.clearGutter(GUTTER_NAME); // clear color markers
144+
// if the file is updated we don't need to clear the gutter
145+
// as it will clear all the existing markers.
146+
if(!update) {
147+
editor.clearGutter(GUTTER_NAME); // clear color markers
148+
}
180149
_addDummyGutterMarkerIfNotExist(editor, editor.getCursorPos().line);
181150

182151
// Only add markers if enabled
@@ -319,11 +288,128 @@ define(function (require, exports, module) {
319288
}
320289
}
321290

291+
/**
292+
* Detects valid colors in a given line of text
293+
*
294+
* @param {Editor} editor - The editor instance
295+
* @param {number} lineNumber - The line number to check
296+
* @return {Array<{color: string, index: number}>} An array of valid color values with their indices
297+
*/
298+
function detectValidColorsInLine(editor, lineNumber) {
299+
const lineText = editor.getLine(lineNumber);
300+
const valueRegex = /:[^;]*;/g;
301+
const validColors = [];
302+
303+
// Find all property value sections in the line
304+
const lineMatches = [...lineText.matchAll(valueRegex)];
305+
306+
for (const lineMatch of lineMatches) {
307+
// Find colors within each property value
308+
const colorMatches = [...lineMatch[0].matchAll(COLOR_REGEX)];
309+
310+
colorMatches.forEach(colorMatch => {
311+
const colorIndex = lineMatch.index + colorMatch.index;
312+
313+
// Check if the color is within a comment
314+
const token = editor.getToken({ line: lineNumber, ch: colorIndex }, true);
315+
316+
// If the token is not a comment, add the color
317+
if (token.type !== "comment") {
318+
validColors.push({
319+
color: colorMatch[0],
320+
index: colorIndex
321+
});
322+
}
323+
});
324+
}
325+
326+
// Return up to 4 colors
327+
return validColors.slice(0, 4).map(item => item.color);
328+
}
329+
330+
/**
331+
* Responsible to get all the colors and their respective line numbers.
332+
*
333+
* @param {Editor} editor
334+
* @return {Array.<Object>} an array of objects with all the line nos and,
335+
* the colors to be added on those lines
336+
*/
337+
function _getAllColorsAndLineNums(editor) {
338+
const nLen = editor.lineCount();
339+
const aColors = [];
340+
341+
// Match colors and push into an array
342+
for (let i = 0; i < nLen; i++) {
343+
const colors = detectValidColorsInLine(editor, i);
344+
345+
// If valid colors found, add to the results
346+
if (colors.length > 0) {
347+
aColors.push({
348+
lineNumber: i,
349+
colorValues: colors
350+
});
351+
}
352+
}
353+
354+
return aColors;
355+
}
356+
357+
358+
/**
359+
* Responsible to update the color marks only on the modified lines
360+
*
361+
* @param {Editor} editor the editor instance
362+
* @param {Number} fromLineNumber modification start from line number
363+
* @param {Number} toLineNumber modification upto line number
364+
* @return {Array.<Object>} an array of objects with all the line nos and,
365+
* the colors to be added on those lines
366+
*/
367+
function updateColorMarks(editor, fromLineNumber, toLineNumber) {
368+
const aColors = [];
369+
370+
// Match colors and push into an array for modified lines
371+
for (let i = fromLineNumber; i <= toLineNumber; i++) {
372+
const colors = detectValidColorsInLine(editor, i);
373+
374+
// If no valid colors, clear the gutter marker
375+
if (colors.length === 0) {
376+
editor.setGutterMarker(i, GUTTER_NAME, "");
377+
} else {
378+
aColors.push({
379+
lineNumber: i,
380+
colorValues: colors
381+
});
382+
}
383+
}
384+
385+
return aColors;
386+
}
387+
388+
322389
/**
323390
* Function that gets triggered when any change occurs on the editor
391+
*
392+
* @param {Editor} instance the codemirror instance
393+
* @param {Object} changeObj an object that has properties regarding the line changed and type of change
324394
*/
325-
function onChanged() {
326-
showColorMarks();
395+
function onChanged(instance, changeObj) {
396+
397+
const editor = EditorManager.getActiveEditor();
398+
399+
// for insertion and deletion, update the changed lines
400+
if(changeObj.origin === '+input' || changeObj.origin === '+delete') {
401+
// make sure that the required properties exist and in the form they are expected to be
402+
if(changeObj.from.line && changeObj.to.line && changeObj.from.line <= changeObj.to.line) {
403+
const aColors = updateColorMarks(editor, changeObj.from.line, changeObj.to.line);
404+
showGutters(editor, aColors, true);
405+
} else {
406+
showColorMarks();
407+
}
408+
409+
} else { // for any complex operation like, cut, paste etc, we re-update the whole file
410+
showColorMarks();
411+
}
412+
327413
}
328414

329415
/**

0 commit comments

Comments
 (0)