Improve completions when class contains trigger character (#710)

* Improve completions when class contains trigger character

* wip

* wip
master
Brad Cornes 2023-01-27 13:24:41 +00:00 committed by GitHub
parent 8c152bd650
commit 862af2289f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 2 deletions

View File

@ -412,6 +412,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( async function provideClassAttributeCompletions(
state: State, state: State,
document: TextDocument, document: TextDocument,
@ -423,6 +453,8 @@ async function provideClassAttributeCompletions(
end: position, end: position,
}) })
str = ensureTriggerCharacterIsIncluded(str, document, position, context)
let matches = matchClassAttributes( let matches = matchClassAttributes(
str, str,
(await state.editor.getConfiguration(document.uri)).tailwindCSS.classAttributes (await state.editor.getConfiguration(document.uri)).tailwindCSS.classAttributes
@ -545,13 +577,16 @@ async function provideCustomClassNameCompletions(
function provideAtApplyCompletions( function provideAtApplyCompletions(
state: State, state: State,
document: TextDocument, document: TextDocument,
position: Position position: Position,
context?: CompletionContext
): CompletionList { ): CompletionList {
let str = document.getText({ let str = document.getText({
start: { line: Math.max(position.line - 30, 0), character: 0 }, start: { line: Math.max(position.line - 30, 0), character: 0 },
end: position, end: position,
}) })
str = ensureTriggerCharacterIsIncluded(str, document, position, context)
const match = findLast(/@apply\s+(?<classList>[^;}]*)$/gi, str) const match = findLast(/@apply\s+(?<classList>[^;}]*)$/gi, str)
if (match === null) { if (match === null) {
@ -596,7 +631,7 @@ async function provideClassNameCompletions(
context?: CompletionContext context?: CompletionContext
): Promise<CompletionList> { ): Promise<CompletionList> {
if (isCssContext(state, document, position)) { 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)) { if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) {