Skip to content

Support class function hovers in Svelte and HTML <script> blocks #1311

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 3 commits into from
Apr 23, 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
86 changes: 85 additions & 1 deletion packages/tailwindcss-language-service/src/util/find.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { test } from 'vitest'
import { findClassListsInHtmlRange } from './find'
import { findClassListsInHtmlRange, findClassNameAtPosition } from './find'
import { js, html, pug, createDocument } from './test-utils'

test('class regex works in astro', async ({ expect }) => {
Expand Down Expand Up @@ -791,3 +791,87 @@ test('classAttributes find class lists inside Vue bindings', async ({ expect })
},
])
})

test('Can find class name inside JS/TS functions in <script> tags (HTML)', async ({ expect }) => {
let file = createDocument({
name: 'file.html',
lang: 'html',
settings: {
tailwindCSS: {
classFunctions: ['clsx'],
},
},
content: html`
<script>
let classes = clsx('flex relative')
</script>
`,
})

let className = await findClassNameAtPosition(file.state, file.doc, {
line: 1,
character: 23,
})

expect(className).toEqual({
className: 'flex',
range: {
start: { line: 1, character: 22 },
end: { line: 1, character: 26 },
},
relativeRange: {
start: { line: 0, character: 0 },
end: { line: 0, character: 4 },
},
classList: {
classList: 'flex relative',
important: undefined,
range: {
start: { character: 22, line: 1 },
end: { character: 35, line: 1 },
},
},
})
})

test('Can find class name inside JS/TS functions in <script> tags (Svelte)', async ({ expect }) => {
let file = createDocument({
name: 'file.svelte',
lang: 'svelte',
settings: {
tailwindCSS: {
classFunctions: ['clsx'],
},
},
content: html`
<script>
let classes = clsx('flex relative')
</script>
`,
})

let className = await findClassNameAtPosition(file.state, file.doc, {
line: 1,
character: 23,
})

expect(className).toEqual({
className: 'flex',
range: {
start: { line: 1, character: 22 },
end: { line: 1, character: 26 },
},
relativeRange: {
start: { line: 0, character: 0 },
end: { line: 0, character: 4 },
},
classList: {
classList: 'flex relative',
important: undefined,
range: {
start: { character: 22, line: 1 },
end: { character: 35, line: 1 },
},
},
})
})
4 changes: 2 additions & 2 deletions packages/tailwindcss-language-service/src/util/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import lineColumn from 'line-column'
import { isCssContext, isCssDoc } from './css'
import { isHtmlContext, isVueDoc } from './html'
import { isWithinRange } from './isWithinRange'
import { isJsxContext } from './js'
import { isJsContext } from './js'
import { dedupeByRange, flatten } from './array'
import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
import { getLanguageBoundaries } from './getLanguageBoundaries'
Expand Down Expand Up @@ -526,7 +526,7 @@ export async function findClassNameAtPosition(
classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
} else if (isHtmlContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
} else if (isJsxContext(state, doc, position)) {
} else if (isJsContext(state, doc, position)) {
classNames = await findClassNamesInRange(state, doc, searchRange, 'jsx')
} else {
classNames = await findClassNamesInRange(state, doc, searchRange)
Expand Down
1 change: 1 addition & 0 deletions packages/tailwindcss-language-service/src/util/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export function createState(
return {
enabled: true,
features: [],
blocklist: [],
...partial,
editor: {
get connection(): Connection {
Expand Down
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Prerelease

- Warn when using a blocklisted class in v4 ([#1310](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1310))
- Support class function hovers in Svelte and HTML `<script>` blocks ([#1311](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1311))

# 0.14.15

Expand Down