Improve completions when class contains trigger character

master
Brad Cornes 2023-01-27 15:48:53 +00:00
parent 8e70fbb460
commit 0b2db09a22
1 changed files with 21 additions and 27 deletions

View File

@ -47,8 +47,8 @@ export function completionsFromClassList(
document?: TextDocument, document?: TextDocument,
context?: CompletionContext context?: CompletionContext
): CompletionList { ): CompletionList {
let classNames = classList.split(/[\s+]/) let classNamesAndWhitespace = classList.split(/(\s+)/)
const partialClassName = classNames[classNames.length - 1] const partialClassName = classNamesAndWhitespace[classNamesAndWhitespace.length - 1]
let sep = state.separator let sep = state.separator
let parts = partialClassName.split(sep) let parts = partialClassName.split(sep)
let subset: any let subset: any
@ -57,10 +57,10 @@ export function completionsFromClassList(
let replacementRange = { let replacementRange = {
...classListRange, ...classListRange,
start: { start: document.positionAt(
...classListRange.start, document.offsetAt(classListRange.start) +
character: classListRange.end.character - partialClassName.length, classNamesAndWhitespace.slice(0, classNamesAndWhitespace.length - 1).join('').length
}, ),
} }
if (state.jit) { if (state.jit) {
@ -201,10 +201,12 @@ export function completionsFromClassList(
resultingVariants.slice(0, resultingVariants.length - 1).join(sep) + resultingVariants.slice(0, resultingVariants.length - 1).join(sep) +
sep, sep,
range: { range: {
start: { start: document.positionAt(
...classListRange.start, document.offsetAt(classListRange.start) +
character: classListRange.end.character - partialClassName.length, classNamesAndWhitespace
}, .slice(0, classNamesAndWhitespace.length - 1)
.join('').length
),
end: { end: {
...replacementRange.start, ...replacementRange.start,
character: replacementRange.start.character, character: replacementRange.start.character,
@ -427,16 +429,6 @@ function ensureTriggerCharacterIsIncluded(
context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter
text.slice(-1) !== context.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.slice(0, text.length - 1)}${context.triggerCharacter}`
} }
return text return text
@ -448,8 +440,9 @@ async function provideClassAttributeCompletions(
position: Position, position: Position,
context?: CompletionContext context?: CompletionContext
): Promise<CompletionList> { ): Promise<CompletionList> {
let startOffset = Math.max(0, document.offsetAt(position) - 1000)
let str = document.getText({ let str = document.getText({
start: document.positionAt(Math.max(0, document.offsetAt(position) - 1000)), start: document.positionAt(startOffset),
end: position, end: position,
}) })
@ -466,11 +459,13 @@ async function provideClassAttributeCompletions(
let match = matches[matches.length - 1] let match = matches[matches.length - 1]
const lexer = let lexer =
match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']')) match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']'))
? getComputedClassAttributeLexer() ? getComputedClassAttributeLexer()
: getClassAttributeLexer() : getClassAttributeLexer()
lexer.reset(str.substr(match.index + match[0].length - 1)) let attributeOffset = match.index + match[0].length - 1
let attributeText = str.substr(attributeOffset)
lexer.reset(attributeText)
try { try {
let tokens = Array.from(lexer) let tokens = Array.from(lexer)
@ -489,10 +484,9 @@ async function provideClassAttributeCompletions(
state, state,
classList, classList,
{ {
start: { start: document.positionAt(
line: position.line, startOffset + attributeOffset + (attributeText.length - classList.length)
character: position.character - classList.length, ),
},
end: position, end: position,
}, },
undefined, undefined,