Skip to content

Commit e88d6dc

Browse files
Support class function hovers in Svelte and HTML <script> blocks (#1311)
When using the `tailwindCSS.classFunctions` setting we were handling completions correctly inside `<script>` tags but not class hovers. This is b/c the code path for the two is just… different. It'll get unified eventually (soon hopefully) but until then this is the bug fix to ensure that hovering inside something like `<script>let classes = clsx("flex relative")</script>` works
1 parent bd1fc8c commit e88d6dc

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

packages/tailwindcss-language-service/src/util/find.test.ts

+85-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test } from 'vitest'
2-
import { findClassListsInHtmlRange } from './find'
2+
import { findClassListsInHtmlRange, findClassNameAtPosition } from './find'
33
import { js, html, pug, createDocument } from './test-utils'
44

55
test('class regex works in astro', async ({ expect }) => {
@@ -791,3 +791,87 @@ test('classAttributes find class lists inside Vue bindings', async ({ expect })
791791
},
792792
])
793793
})
794+
795+
test('Can find class name inside JS/TS functions in <script> tags (HTML)', async ({ expect }) => {
796+
let file = createDocument({
797+
name: 'file.html',
798+
lang: 'html',
799+
settings: {
800+
tailwindCSS: {
801+
classFunctions: ['clsx'],
802+
},
803+
},
804+
content: html`
805+
<script>
806+
let classes = clsx('flex relative')
807+
</script>
808+
`,
809+
})
810+
811+
let className = await findClassNameAtPosition(file.state, file.doc, {
812+
line: 1,
813+
character: 23,
814+
})
815+
816+
expect(className).toEqual({
817+
className: 'flex',
818+
range: {
819+
start: { line: 1, character: 22 },
820+
end: { line: 1, character: 26 },
821+
},
822+
relativeRange: {
823+
start: { line: 0, character: 0 },
824+
end: { line: 0, character: 4 },
825+
},
826+
classList: {
827+
classList: 'flex relative',
828+
important: undefined,
829+
range: {
830+
start: { character: 22, line: 1 },
831+
end: { character: 35, line: 1 },
832+
},
833+
},
834+
})
835+
})
836+
837+
test('Can find class name inside JS/TS functions in <script> tags (Svelte)', async ({ expect }) => {
838+
let file = createDocument({
839+
name: 'file.svelte',
840+
lang: 'svelte',
841+
settings: {
842+
tailwindCSS: {
843+
classFunctions: ['clsx'],
844+
},
845+
},
846+
content: html`
847+
<script>
848+
let classes = clsx('flex relative')
849+
</script>
850+
`,
851+
})
852+
853+
let className = await findClassNameAtPosition(file.state, file.doc, {
854+
line: 1,
855+
character: 23,
856+
})
857+
858+
expect(className).toEqual({
859+
className: 'flex',
860+
range: {
861+
start: { line: 1, character: 22 },
862+
end: { line: 1, character: 26 },
863+
},
864+
relativeRange: {
865+
start: { line: 0, character: 0 },
866+
end: { line: 0, character: 4 },
867+
},
868+
classList: {
869+
classList: 'flex relative',
870+
important: undefined,
871+
range: {
872+
start: { character: 22, line: 1 },
873+
end: { character: 35, line: 1 },
874+
},
875+
},
876+
})
877+
})

packages/tailwindcss-language-service/src/util/find.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import lineColumn from 'line-column'
55
import { isCssContext, isCssDoc } from './css'
66
import { isHtmlContext, isVueDoc } from './html'
77
import { isWithinRange } from './isWithinRange'
8-
import { isJsxContext } from './js'
8+
import { isJsContext } from './js'
99
import { dedupeByRange, flatten } from './array'
1010
import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
1111
import { getLanguageBoundaries } from './getLanguageBoundaries'
@@ -526,7 +526,7 @@ export async function findClassNameAtPosition(
526526
classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
527527
} else if (isHtmlContext(state, doc, position)) {
528528
classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
529-
} else if (isJsxContext(state, doc, position)) {
529+
} else if (isJsContext(state, doc, position)) {
530530
classNames = await findClassNamesInRange(state, doc, searchRange, 'jsx')
531531
} else {
532532
classNames = await findClassNamesInRange(state, doc, searchRange)

packages/tailwindcss-language-service/src/util/state.ts

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export function createState(
228228
return {
229229
enabled: true,
230230
features: [],
231+
blocklist: [],
231232
...partial,
232233
editor: {
233234
get connection(): Connection {

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Prerelease
44

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

78
# 0.14.15
89

0 commit comments

Comments
 (0)