Skip to content

Commit 76464d0

Browse files
perf: improve color swatches (#31)
1 parent 902c48e commit 76464d0

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

src/lib/completions.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ExtensionContext, languages, Range, Position, CompletionItem, CompletionItemKind, Color, ColorInformation, Hover } from 'vscode';
22
import { highlightCSS, isColor, getConfig } from '../utils';
33
import { fileTypes } from '../utils/filetypes';
4-
import { ClassParser } from 'windicss/utils/parser';
4+
import { HTMLParser, ClassParser } from 'windicss/utils/parser';
55
import type { Core } from '../interfaces';
66
import type { Disposable } from 'vscode';
77

@@ -90,22 +90,18 @@ export async function registerCompletions(ctx: ExtensionContext, core: Core): Pr
9090
// insert color before class
9191
provideDocumentColors: (document, token) => {
9292
const colors: ColorInformation[] = [];
93-
for (const line of Array.from(Array(document.lineCount).keys())) {
94-
const text = document.lineAt(line).text;
95-
if (text.match(/(class|className|dark|light|active|after|before|checked|disabled|focus|hover|tw)=["|']([.\w-+@: ]*)/)) {
96-
const matched = text.match(/(?<=(class|className|dark|light|active|after|before|checked|disabled|focus|hover|tw)=["|'])[^"']*/);
97-
if (matched && matched.index) {
98-
const offset = matched.index;
99-
const elements = new ClassParser(matched[0]).parse(false);
100-
elements.forEach(element => {
101-
if (typeof element.content === 'string') {
102-
const color = isColor(element.raw, core.colors);
103-
if (color) {
104-
const char = element.start + offset;
105-
colors.push(new ColorInformation(new Range(new Position(line, char), new Position(line, char + 1)), new Color(color[0]/255, color[1]/255, color[2]/255, 1)));
106-
}
107-
}
108-
});
93+
// try one time update instead of line
94+
const documentText = document.getText();
95+
const parser = new HTMLParser(documentText);
96+
const classes = parser.parseClasses();
97+
if (classes) {
98+
for (const c of classes) {
99+
const elements = new ClassParser(c.result).parse(false);
100+
for (const element of elements) {
101+
const color = isColor(element.raw, core.colors);
102+
if(color) {
103+
colors.push(new ColorInformation(new Range(document.positionAt(c.start+element.start), document.positionAt(c.start+element.start + 1)), new Color(color[0]/255, color[1]/255, color[2]/255, 1)));
104+
}
109105
}
110106
}
111107
}

0 commit comments

Comments
 (0)