Skip to content

Commit fdc0f5b

Browse files
committed
Add support for styled-jsx / CSS in JS syntax
1 parent d49a90b commit fdc0f5b

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

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

+19-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ let htmlScriptTypes = [
2020
]
2121

2222
let text = { text: { match: /[^]/, lineBreaks: true } }
23+
let styledCssBlockStart = { match: /css(?:\.resolve|\.global)?`/, push: 'styledCssBlock' }
2324

2425
let states = {
2526
main: {
2627
cssBlockStart: { match: /<style(?=[>\s])/, push: 'cssBlock' },
28+
cssStyledBlockStart: styledCssBlockStart,
2729
jsBlockStart: { match: '<script', push: 'jsBlock' },
2830
...text,
2931
},
@@ -35,7 +37,13 @@ let states = {
3537
interp: { match: '{', push: 'interp' },
3638
...text,
3739
},
40+
styledCssBlock: {
41+
cssBlockEnd: { match: '`', pop: 1 },
42+
jsBlockStart: { match: '${', push: 'jsInterp' },
43+
...text,
44+
},
3845
jsBlock: {
46+
cssStyledBlockStart: styledCssBlockStart,
3947
scriptStart: { match: '>', next: 'script' },
4048
jsBlockEnd: { match: '/>', pop: 1 },
4149
langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' },
@@ -47,6 +55,12 @@ let states = {
4755
interp: { match: '{', push: 'interp' },
4856
...text,
4957
},
58+
jsInterp: {
59+
cssStyledBlockStart: styledCssBlockStart,
60+
interp: { match: '{', push: 'jsInterp' },
61+
jsBlockEnd: { match: '}', pop: 1 },
62+
...text,
63+
},
5064
interp: {
5165
interp: { match: '{', push: 'interp' },
5266
end: { match: '}', pop: 1 },
@@ -154,7 +168,7 @@ export function getLanguageBoundaries(
154168
let lexer = defaultType === 'none' ? vueLexer : defaultLexer
155169
lexer.reset(text)
156170

157-
let type = defaultType
171+
let types = [defaultType]
158172
let boundaries: LanguageBoundary[] = [
159173
{ type: defaultType, range: { start: { line: 0, character: 0 }, end: undefined } },
160174
]
@@ -168,12 +182,13 @@ export function getLanguageBoundaries(
168182
if (!boundaries[boundaries.length - 1].range.end) {
169183
boundaries[boundaries.length - 1].range.end = position
170184
}
171-
type = token.type.replace(/BlockStart$/, '')
172-
boundaries.push({ type, range: { start: position, end: undefined } })
185+
types.push(token.type.replace(/(?:[A-Z].+?)?BlockStart$/, ''))
186+
boundaries.push({ type: types[types.length - 1], range: { start: position, end: undefined } })
173187
} else if (token.type.endsWith('BlockEnd')) {
174188
let position = indexToPosition(text, offset)
189+
types.pop()
175190
boundaries[boundaries.length - 1].range.end = position
176-
boundaries.push({ type: defaultType, range: { start: position, end: undefined } })
191+
boundaries.push({ type: types[types.length - 1], range: { start: position, end: undefined } })
177192
} else if (token.type === 'lang') {
178193
boundaries[boundaries.length - 1].type = token.text
179194
} else if (token.type === 'type' && htmlScriptTypes.includes(token.text)) {

packages/vscode-tailwindcss/package.json

+14-2
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,27 @@
8383
"source.css",
8484
"source.vue",
8585
"source.svelte",
86+
"source.tsx",
87+
"source.jsx",
8688
"text.html"
8789
]
8890
},
8991
{
9092
"scopeName": "tailwindcss.at-rules.scss.injection",
9193
"path": "./syntaxes/at-rules.scss.tmLanguage.json",
9294
"injectTo": [
93-
"source.css.scss"
95+
"source.css.scss",
96+
"source.tsx",
97+
"source.jsx"
9498
]
9599
},
96100
{
97101
"scopeName": "tailwindcss.at-rules.postcss.injection",
98102
"path": "./syntaxes/at-rules.postcss.tmLanguage.json",
99103
"injectTo": [
100-
"source.css.postcss"
104+
"source.css.postcss",
105+
"source.tsx",
106+
"source.jsx"
101107
]
102108
},
103109
{
@@ -108,6 +114,8 @@
108114
"source.css.postcss",
109115
"source.vue",
110116
"source.svelte",
117+
"source.tsx",
118+
"source.jsx",
111119
"text.html"
112120
]
113121
},
@@ -119,6 +127,8 @@
119127
"source.css.postcss",
120128
"source.vue",
121129
"source.svelte",
130+
"source.tsx",
131+
"source.jsx",
122132
"text.html"
123133
]
124134
},
@@ -130,6 +140,8 @@
130140
"source.css.postcss",
131141
"source.vue",
132142
"source.svelte",
143+
"source.tsx",
144+
"source.jsx",
133145
"text.html"
134146
]
135147
}

0 commit comments

Comments
 (0)