Skip to content

Commit e55d3cb

Browse files
committed
Make sure completions work when using prefixes in v4
1 parent 618cbfc commit e55d3cb

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/tailwindcss-language-server/src/projects.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,20 @@ function isAtRule(node: Node): node is AtRule {
13651365

13661366
function getVariants(state: State): Array<Variant> {
13671367
if (state.v4) {
1368-
return state.designSystem.getVariants()
1368+
let variants = Array.from(state.designSystem.getVariants())
1369+
1370+
let prefix = state.designSystem.theme.prefix ?? ''
1371+
if (prefix.length > 0) {
1372+
variants.unshift({
1373+
name: prefix,
1374+
values: [],
1375+
isArbitrary: false,
1376+
hasDash: true,
1377+
selectors: () => ['&'],
1378+
})
1379+
}
1380+
1381+
return variants
13691382
}
13701383

13711384
if (state.jitContext?.getVariants) {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export function completionsFromClassList(
7272
}
7373

7474
if (state.v4) {
75+
let prefix = state.designSystem.theme.prefix ?? ''
76+
7577
let { variants: existingVariants, offset } = getVariantsFromClassName(state, partialClassName)
7678

7779
if (
@@ -277,6 +279,35 @@ export function completionsFromClassList(
277279
}
278280
}
279281

282+
// TODO: This is a bit of a hack
283+
if (prefix.length > 0) {
284+
// No variants seen: suggest the prefix only
285+
if (existingVariants.length === 0) {
286+
items = items.slice(0, 1)
287+
288+
return withDefaults(
289+
{
290+
isIncomplete: false,
291+
items,
292+
},
293+
{
294+
data: {
295+
...(state.completionItemData ?? {}),
296+
...(important ? { important } : {}),
297+
variants: existingVariants,
298+
},
299+
range: replacementRange,
300+
},
301+
state.editor.capabilities.itemDefaults,
302+
)
303+
}
304+
305+
// The first variant is not the prefix: don't suggest anything
306+
if (existingVariants[0] !== prefix) {
307+
return null
308+
}
309+
}
310+
280311
return withDefaults(
281312
{
282313
isIncomplete: false,

0 commit comments

Comments
 (0)