Skip to content

Commit 0b2db09

Browse files
committed
Improve completions when class contains trigger character
1 parent 8e70fbb commit 0b2db09

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export function completionsFromClassList(
4747
document?: TextDocument,
4848
context?: CompletionContext
4949
): CompletionList {
50-
let classNames = classList.split(/[\s+]/)
51-
const partialClassName = classNames[classNames.length - 1]
50+
let classNamesAndWhitespace = classList.split(/(\s+)/)
51+
const partialClassName = classNamesAndWhitespace[classNamesAndWhitespace.length - 1]
5252
let sep = state.separator
5353
let parts = partialClassName.split(sep)
5454
let subset: any
@@ -57,10 +57,10 @@ export function completionsFromClassList(
5757

5858
let replacementRange = {
5959
...classListRange,
60-
start: {
61-
...classListRange.start,
62-
character: classListRange.end.character - partialClassName.length,
63-
},
60+
start: document.positionAt(
61+
document.offsetAt(classListRange.start) +
62+
classNamesAndWhitespace.slice(0, classNamesAndWhitespace.length - 1).join('').length
63+
),
6464
}
6565

6666
if (state.jit) {
@@ -201,10 +201,12 @@ export function completionsFromClassList(
201201
resultingVariants.slice(0, resultingVariants.length - 1).join(sep) +
202202
sep,
203203
range: {
204-
start: {
205-
...classListRange.start,
206-
character: classListRange.end.character - partialClassName.length,
207-
},
204+
start: document.positionAt(
205+
document.offsetAt(classListRange.start) +
206+
classNamesAndWhitespace
207+
.slice(0, classNamesAndWhitespace.length - 1)
208+
.join('').length
209+
),
208210
end: {
209211
...replacementRange.start,
210212
character: replacementRange.start.character,
@@ -427,16 +429,6 @@ function ensureTriggerCharacterIsIncluded(
427429
context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter
428430
text.slice(-1) !== context.triggerCharacter
429431
) {
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-
}
440432
return `${text.slice(0, text.length - 1)}${context.triggerCharacter}`
441433
}
442434
return text
@@ -448,8 +440,9 @@ async function provideClassAttributeCompletions(
448440
position: Position,
449441
context?: CompletionContext
450442
): Promise<CompletionList> {
443+
let startOffset = Math.max(0, document.offsetAt(position) - 1000)
451444
let str = document.getText({
452-
start: document.positionAt(Math.max(0, document.offsetAt(position) - 1000)),
445+
start: document.positionAt(startOffset),
453446
end: position,
454447
})
455448

@@ -466,11 +459,13 @@ async function provideClassAttributeCompletions(
466459

467460
let match = matches[matches.length - 1]
468461

469-
const lexer =
462+
let lexer =
470463
match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']'))
471464
? getComputedClassAttributeLexer()
472465
: getClassAttributeLexer()
473-
lexer.reset(str.substr(match.index + match[0].length - 1))
466+
let attributeOffset = match.index + match[0].length - 1
467+
let attributeText = str.substr(attributeOffset)
468+
lexer.reset(attributeText)
474469

475470
try {
476471
let tokens = Array.from(lexer)
@@ -489,10 +484,9 @@ async function provideClassAttributeCompletions(
489484
state,
490485
classList,
491486
{
492-
start: {
493-
line: position.line,
494-
character: position.character - classList.length,
495-
},
487+
start: document.positionAt(
488+
startOffset + attributeOffset + (attributeText.length - classList.length)
489+
),
496490
end: position,
497491
},
498492
undefined,

0 commit comments

Comments
 (0)