Skip to content

Commit 8ec9b53

Browse files
committed
refactor: better support for variant nested and prefix completion
1 parent 1703572 commit 8ec9b53

3 files changed

Lines changed: 26 additions & 11 deletions

File tree

src/lib/completions.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,22 @@ export default class Completions {
140140
if (text.match(/(<\w+\s*)[^>]*$/) !== null) {
141141
if (!text.match(/\S+(?=\s*=\s*["']?[^"']*$)/) || text.match(/<\w+\s+$/)) {
142142
let completions: CompletionItem[] = [];
143-
if (enableUtility) {
144-
completions = completions.concat(
145-
Object.keys(this.completions.attr.static).map(label => attrKey(label, CompletionItemKind.Field, 0))
146-
);
143+
const prefix = this.processor.config('attributify.prefix');
144+
const disable = this.processor.config('attributify.disable') as string[] | undefined;
145+
let prevKey = undefined;
146+
if (text.endsWith(':')) {
147+
prevKey = document.getText(document.getWordRangeAtPosition(new Position(position.line, position.character-1), /[^:\s]+/));
147148
}
148-
if (enableVariant) {
149-
const prefix = this.processor.config('attributify.prefix');
150-
completions = completions.concat(
151-
Object.keys(this.extension.variants).map(label => attrKey(prefix ? prefix + label: label, CompletionItemKind.Module, 1))
152-
);
149+
if (!prevKey || (prevKey && (prevKey in this.extension.variants || this.extension.isAttrVariant(prevKey)))) {
150+
if (enableUtility) {
151+
completions = completions.concat(
152+
Object.keys(this.completions.attr.static).map(label => attrKey(prefix && !text.endsWith(':') ? prefix + label : label, CompletionItemKind.Field, 0))
153+
);
154+
}
155+
if (enableVariant) {
156+
const variants = disable ? Object.keys(this.extension.variants).filter(i => !disable.includes(i)) : Object.keys(this.extension.variants);
157+
completions = completions.concat(variants.map(label => attrKey(prefix && !text.endsWith(':') ? prefix + label : label, CompletionItemKind.Module, 1)));
158+
}
153159
}
154160
return completions;
155161
}

src/lib/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export default class Extension {
2626
processor: Processor | undefined;
2727
attrs: Attr['static'];
2828
colors: DictStr;
29+
attrPrefix?: string;
2930
configFile?: string;
3031
variants: ResolvedVariants;
3132
disposables: Disposable[];
3233
constructor(ctx: ExtensionContext, pattern: GlobPattern) {
3334
this.ctx = ctx;
35+
this.attrPrefix = undefined;
3436
this.pattern = pattern;
3537
this.colors = {};
3638
this.attrs = {};
@@ -45,6 +47,7 @@ export default class Extension {
4547
this.jiti = jiti(__filename);
4648
this.configFile = files[0] ? files[0].fsPath : undefined;
4749
this.processor = new Processor(this.loadConfig(this.configFile)) as Processor;
50+
this.attrPrefix = this.processor.config('attributify.prefix') as string | undefined;
4851
this.variants = this.processor.resolveVariants();
4952
this.colors = flatColors(this.processor.theme('colors', {}) as colorObject);
5053
this.register();
@@ -224,12 +227,20 @@ export default class Extension {
224227
}
225228

226229
isAttrVariant(word: string): boolean {
230+
if (this.attrPrefix) {
231+
if (!word.startsWith(this.attrPrefix)) return false;
232+
word = word.slice(this.attrPrefix.length);
233+
}
227234
const lastKey = word.match(/[^:-]+$/)?.[0] || word;
228235
return getConfig('windicss.enableAttrVariantCompletion') && lastKey in this.variants;
229236
}
230237

231238
isAttrUtility(word?: string): string | undefined {
232239
if (!word) return;
240+
if (this.attrPrefix) {
241+
if (!word.startsWith(this.attrPrefix)) return;
242+
word = word.slice(this.attrPrefix.length);
243+
}
233244
const lastKey = word.match(/[^:-]+$/)?.[0] || word;
234245
return getConfig('windicss.enableAttrUtilityCompletion') && lastKey in this.attrs ? lastKey : undefined;
235246
}

src/utils/completions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ export function generateCompletions(processor: Processor, colors: colorObject, a
6969
const attr: Attr = { static: {}, color: {}, bracket: {}, dynamic: {} };
7070

7171
if (attributify) {
72-
const attrPrefix = processor.config('attributify.prefix') as string | undefined;
7372
const attrDisable = processor.config('attributify.disable') as string[] | undefined;
7473
const addAttr = (key: string, value: any, type: 'static' | 'color' | 'bracket' | 'dynamic' = 'static') => {
7574
if (attrDisable && attrDisable.includes(key)) return;
76-
if (attrPrefix) key = attrPrefix + key;
7775
key in attr[type] ? attr[type][key].push(value) : attr[type][key] = [ value ];
7876
};
7977

0 commit comments

Comments
 (0)