Skip to content

Commit c8e6ff9

Browse files
devvaannshabose
authored andcommitted
feat: suggest previously used colors in file
1 parent 35b53fd commit c8e6ff9

File tree

1 file changed

+78
-0
lines changed
  • src/extensions/default/CSSCodeHints

1 file changed

+78
-0
lines changed

src/extensions/default/CSSCodeHints/main.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ define(function (require, exports, module) {
2626

2727
var AppInit = brackets.getModule("utils/AppInit"),
2828
CodeHintManager = brackets.getModule("editor/CodeHintManager"),
29+
EditorManager = brackets.getModule("editor/EditorManager"),
2930
CSSUtils = brackets.getModule("language/CSSUtils"),
3031
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
3132
TokenUtils = brackets.getModule("utils/TokenUtils"),
@@ -92,6 +93,62 @@ define(function (require, exports, module) {
9293
this.exclusion = null;
9394
}
9495

96+
function isAlphanumeric(char) {
97+
return /^[a-z0-9-@$]$/i.test(char);
98+
}
99+
100+
function isValidColor(text, colorMatch) {
101+
const colorIndex = colorMatch.index;
102+
const previousChar = colorIndex === 0 ? "" : text.charAt(colorIndex - 1);
103+
const endIndex = colorIndex + colorMatch[0].length;
104+
const nextChar = endIndex === text.length ? "" : text.charAt(endIndex);
105+
return !isAlphanumeric(previousChar) && !isAlphanumeric(nextChar);
106+
}
107+
108+
function updateColorList(colorList, color, lineNumber) {
109+
const existingColor = colorList.find(item => item.color === color);
110+
if (existingColor) {
111+
existingColor.count++;
112+
if (!existingColor.lines.includes(lineNumber)) {
113+
existingColor.lines.push(lineNumber);
114+
}
115+
} else {
116+
colorList.push({
117+
color: color,
118+
lines: [lineNumber],
119+
count: 1
120+
});
121+
}
122+
}
123+
124+
function getAllColorsInFile() {
125+
const editor = EditorManager.getActiveEditor();
126+
const nLen = editor.lineCount();
127+
128+
const colorList = [];
129+
130+
for (let i = 0; i < nLen; i++) {
131+
const lineText = editor.getLine(i);
132+
133+
if (!lineText || lineText.length > 1000) {
134+
continue;
135+
}
136+
137+
const matches = [...lineText.matchAll(ColorUtils.COLOR_REGEX)];
138+
139+
for (const match of matches) {
140+
if (isValidColor(lineText, match)) {
141+
const token = editor.getToken({ line: i, ch: match.index });
142+
if (token && token.type !== "comment") {
143+
updateColorList(colorList, match[0], i);
144+
}
145+
}
146+
}
147+
}
148+
149+
return colorList;
150+
}
151+
95152
/**
96153
* Check whether the exclusion is still the same as text after the cursor.
97154
* If not, reset it to null.
@@ -298,6 +355,10 @@ define(function (require, exports, module) {
298355
result,
299356
selectInitial = false;
300357

358+
let previouslyUsedColors = [];
359+
360+
361+
301362
// Clear the exclusion if the user moves the cursor with left/right arrow key.
302363
this.updateExclusion(true);
303364

@@ -339,6 +400,22 @@ define(function (require, exports, module) {
339400
let isColorSwatch = false;
340401
if (type === "color") {
341402
isColorSwatch = true;
403+
404+
405+
const colorList = getAllColorsInFile();
406+
407+
// Convert COLOR_LIST to previouslyUsedColors format and sort by count
408+
previouslyUsedColors = colorList
409+
.sort((a, b) => b.count - a.count) // Sort in descending order by count
410+
.map(item => item.color); // Extract only the colors
411+
412+
// Combine default hex, rgb colors with existing color names
413+
valueArray = previouslyUsedColors.concat(
414+
ColorUtils.COLOR_NAMES.map(function (color) {
415+
return { text: color, color: color };
416+
})
417+
);
418+
342419
valueArray = valueArray.concat(ColorUtils.COLOR_NAMES.map(function (color) {
343420
return { text: color, color: color };
344421
}));
@@ -351,6 +428,7 @@ define(function (require, exports, module) {
351428

352429
result = StringMatch.codeHintsSort(valueNeedle, valueArray, {
353430
limit: MAX_CSS_HINTS,
431+
boostPrefixList: previouslyUsedColors, // for named colors to make them appear before other color hints
354432
onlyContiguous: isColorSwatch // for color swatches, when searching for `ora` we should
355433
// only hint <ora>nge and not <o>lived<ra>b (green shade)
356434
});

0 commit comments

Comments
 (0)