From f9045bdd32594b64f960fe52f44e36f2dc4ecc73 Mon Sep 17 00:00:00 2001 From: voorjaar Date: Sun, 23 May 2021 13:50:54 +0800 Subject: [PATCH] feat: support bracket snippet in attr and class --- src/interfaces.ts | 4 ++++ src/lib/completions.ts | 29 +++++++++++++++++++++++++++-- src/utils/completions.ts | 22 ++++++++++++++++++---- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/interfaces.ts b/src/interfaces.ts index 3aa6d60..dce3df4 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -30,6 +30,9 @@ export interface Attr { doc: string }[] }, + bracket: { + [key:string]: string[] + }, dynamic: { [key:string]: { label: string @@ -44,6 +47,7 @@ export interface Completion { label: string doc: string }[], + bracket: string[] dynamic: { label: string pos: number diff --git a/src/lib/completions.ts b/src/lib/completions.ts index 217647c..7ff7310 100644 --- a/src/lib/completions.ts +++ b/src/lib/completions.ts @@ -73,10 +73,17 @@ export default class Completions { } if (enableDynamic) { + completions = completions.concat( + this.completions.bracket.map((label, index) => { + const item = new CompletionItem(label, CompletionItemKind.Struct); + item.sortText = '3-' + index.toString().padStart(8, '0'); + return item; + }) + ); completions = completions.concat( this.completions.dynamic.map(({ label, pos }, index) => { const item = new CompletionItem(label, CompletionItemKind.Variable); - item.sortText = '3-' + index.toString().padStart(8, '0'); + item.sortText = '4-' + index.toString().padStart(8, '0'); item.command = { command: 'cursorMove', arguments: [{ @@ -103,6 +110,10 @@ export default class Completions { item.documentation = this.buildVariantDoc(item.detail); item.detail = undefined; break; + case CompletionItemKind.Struct: + item.documentation = buildStyle(this.processor.interpret(item.label).styleSheet); + item.insertText = new SnippetString(`${item.label.replace('-[', '-[${1:').slice(0, -1)}}]`); + break; case CompletionItemKind.Variable: // TODO break; @@ -212,10 +223,19 @@ export default class Completions { } if (enableDynamic && key in this.completions.attr.dynamic) { + completions = completions.concat( + this.completions.attr.bracket[key].map((label, index) => { + const item = new CompletionItem(label, CompletionItemKind.Struct); + item.detail = key; + item.sortText = '3-' + index.toString().padStart(8, '0'); + return item; + }) + ); + completions = completions.concat( this.completions.attr.dynamic[key].map(({ label, pos }, index) => { const item = new CompletionItem(label, CompletionItemKind.Variable); - item.sortText = '3-' + index.toString().padStart(8, '0'); + item.sortText = '5-' + index.toString().padStart(8, '0'); item.command = { command: 'cursorMove', arguments: [{ @@ -245,6 +265,11 @@ export default class Completions { item.documentation = this.buildAttrDoc(attr, variant, this.separator); item.detail = undefined; break; + case CompletionItemKind.Struct: + item.documentation = buildStyle(this.processor.attributify({ [item.detail ?? ''] : [ item.label ] }).styleSheet); + item.insertText = new SnippetString(`${item.label.replace('[', '[${1:').slice(0, -1)}}]`); + item.detail = undefined; + break; case CompletionItemKind.Variable: break; case CompletionItemKind.Color: diff --git a/src/utils/completions.ts b/src/utils/completions.ts index e814c59..82da001 100644 --- a/src/utils/completions.ts +++ b/src/utils/completions.ts @@ -9,10 +9,12 @@ export function generateCompletions(processor: Processor, colors: colorObject, a const completions: Completion = { static: [], color: [], + bracket: [], dynamic: [], attr: { static: {}, color: {}, + bracket: {}, dynamic: {}, }, }; @@ -20,8 +22,13 @@ export function generateCompletions(processor: Processor, colors: colorObject, a const staticUtilities = processor.resolveStaticUtilities(true); // generate normal utilities completions for (const [config, list] of Object.entries(utilities)) { - list.forEach(utility => { - const mark = utility.search(/\$/); + for (const utility of list) { + const bracket = utility.indexOf('['); + if (bracket !== -1) { + completions.bracket.push(utility); + continue; + } + const mark = utility.indexOf('$'); if (mark === -1) { completions.static.push(utility); } else { @@ -55,11 +62,11 @@ export function generateCompletions(processor: Processor, colors: colorObject, a break; } } - }); + } } // generate attributify completions - const attr: Attr = { static: {}, color: {}, dynamic: {} }; + const attr: Attr = { static: {}, color: {}, bracket: {}, dynamic: {} }; const addStatic = (key: string, value: string) => { key in attr.static ? attr.static[key].push(value) : attr.static[key] = [ value ]; }; @@ -234,6 +241,13 @@ export function generateCompletions(processor: Processor, colors: colorObject, a } } + for (const utility of completions.bracket) { + const { key, body } = split(utility); + if (key) { + attr.bracket[key] = key in attr.bracket ? [...attr.bracket[key], body] : [ body ]; + } + } + for (const { label, pos } of completions.dynamic) { const { key, body } = split(label); if (key) {