diff --git a/src/extensions/default/CSSCodeHints/main.js b/src/extensions/default/CSSCodeHints/main.js index 0b17d87a7f..2d4e417839 100644 --- a/src/extensions/default/CSSCodeHints/main.js +++ b/src/extensions/default/CSSCodeHints/main.js @@ -24,20 +24,21 @@ define(function (require, exports, module) { - var AppInit = brackets.getModule("utils/AppInit"), - CodeHintManager = brackets.getModule("editor/CodeHintManager"), - CSSUtils = brackets.getModule("language/CSSUtils"), - PreferencesManager = brackets.getModule("preferences/PreferencesManager"), - TokenUtils = brackets.getModule("utils/TokenUtils"), - StringMatch = brackets.getModule("utils/StringMatch"), - ColorUtils = brackets.getModule("utils/ColorUtils"), - Strings = brackets.getModule("strings"), - KeyEvent = brackets.getModule("utils/KeyEvent"), - LiveDevelopment = brackets.getModule("LiveDevelopment/main"), - Metrics = brackets.getModule("utils/Metrics"), - AllPreferences = brackets.getModule("preferences/AllPreferences"), - CSSProperties = require("text!CSSProperties.json"), - properties = JSON.parse(CSSProperties); + var AppInit = brackets.getModule("utils/AppInit"), + CodeHintManager = brackets.getModule("editor/CodeHintManager"), + EditorManager = brackets.getModule("editor/EditorManager"), + CSSUtils = brackets.getModule("language/CSSUtils"), + PreferencesManager = brackets.getModule("preferences/PreferencesManager"), + TokenUtils = brackets.getModule("utils/TokenUtils"), + StringMatch = brackets.getModule("utils/StringMatch"), + ColorUtils = brackets.getModule("utils/ColorUtils"), + Strings = brackets.getModule("strings"), + KeyEvent = brackets.getModule("utils/KeyEvent"), + LiveDevelopment = brackets.getModule("LiveDevelopment/main"), + Metrics = brackets.getModule("utils/Metrics"), + AllPreferences = brackets.getModule("preferences/AllPreferences"), + CSSProperties = require("text!CSSProperties.json"), + properties = JSON.parse(CSSProperties); /** * Emmet API: @@ -92,6 +93,75 @@ define(function (require, exports, module) { this.exclusion = null; } + function isAlphanumeric(char) { + return /^[a-z0-9-@$]$/i.test(char); + } + + function isValidColor(text, colorMatch) { + const colorIndex = colorMatch.index; + const previousChar = colorIndex === 0 ? "" : text.charAt(colorIndex - 1); + const endIndex = colorIndex + colorMatch[0].length; + const nextChar = endIndex === text.length ? "" : text.charAt(endIndex); + return !isAlphanumeric(previousChar) && !isAlphanumeric(nextChar); + } + + function updateColorList(colorList, color, lineNumber) { + const existingColor = colorList.find(item => item.color === color); + if (existingColor) { + existingColor.count++; + if (!existingColor.lines.includes(lineNumber)) { + existingColor.lines.push(lineNumber); + } + } else { + colorList.push({ + color: color, + lines: [lineNumber], + count: 1 + }); + } + } + + function getAllColorsInFile() { + const editor = EditorManager.getActiveEditor(); + const nLen = editor.lineCount(); + + const colorList = []; + + for (let i = 0; i < nLen; i++) { + const lineText = editor.getLine(i); + + if (!lineText || lineText.length > 1000) { + continue; + } + + const matches = [...lineText.matchAll(ColorUtils.COLOR_REGEX)]; + + for (const match of matches) { + if (isValidColor(lineText, match)) { + const token = editor.getToken({ line: i, ch: match.index }); + + // this check is added to filter out non-required colors. For ex: + // the color should not be inside a commented line + // the color should not be written as a plain text (in html files) + // like

Red is bad.

, we need to ignore this Red + if ( + token && + ( + // If we're in an HTML file (token.state.htmlState exists), + // then only process if inside a