From bbd5ae59dde03a175475d0da1d1dd00009b07e6b Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Thu, 26 Jan 2023 11:06:07 +0000 Subject: [PATCH 1/3] Improve completions when class contains trigger character --- .../src/completionProvider.ts | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts index 63a5edf3..16cb1e99 100644 --- a/packages/tailwindcss-language-service/src/completionProvider.ts +++ b/packages/tailwindcss-language-service/src/completionProvider.ts @@ -1,5 +1,5 @@ import { Settings, State } from './util/state' -import type { +import { CompletionItem, CompletionItemKind, Range, @@ -8,6 +8,7 @@ import type { TextDocument, Position, CompletionContext, + CompletionTriggerKind, } from 'vscode-languageserver' import dlv from 'dlv' import removeMeta from './util/removeMeta' @@ -405,6 +406,21 @@ export function completionsFromClassList( } } +// This might be a VS Code bug? +// The trigger character is not included in the document text +function ensureTriggerCharacterIsIncluded(text: string, context?: CompletionContext): string { + if (!context) { + return text + } + if ( + context.triggerKind === CompletionTriggerKind.TriggerCharacter && + text.slice(-1) !== context.triggerCharacter + ) { + return `${text.slice(0, text.length - 1)}${context.triggerCharacter}` + } + return text +} + async function provideClassAttributeCompletions( state: State, document: TextDocument, @@ -416,6 +432,8 @@ async function provideClassAttributeCompletions( end: position, }) + str = ensureTriggerCharacterIsIncluded(str, context) + let matches = matchClassAttributes( str, (await state.editor.getConfiguration(document.uri)).tailwindCSS.classAttributes @@ -538,13 +556,16 @@ async function provideCustomClassNameCompletions( function provideAtApplyCompletions( state: State, document: TextDocument, - position: Position + position: Position, + context?: CompletionContext ): CompletionList { let str = document.getText({ start: { line: Math.max(position.line - 30, 0), character: 0 }, end: position, }) + str = ensureTriggerCharacterIsIncluded(str, context) + const match = findLast(/@apply\s+(?[^;}]*)$/gi, str) if (match === null) { @@ -587,7 +608,7 @@ async function provideClassNameCompletions( context?: CompletionContext ): Promise { if (isCssContext(state, document, position)) { - return provideAtApplyCompletions(state, document, position) + return provideAtApplyCompletions(state, document, position, context) } if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) { From 539252a77520b967db57417ccbb728f31f487f79 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 27 Jan 2023 10:42:50 +0000 Subject: [PATCH 2/3] wip --- .../tailwindcss-language-service/src/completionProvider.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts index 16cb1e99..640afbab 100644 --- a/packages/tailwindcss-language-service/src/completionProvider.ts +++ b/packages/tailwindcss-language-service/src/completionProvider.ts @@ -1,5 +1,5 @@ import { Settings, State } from './util/state' -import { +import type { CompletionItem, CompletionItemKind, Range, @@ -8,7 +8,6 @@ import { TextDocument, Position, CompletionContext, - CompletionTriggerKind, } from 'vscode-languageserver' import dlv from 'dlv' import removeMeta from './util/removeMeta' @@ -413,7 +412,7 @@ function ensureTriggerCharacterIsIncluded(text: string, context?: CompletionCont return text } if ( - context.triggerKind === CompletionTriggerKind.TriggerCharacter && + context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter text.slice(-1) !== context.triggerCharacter ) { return `${text.slice(0, text.length - 1)}${context.triggerCharacter}` From 55063b01b57ed624534cc84268c0ee1ce7cacc93 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 27 Jan 2023 11:01:40 +0000 Subject: [PATCH 3/3] wip --- .../src/completionProvider.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts index 640afbab..4d9c5c00 100644 --- a/packages/tailwindcss-language-service/src/completionProvider.ts +++ b/packages/tailwindcss-language-service/src/completionProvider.ts @@ -407,7 +407,12 @@ export function completionsFromClassList( // This might be a VS Code bug? // The trigger character is not included in the document text -function ensureTriggerCharacterIsIncluded(text: string, context?: CompletionContext): string { +function ensureTriggerCharacterIsIncluded( + text: string, + document: TextDocument, + position: Position, + context?: CompletionContext +): string { if (!context) { return text } @@ -415,6 +420,16 @@ function ensureTriggerCharacterIsIncluded(text: string, context?: CompletionCont context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter text.slice(-1) !== context.triggerCharacter ) { + let nextChar = document.getText({ + start: position, + end: document.positionAt(document.offsetAt(position) + 1), + }) + // If there's a next char (i.e. we're not at the end of the document) + // then it will be included instead of the trigger character, so we replace it. + // Otherwise we just append. + if (nextChar.length === 0) { + return `${text}${context.triggerCharacter}` + } return `${text.slice(0, text.length - 1)}${context.triggerCharacter}` } return text @@ -431,7 +446,7 @@ async function provideClassAttributeCompletions( end: position, }) - str = ensureTriggerCharacterIsIncluded(str, context) + str = ensureTriggerCharacterIsIncluded(str, document, position, context) let matches = matchClassAttributes( str, @@ -563,7 +578,7 @@ function provideAtApplyCompletions( end: position, }) - str = ensureTriggerCharacterIsIncluded(str, context) + str = ensureTriggerCharacterIsIncluded(str, document, position, context) const match = findLast(/@apply\s+(?[^;}]*)$/gi, str)