diff --git a/packages/tailwindcss-language-service/src/completionProvider.ts b/packages/tailwindcss-language-service/src/completionProvider.ts index 36e7676b..570ca535 100644 --- a/packages/tailwindcss-language-service/src/completionProvider.ts +++ b/packages/tailwindcss-language-service/src/completionProvider.ts @@ -13,7 +13,7 @@ import type { TextDocument } from 'vscode-languageserver-textdocument' import dlv from 'dlv' import removeMeta from './util/removeMeta' import { formatColor, getColor, getColorFromValue } from './util/color' -import { isHtmlContext, isHtmlDoc, isVueDoc } from './util/html' +import { isHtmlContext, isHtmlDoc } from './util/html' import { isCssContext } from './util/css' import { findLast, matchClassAttributes, matchClassFunctions } from './util/find' import { stringifyConfigValue, stringifyCss } from './util/stringify' diff --git a/packages/tailwindcss-language-service/src/util/find.test.ts b/packages/tailwindcss-language-service/src/util/find.test.ts index 9c65f23d..52fea620 100644 --- a/packages/tailwindcss-language-service/src/util/find.test.ts +++ b/packages/tailwindcss-language-service/src/util/find.test.ts @@ -4,7 +4,7 @@ import { findClassNameAtPosition, findHelperFunctionsInDocument, } from './find' -import { js, html, pug, createDocument, css } from './test-utils' +import { js, html, pug, createDocument, css, astro } from './test-utils' import type { Range } from 'vscode-languageserver-textdocument' const range = (startLine: number, startCol: number, endLine: number, endCol: number): Range => ({ @@ -1044,3 +1044,40 @@ test('Can find helper functions in CSS', async ({ expect }) => { }, ]) }) + +test('class functions work inside astro code fences', async ({ expect }) => { + let file = createDocument({ + name: 'file.astro', + lang: 'astro', + settings: { + tailwindCSS: { + classFunctions: ['clsx'], + }, + }, + content: astro` + --- + let x = clsx("relative flex bg-red-500") + --- +
+ `, + }) + + let classLists = await findClassListsInHtmlRange(file.state, file.doc, 'html') + + expect(classLists).toEqual([ + { + classList: 'relative flex bg-red-500', + range: { + start: { line: 3, character: 12 }, + end: { line: 3, character: 36 }, + }, + }, + { + classList: 'relative flex bg-red-500', + range: { + start: { line: 1, character: 14 }, + end: { line: 1, character: 38 }, + }, + }, + ]) +}) diff --git a/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts b/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts index 786cc2f7..d758c5b3 100644 --- a/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts +++ b/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts @@ -1,6 +1,6 @@ import type { Range } from 'vscode-languageserver' import type { TextDocument } from 'vscode-languageserver-textdocument' -import { isVueDoc, isHtmlDoc, isSvelteDoc } from './html' +import { isVueDoc, isHtmlDoc, isSvelteDoc, isAstroDoc } from './html' import type { State } from './state' import { indexToPosition } from './find' import { isJsDoc } from './js' @@ -134,8 +134,21 @@ let vueStates = { }, } +let astroStates = { + ...states, + main: { + frontmatterBlockStart: { match: /^[\s\n]*---/, push: 'frontmatterScript' }, + ...states.main, + }, + frontmatterScript: { + frontmatterBlockEnd: { match: /\s*---(?=\s)/, pop: 1 }, + ...text, + }, +} + let defaultLexer = moo.states(states) let vueLexer = moo.states(vueStates) +let astroLexer = moo.states(astroStates) let cache = new Cache