Skip to content

Commit 862af22

Browse files
authored
Improve completions when class contains trigger character (tailwindlabs#710)
* Improve completions when class contains trigger character * wip * wip
1 parent 8c152bd commit 862af22

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

packages/tailwindcss-language-service/src/completionProvider.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,36 @@ export function completionsFromClassList(
412412
)
413413
}
414414

415+
// This might be a VS Code bug?
416+
// The trigger character is not included in the document text
417+
function ensureTriggerCharacterIsIncluded(
418+
text: string,
419+
document: TextDocument,
420+
position: Position,
421+
context?: CompletionContext
422+
): string {
423+
if (!context) {
424+
return text
425+
}
426+
if (
427+
context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter
428+
text.slice(-1) !== context.triggerCharacter
429+
) {
430+
let nextChar = document.getText({
431+
start: position,
432+
end: document.positionAt(document.offsetAt(position) + 1),
433+
})
434+
// If there's a next char (i.e. we're not at the end of the document)
435+
// then it will be included instead of the trigger character, so we replace it.
436+
// Otherwise we just append.
437+
if (nextChar.length === 0) {
438+
return `${text}${context.triggerCharacter}`
439+
}
440+
return `${text.slice(0, text.length - 1)}${context.triggerCharacter}`
441+
}
442+
return text
443+
}
444+
415445
async function provideClassAttributeCompletions(
416446
state: State,
417447
document: TextDocument,
@@ -423,6 +453,8 @@ async function provideClassAttributeCompletions(
423453
end: position,
424454
})
425455

456+
str = ensureTriggerCharacterIsIncluded(str, document, position, context)
457+
426458
let matches = matchClassAttributes(
427459
str,
428460
(await state.editor.getConfiguration(document.uri)).tailwindCSS.classAttributes
@@ -545,13 +577,16 @@ async function provideCustomClassNameCompletions(
545577
function provideAtApplyCompletions(
546578
state: State,
547579
document: TextDocument,
548-
position: Position
580+
position: Position,
581+
context?: CompletionContext
549582
): CompletionList {
550583
let str = document.getText({
551584
start: { line: Math.max(position.line - 30, 0), character: 0 },
552585
end: position,
553586
})
554587

588+
str = ensureTriggerCharacterIsIncluded(str, document, position, context)
589+
555590
const match = findLast(/@apply\s+(?<classList>[^;}]*)$/gi, str)
556591

557592
if (match === null) {
@@ -596,7 +631,7 @@ async function provideClassNameCompletions(
596631
context?: CompletionContext
597632
): Promise<CompletionList> {
598633
if (isCssContext(state, document, position)) {
599-
return provideAtApplyCompletions(state, document, position)
634+
return provideAtApplyCompletions(state, document, position, context)
600635
}
601636

602637
if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) {

0 commit comments

Comments
 (0)