From 7e3b93dd874db71cd742007347075ef1324b88d6 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 10 Mar 2023 13:45:54 +0000 Subject: [PATCH] Fix IntelliSense inside Handlebars template scripts (#726) --- .../src/util/getLanguageBoundaries.ts | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts b/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts index 3e972e9..a542e8f 100644 --- a/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts +++ b/packages/tailwindcss-language-service/src/util/getLanguageBoundaries.ts @@ -7,7 +7,16 @@ import moo from 'moo' import Cache from 'tmp-cache' import { getTextWithoutComments } from './doc' -export type LanguageBoundary = { type: 'html' | 'js' | 'css' | string; range: Range } +export type LanguageBoundary = { type: 'html' | 'js' | 'css' | (string & {}); range: Range } + +let htmlScriptTypes = [ + // https://v3-migration.vuejs.org/breaking-changes/inline-template-attribute.html#option-1-use-script-tag + 'text/html', + // https://vuejs.org/guide/essentials/component-basics.html#dom-template-parsing-caveats + 'text/x-template', + // https://github.com/tailwindlabs/tailwindcss-intellisense/issues/722 + 'text/x-handlebars-template', +] let text = { text: { match: /[^]/, lineBreaks: true } } @@ -30,6 +39,8 @@ let states = { jsBlockEnd: { match: '/>', pop: 1 }, langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' }, langAttrStartSingle: { match: "lang='", push: 'langAttrSingle' }, + typeAttrStartDouble: { match: 'type="', push: 'typeAttrDouble' }, + typeAttrStartSingle: { match: "type='", push: 'typeAttrSingle' }, attrStartDouble: { match: '"', push: 'attrDouble' }, attrStartSingle: { match: "'", push: 'attrSingle' }, interp: { match: '{', push: 'interp' }, @@ -48,6 +59,14 @@ let states = { langAttrEnd: { match: "'", pop: 1 }, lang: { match: /[^']+/, lineBreaks: true }, }, + typeAttrDouble: { + langAttrEnd: { match: '"', pop: 1 }, + type: { match: /[^"]+/, lineBreaks: true }, + }, + typeAttrSingle: { + langAttrEnd: { match: "'", pop: 1 }, + type: { match: /[^']+/, lineBreaks: true }, + }, attrDouble: { attrEnd: { match: '"', pop: 1 }, ...text, @@ -156,6 +175,8 @@ export function getLanguageBoundaries( boundaries.push({ type: defaultType, 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)) { + boundaries[boundaries.length - 1].type = 'html' } } offset += token.text.length