|
1 | 1 | import { ExtensionContext, languages, Range, Position, CompletionItem, CompletionItemKind, Color, ColorInformation, Hover } from 'vscode'; |
2 | 2 | import { highlightCSS, isColor, getConfig } from '../utils'; |
3 | 3 | import { fileTypes } from '../utils/filetypes'; |
4 | | -import { ClassParser } from 'windicss/utils/parser'; |
| 4 | +import { HTMLParser, ClassParser } from 'windicss/utils/parser'; |
5 | 5 | import type { Core } from '../interfaces'; |
6 | 6 | import type { Disposable } from 'vscode'; |
7 | 7 |
|
@@ -90,22 +90,18 @@ export async function registerCompletions(ctx: ExtensionContext, core: Core): Pr |
90 | 90 | // insert color before class |
91 | 91 | provideDocumentColors: (document, token) => { |
92 | 92 | 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 | + } |
109 | 105 | } |
110 | 106 | } |
111 | 107 | } |
|
0 commit comments