Skip to content

Commit 2af2dba

Browse files
Make sure completions work when using prefixes in v4 (#1129)
Fixes #1128
1 parent 618cbfc commit 2af2dba

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-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,

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Add support for `@custom-variant` ([#1127](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1127))
99
- Add variant suggestions to `@variant` ([#1127](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1127))
1010
- Don't suggest at-rules when nested that cannot be used in a nested context ([#1127](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1127))
11+
- Make sure completions work when using prefixes in v4 ([#1129](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1129))
1112

1213
## 0.12.18
1314

0 commit comments

Comments
 (0)