Skip to content

Improve completions when class contains trigger character #710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions packages/tailwindcss-language-service/src/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ export function completionsFromClassList(
}
}

// This might be a VS Code bug?
// The trigger character is not included in the document text
function ensureTriggerCharacterIsIncluded(
text: string,
document: TextDocument,
position: Position,
context?: CompletionContext
): string {
if (!context) {
return text
}
if (
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
}

async function provideClassAttributeCompletions(
state: State,
document: TextDocument,
Expand All @@ -416,6 +446,8 @@ async function provideClassAttributeCompletions(
end: position,
})

str = ensureTriggerCharacterIsIncluded(str, document, position, context)

let matches = matchClassAttributes(
str,
(await state.editor.getConfiguration(document.uri)).tailwindCSS.classAttributes
Expand Down Expand Up @@ -538,13 +570,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, document, position, context)

const match = findLast(/@apply\s+(?<classList>[^;}]*)$/gi, str)

if (match === null) {
Expand Down Expand Up @@ -587,7 +622,7 @@ async function provideClassNameCompletions(
context?: CompletionContext
): Promise<CompletionList> {
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)) {
Expand Down