Skip to content

Commit ef09d74

Browse files
committed
Revert "Improve completions when class contains trigger character"
1 parent 086dfb4 commit ef09d74

File tree

1 file changed

+33
-73
lines changed

1 file changed

+33
-73
lines changed

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

+33-73
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ export function completionsFromClassList(
4343
state: State,
4444
classList: string,
4545
classListRange: Range,
46-
document: TextDocument,
4746
filter?: (item: CompletionItem) => boolean,
47+
document?: TextDocument,
4848
context?: CompletionContext
4949
): CompletionList {
50-
let classNamesAndWhitespace = classList.split(/(\s+)/)
51-
const partialClassName = classNamesAndWhitespace[classNamesAndWhitespace.length - 1]
50+
let classNames = classList.split(/[\s+]/)
51+
const partialClassName = classNames[classNames.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: document.positionAt(
61-
document.offsetAt(classListRange.start) +
62-
classNamesAndWhitespace.slice(0, classNamesAndWhitespace.length - 1).join('').length
63-
),
60+
start: {
61+
...classListRange.start,
62+
character: classListRange.end.character - partialClassName.length,
63+
},
6464
}
6565

6666
if (state.jit) {
@@ -201,12 +201,10 @@ export function completionsFromClassList(
201201
resultingVariants.slice(0, resultingVariants.length - 1).join(sep) +
202202
sep,
203203
range: {
204-
start: document.positionAt(
205-
document.offsetAt(classListRange.start) +
206-
classNamesAndWhitespace
207-
.slice(0, classNamesAndWhitespace.length - 1)
208-
.join('').length
209-
),
204+
start: {
205+
...classListRange.start,
206+
character: classListRange.end.character - partialClassName.length,
207+
},
210208
end: {
211209
...replacementRange.start,
212210
character: replacementRange.start.character,
@@ -414,40 +412,17 @@ export function completionsFromClassList(
414412
)
415413
}
416414

417-
// This might be a VS Code bug?
418-
// The trigger character is not included in the document text
419-
function ensureTriggerCharacterIsIncluded(
420-
text: string,
421-
document: TextDocument,
422-
position: Position,
423-
context?: CompletionContext
424-
): string {
425-
if (!context) {
426-
return text
427-
}
428-
if (
429-
context.triggerKind === 2 && // CompletionTriggerKind.TriggerCharacter
430-
text.slice(-1) !== context.triggerCharacter
431-
) {
432-
return `${text.slice(0, text.length - 1)}${context.triggerCharacter}`
433-
}
434-
return text
435-
}
436-
437415
async function provideClassAttributeCompletions(
438416
state: State,
439417
document: TextDocument,
440418
position: Position,
441419
context?: CompletionContext
442420
): Promise<CompletionList> {
443-
let startOffset = Math.max(0, document.offsetAt(position) - 1000)
444421
let str = document.getText({
445-
start: document.positionAt(startOffset),
422+
start: document.positionAt(Math.max(0, document.offsetAt(position) - 1000)),
446423
end: position,
447424
})
448425

449-
str = ensureTriggerCharacterIsIncluded(str, document, position, context)
450-
451426
let matches = matchClassAttributes(
452427
str,
453428
(await state.editor.getConfiguration(document.uri)).tailwindCSS.classAttributes
@@ -459,13 +434,11 @@ async function provideClassAttributeCompletions(
459434

460435
let match = matches[matches.length - 1]
461436

462-
let lexer =
437+
const lexer =
463438
match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']'))
464439
? getComputedClassAttributeLexer()
465440
: getClassAttributeLexer()
466-
let attributeOffset = match.index + match[0].length - 1
467-
let attributeText = str.substr(attributeOffset)
468-
lexer.reset(attributeText)
441+
lexer.reset(str.substr(match.index + match[0].length - 1))
469442

470443
try {
471444
let tokens = Array.from(lexer)
@@ -484,13 +457,14 @@ async function provideClassAttributeCompletions(
484457
state,
485458
classList,
486459
{
487-
start: document.positionAt(
488-
startOffset + attributeOffset + (attributeText.length - classList.length)
489-
),
460+
start: {
461+
line: position.line,
462+
character: position.character - classList.length,
463+
},
490464
end: position,
491465
},
492-
document,
493466
undefined,
467+
document,
494468
context
495469
)
496470
}
@@ -553,18 +527,13 @@ async function provideCustomClassNameCompletions(
553527
classList = containerMatch[1].substr(0, cursor - matchStart)
554528
}
555529

556-
return completionsFromClassList(
557-
state,
558-
classList,
559-
{
560-
start: {
561-
line: position.line,
562-
character: position.character - classList.length,
563-
},
564-
end: position,
530+
return completionsFromClassList(state, classList, {
531+
start: {
532+
line: position.line,
533+
character: position.character - classList.length,
565534
},
566-
document
567-
)
535+
end: position,
536+
})
568537
}
569538
}
570539
} catch (_) {}
@@ -576,16 +545,13 @@ async function provideCustomClassNameCompletions(
576545
function provideAtApplyCompletions(
577546
state: State,
578547
document: TextDocument,
579-
position: Position,
580-
context?: CompletionContext
548+
position: Position
581549
): CompletionList {
582550
let str = document.getText({
583551
start: { line: Math.max(position.line - 30, 0), character: 0 },
584552
end: position,
585553
})
586554

587-
str = ensureTriggerCharacterIsIncluded(str, document, position, context)
588-
589555
const match = findLast(/@apply\s+(?<classList>[^;}]*)$/gi, str)
590556

591557
if (match === null) {
@@ -604,7 +570,6 @@ function provideAtApplyCompletions(
604570
},
605571
end: position,
606572
},
607-
document,
608573
(item) => {
609574
if (item.kind === 9) {
610575
return (
@@ -631,7 +596,7 @@ async function provideClassNameCompletions(
631596
context?: CompletionContext
632597
): Promise<CompletionList> {
633598
if (isCssContext(state, document, position)) {
634-
return provideAtApplyCompletions(state, document, position, context)
599+
return provideAtApplyCompletions(state, document, position)
635600
}
636601

637602
if (isHtmlContext(state, document, position) || isJsxContext(state, document, position)) {
@@ -1338,18 +1303,13 @@ async function provideEmmetCompletions(
13381303
const parts = emmetItems.items[0].label.split('.')
13391304
if (parts.length < 2) return null
13401305

1341-
return completionsFromClassList(
1342-
state,
1343-
parts[parts.length - 1],
1344-
{
1345-
start: {
1346-
line: position.line,
1347-
character: position.character - parts[parts.length - 1].length,
1348-
},
1349-
end: position,
1306+
return completionsFromClassList(state, parts[parts.length - 1], {
1307+
start: {
1308+
line: position.line,
1309+
character: position.character - parts[parts.length - 1].length,
13501310
},
1351-
document
1352-
)
1311+
end: position,
1312+
})
13531313
}
13541314

13551315
export async function doComplete(

0 commit comments

Comments
 (0)