Skip to content

Discard function matches that appear after the current cursor in completions #1278

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

Merged
merged 5 commits into from
Mar 20, 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 @@ -849,3 +849,61 @@ defineTest({
})
},
})

defineTest({
name: 'v4: class function completions mixed with class attribute completions work',
fs: {
'app.css': css`
@import 'tailwindcss';
`,
},
prepare: async ({ root }) => ({ client: await createClient({ root }) }),
handle: async ({ client }) => {
let document = await client.open({
settings: {
tailwindCSS: {
classAttributes: ['className'],
classFunctions: ['cn', 'cva'],
},
},
lang: 'javascriptreact',
text: js`
let x = cva("")

export function Button() {
return <Test className={cn("")} />
}

export function Button2() {
return <Test className={cn("")} />
}

let y = cva("")
`,
})

// let x = cva("");
// ^
let completionA = await document.completions({ line: 0, character: 13 })

expect(completionA?.items.length).toBe(12289)

// return <Test className={cn("")} />;
// ^
let completionB = await document.completions({ line: 3, character: 30 })

expect(completionB?.items.length).toBe(12289)

// return <Test className={cn("")} />;
// ^
let completionC = await document.completions({ line: 7, character: 30 })

expect(completionC?.items.length).toBe(12289)

// let y = cva("");
// ^
let completionD = await document.completions({ line: 10, character: 13 })

expect(completionD?.items.length).toBe(12289)
},
})
13 changes: 9 additions & 4 deletions packages/tailwindcss-language-service/src/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,9 @@ async function provideClassAttributeCompletions(
position: Position,
context?: CompletionContext,
): Promise<CompletionList> {
let current = document.offsetAt(position)
let range: Range = {
start: document.positionAt(Math.max(0, document.offsetAt(position) - SEARCH_RANGE)),
start: document.positionAt(Math.max(0, current - SEARCH_RANGE)),
end: position,
}

Expand Down Expand Up @@ -734,13 +735,17 @@ async function provideClassAttributeCompletions(
let offset = document.offsetAt(boundary.range.start)
let fnMatches = matchClassFunctions(str, settings.classFunctions)

fnMatches.forEach((match) => {
for (let match of fnMatches) {
if (match.index) match.index += offset
})
if (match.index > current) continue

matches.push(...fnMatches)
matches.push(match)
}
}

// Make sure matches are sorted by index
matches.sort((a, b) => a.index - b.index)

if (matches.length === 0) {
return null
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Prerelease

- Nothing yet!
- Fix completions not showing for some class attributes when a class function exists in the document ([#1278](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1278))

# 0.14.10

Expand Down