Skip to content

Commit

Permalink
feat: support show color decoration in apply #114
Browse files Browse the repository at this point in the history
  • Loading branch information
voorjaar committed May 19, 2021
1 parent 59949ce commit 15b809b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/lib/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ export function registerCompletions(ctx: ExtensionContext, core: Core): Disposab

for (const attr of parser.parseAttrs()) {
if (isAttrUtility(attr.key)) {
// inset decoration in bg|text|... = "..."
// insert decoration in bg|text|... = "..."
const regex = /\S+/igm;
const data = attr.value.raw;
let match;
Expand All @@ -357,7 +357,7 @@ export function registerCompletions(ctx: ExtensionContext, core: Core): Disposab
}
}
} else if (['class', 'className'].includes(attr.key) || isAttrVariant(attr.key)) {
// inset decoration in class|className|sm|hover|... = "..."
// insert decoration in class|className|sm|hover|... = "..."
const elements = new ClassParser(attr.value.raw, core.processor?.config('separator', ':') as string, Object.keys(core.variants)).parse(false);
for (const element of elements) {
if (element.type === 'group' && Array.isArray(element.content)) {
Expand All @@ -372,6 +372,21 @@ export function registerCompletions(ctx: ExtensionContext, core: Core): Disposab
}
}

// insert decoration in @apply ...
for (const className of parser.parseApplies()) {
const elements = new ClassParser(className.result, core.processor?.config('separator', ':') as string, Object.keys(core.variants)).parse(false);
for (const element of elements) {
if (element.type === 'group' && Array.isArray(element.content)) {
for (const e of element.content) {
const color = isValidColor(e.raw);
if(color) colors.push(createColor(document, className.start, e.start, color));
}
}
const color = element.type === 'utility' && isValidColor(element.raw);
if(color) colors.push(createColor(document, className.start, element.start, color));
}
}

return colors;
},
provideColorPresentations: () => {
Expand Down
20 changes: 19 additions & 1 deletion src/utils/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type Attr = { raw: string, key: string, value: { raw: string, start: number }, start: number, end: number };
export type ClassName = { result: string, start: number, end: number };

export class HTMLParser {
html?: string;
Expand Down Expand Up @@ -39,7 +40,7 @@ export class HTMLParser {
return output;
}

parseClasses(): { start: number; end: number; result: string }[] {
parseClasses(): ClassName[] {
// Match all class properties
if (!this.html) return [];
const classRegex = /\s(?:class|className|w:dark|w:light|w:active|w:after|w:before|w:checked|w:disabled|w:focus|w:hover|w:tw)=(["'])([^{}]+)\1/;
Expand Down Expand Up @@ -67,4 +68,21 @@ export class HTMLParser {
}
return classNames;
}

parseApplies(): ClassName[] {
if (!this.html) return [];
const output: ClassName[] = [];
const regex = /(?<=@apply\s+)[^;]+(?=\s+!important)|(?<=@apply\s+)[^;]+(?=;)/igm;
let match;
while ((match = regex.exec(this.html as string))) {
if (match) {
output.push({
result: this.html.slice(match.index, regex.lastIndex),
start: match.index,
end: regex.lastIndex,
});
}
}
return output;
}
}

0 comments on commit 15b809b

Please sign in to comment.