Revert "Improve completions when class contains trigger character"

master
Brad Cornes 2023-01-27 17:11:37 +00:00
parent 086dfb4c28
commit ef09d7431f
1 changed files with 33 additions and 73 deletions

View File

@ -43,12 +43,12 @@ export function completionsFromClassList(
state: State, state: State,
classList: string, classList: string,
classListRange: Range, classListRange: Range,
document: TextDocument,
filter?: (item: CompletionItem) => boolean, filter?: (item: CompletionItem) => boolean,
document?: TextDocument,
context?: CompletionContext context?: CompletionContext
): CompletionList { ): CompletionList {
let classNamesAndWhitespace = classList.split(/(\s+)/) let classNames = classList.split(/[\s+]/)
const partialClassName = classNamesAndWhitespace[classNamesAndWhitespace.length - 1] const partialClassName = classNames[classNames.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: document.positionAt( start: {
document.offsetAt(classListRange.start) + ...classListRange.start,
classNamesAndWhitespace.slice(0, classNamesAndWhitespace.length - 1).join('').length character: classListRange.end.character - partialClassName.length,
), },
} }
if (state.jit) { if (state.jit) {
@ -201,12 +201,10 @@ export function completionsFromClassList(
resultingVariants.slice(0, resultingVariants.length - 1).join(sep) + resultingVariants.slice(0, resultingVariants.length - 1).join(sep) +
sep, sep,
range: { range: {
start: document.positionAt( start: {
document.offsetAt(classListRange.start) + ...classListRange.start,
classNamesAndWhitespace character: classListRange.end.character - partialClassName.length,
.slice(0, classNamesAndWhitespace.length - 1) },
.join('').length
),
end: { end: {
...replacementRange.start, ...replacementRange.start,
character: replacementRange.start.character, character: replacementRange.start.character,
@ -414,40 +412,17 @@ 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
) {
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,
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(startOffset), start: document.positionAt(Math.max(0, document.offsetAt(position) - 1000)),
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
@ -459,13 +434,11 @@ async function provideClassAttributeCompletions(
let match = matches[matches.length - 1] let match = matches[matches.length - 1]
let lexer = const lexer =
match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']')) match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']'))
? getComputedClassAttributeLexer() ? getComputedClassAttributeLexer()
: getClassAttributeLexer() : getClassAttributeLexer()
let attributeOffset = match.index + match[0].length - 1 lexer.reset(str.substr(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)
@ -484,13 +457,14 @@ async function provideClassAttributeCompletions(
state, state,
classList, classList,
{ {
start: document.positionAt( start: {
startOffset + attributeOffset + (attributeText.length - classList.length) line: position.line,
), character: position.character - classList.length,
},
end: position, end: position,
}, },
document,
undefined, undefined,
document,
context context
) )
} }
@ -553,18 +527,13 @@ async function provideCustomClassNameCompletions(
classList = containerMatch[1].substr(0, cursor - matchStart) classList = containerMatch[1].substr(0, cursor - matchStart)
} }
return completionsFromClassList( return completionsFromClassList(state, classList, {
state,
classList,
{
start: { start: {
line: position.line, line: position.line,
character: position.character - classList.length, character: position.character - classList.length,
}, },
end: position, end: position,
}, })
document
)
} }
} }
} catch (_) {} } catch (_) {}
@ -576,16 +545,13 @@ 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) {
@ -604,7 +570,6 @@ function provideAtApplyCompletions(
}, },
end: position, end: position,
}, },
document,
(item) => { (item) => {
if (item.kind === 9) { if (item.kind === 9) {
return ( return (
@ -631,7 +596,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, context) return provideAtApplyCompletions(state, document, position)
} }
if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) { if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) {
@ -1338,18 +1303,13 @@ async function provideEmmetCompletions(
const parts = emmetItems.items[0].label.split('.') const parts = emmetItems.items[0].label.split('.')
if (parts.length < 2) return null if (parts.length < 2) return null
return completionsFromClassList( return completionsFromClassList(state, parts[parts.length - 1], {
state,
parts[parts.length - 1],
{
start: { start: {
line: position.line, line: position.line,
character: position.character - parts[parts.length - 1].length, character: position.character - parts[parts.length - 1].length,
}, },
end: position, end: position,
}, })
document
)
} }
export async function doComplete( export async function doComplete(