Skip to content

Commit

Permalink
Add support for styled-jsx / CSS in JS syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegal committed Sep 28, 2023
1 parent d49a90b commit 5f4c5f2
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 66 deletions.
126 changes: 66 additions & 60 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -20,10 +20,12 @@ let htmlScriptTypes = [
]

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

let states = {
main: {
cssBlockStart: { match: /<style(?=[>\s])/, push: 'cssBlock' },
cssStyledBlockStart: styledCssBlockStart,
jsBlockStart: { match: '<script', push: 'jsBlock' },
...text,
},
Expand All @@ -35,7 +37,13 @@ let states = {
interp: { match: '{', push: 'interp' },
...text,
},
styledCssBlock: {
cssBlockEnd: { match: '`', pop: 1 },
jsBlockStart: { match: '${', push: 'jsInterp' },
...text,
},
jsBlock: {
cssStyledBlockStart: styledCssBlockStart,
scriptStart: { match: '>', next: 'script' },
jsBlockEnd: { match: '/>', pop: 1 },
langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' },
Expand All @@ -47,6 +55,12 @@ let states = {
interp: { match: '{', push: 'interp' },
...text,
},
jsInterp: {
cssStyledBlockStart: styledCssBlockStart,
interp: { match: '{', push: 'jsInterp' },
jsBlockEnd: { match: '}', pop: 1 },
...text,
},
interp: {
interp: { match: '{', push: 'interp' },
end: { match: '}', pop: 1 },
Expand Down Expand Up @@ -154,7 +168,7 @@ export function getLanguageBoundaries(
let lexer = defaultType === 'none' ? vueLexer : defaultLexer
lexer.reset(text)

let type = defaultType
let types = [defaultType]
let boundaries: LanguageBoundary[] = [
{ type: defaultType, range: { start: { line: 0, character: 0 }, end: undefined } },
]
Expand All @@ -168,12 +182,13 @@ export function getLanguageBoundaries(
if (!boundaries[boundaries.length - 1].range.end) {
boundaries[boundaries.length - 1].range.end = position
}
type = token.type.replace(/BlockStart$/, '')
boundaries.push({ type, range: { start: position, end: undefined } })
types.push(token.type.replace(/(?:[A-Z].+?)?BlockStart$/, ''))
boundaries.push({ type: types[types.length - 1], range: { start: position, end: undefined } })
} else if (token.type.endsWith('BlockEnd')) {
let position = indexToPosition(text, offset)
types.pop()
boundaries[boundaries.length - 1].range.end = position
boundaries.push({ type: defaultType, range: { start: position, end: undefined } })
boundaries.push({ type: types[types.length - 1], range: { start: position, end: undefined } })
} else if (token.type === 'lang') {
boundaries[boundaries.length - 1].type = token.text
} else if (token.type === 'type' && htmlScriptTypes.includes(token.text)) {
Expand Down
16 changes: 14 additions & 2 deletions packages/vscode-tailwindcss/package.json
Expand Up @@ -83,21 +83,27 @@
"source.css",
"source.vue",
"source.svelte",
"source.tsx",
"source.jsx",
"text.html"
]
},
{
"scopeName": "tailwindcss.at-rules.scss.injection",
"path": "./syntaxes/at-rules.scss.tmLanguage.json",
"injectTo": [
"source.css.scss"
"source.css.scss",
"source.tsx",
"source.jsx"
]
},
{
"scopeName": "tailwindcss.at-rules.postcss.injection",
"path": "./syntaxes/at-rules.postcss.tmLanguage.json",
"injectTo": [
"source.css.postcss"
"source.css.postcss",
"source.tsx",
"source.jsx"
]
},
{
Expand All @@ -108,6 +114,8 @@
"source.css.postcss",
"source.vue",
"source.svelte",
"source.tsx",
"source.jsx",
"text.html"
]
},
Expand All @@ -119,6 +127,8 @@
"source.css.postcss",
"source.vue",
"source.svelte",
"source.tsx",
"source.jsx",
"text.html"
]
},
Expand All @@ -130,6 +140,8 @@
"source.css.postcss",
"source.vue",
"source.svelte",
"source.tsx",
"source.jsx",
"text.html"
]
}
Expand Down

0 comments on commit 5f4c5f2

Please sign in to comment.