Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4: Improve DX around completions when prefixes are in use #1292

Merged
merged 3 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,80 @@ defineTest({
},
})

defineTest({
name: 'v4: Completions show after a variant arbitrary value, using prefixes',
fs: {
'app.css': css`
@import 'tailwindcss' prefix(tw);
`,
},
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
handle: async ({ client }) => {
let document = await client.open({
lang: 'html',
text: '<div class="tw:data-[foo]:">',
})

// <div class="tw:data-[foo]:">
// ^
let completion = await document.completions({ line: 0, character: 26 })

expect(completion?.items.length).toBe(19236)
},
})

defineTest({
name: 'v4: Variant and utility suggestions show prefix when one has been typed',
fs: {
'app.css': css`
@import 'tailwindcss' prefix(tw);
`,
},
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
handle: async ({ client }) => {
let document = await client.open({
lang: 'html',
text: '<div class="">',
})

// <div class="">
// ^
let completion = await document.completions({ line: 0, character: 12 })

expect(completion?.items.length).toBe(19237)

// Verify that variants and utilities are all prefixed
let prefixed = completion.items.filter((item) => !item.label.startsWith('tw:'))
expect(prefixed).toHaveLength(0)
},
})

defineTest({
name: 'v4: Variant and utility suggestions hide prefix when it has been typed',
fs: {
'app.css': css`
@import 'tailwindcss' prefix(tw);
`,
},
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
handle: async ({ client }) => {
let document = await client.open({
lang: 'html',
text: '<div class="tw:">',
})

// <div class="tw:">
// ^
let completion = await document.completions({ line: 0, character: 15 })

expect(completion?.items.length).toBe(19236)

// Verify that no variants and utilities have prefixes
let prefixed = completion.items.filter((item) => item.label.startsWith('tw:'))
expect(prefixed).toHaveLength(0)
},
})

defineTest({
name: 'v4: Completions show inside class functions in JS/TS files',
fs: {
Expand Down
36 changes: 18 additions & 18 deletions packages/tailwindcss-language-service/src/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,29 +261,25 @@ export function completionsFromClassList(

// TODO: This is a bit of a hack
if (prefix.length > 0) {
// No variants seen: suggest the prefix only
// No variants seen:
// - suggest the prefix as a variant
// - Modify the remaining items to include the prefix in the variant name
if (existingVariants.length === 0) {
items = items.slice(0, 1)
items = items.map((item, idx) => {
if (idx === 0) return item

return withDefaults(
{
isIncomplete: false,
items,
},
{
data: {
...(state.completionItemData ?? {}),
...(important ? { important } : {}),
variants: existingVariants,
},
range: replacementRange,
},
state.editor.capabilities.itemDefaults,
)
item.label = `${prefix}:${item.label}`

if (item.textEditText) {
item.textEditText = `${prefix}:${item.textEditText}`
}

return item
})
}

// The first variant is not the prefix: don't suggest anything
if (existingVariants[0] !== prefix) {
if (existingVariants.length > 0 && existingVariants[0] !== prefix) {
return null
}
}
Expand All @@ -304,6 +300,10 @@ export function completionsFromClassList(
documentation = formatColor(color)
}

if (prefix.length > 0 && existingVariants.length === 0) {
className = `${prefix}:${className}`
}

items.push({
label: className,
kind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export function getVariantsFromClassName(
// NOTE: This should never happen
if (!state.designSystem) return false

let prefix = state.designSystem.theme.prefix ?? ''

if (prefix !== '') {
className = `${prefix}:${className}`
}

// We don't use `compile()` so there's no overhead from PostCSS
let compiled = state.designSystem.candidatesToCss([className])

Expand Down