Fix IntelliSense inside Handlebars template scripts (#726)

master
Brad Cornes 2023-03-10 13:45:54 +00:00 committed by GitHub
parent 8a2df180a8
commit 7e3b93dd87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -7,7 +7,16 @@ import moo from 'moo'
import Cache from 'tmp-cache' import Cache from 'tmp-cache'
import { getTextWithoutComments } from './doc' 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 } } let text = { text: { match: /[^]/, lineBreaks: true } }
@ -30,6 +39,8 @@ let states = {
jsBlockEnd: { match: '/>', pop: 1 }, jsBlockEnd: { match: '/>', pop: 1 },
langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' }, langAttrStartDouble: { match: 'lang="', push: 'langAttrDouble' },
langAttrStartSingle: { match: "lang='", push: 'langAttrSingle' }, langAttrStartSingle: { match: "lang='", push: 'langAttrSingle' },
typeAttrStartDouble: { match: 'type="', push: 'typeAttrDouble' },
typeAttrStartSingle: { match: "type='", push: 'typeAttrSingle' },
attrStartDouble: { match: '"', push: 'attrDouble' }, attrStartDouble: { match: '"', push: 'attrDouble' },
attrStartSingle: { match: "'", push: 'attrSingle' }, attrStartSingle: { match: "'", push: 'attrSingle' },
interp: { match: '{', push: 'interp' }, interp: { match: '{', push: 'interp' },
@ -48,6 +59,14 @@ let states = {
langAttrEnd: { match: "'", pop: 1 }, langAttrEnd: { match: "'", pop: 1 },
lang: { match: /[^']+/, lineBreaks: true }, lang: { match: /[^']+/, lineBreaks: true },
}, },
typeAttrDouble: {
langAttrEnd: { match: '"', pop: 1 },
type: { match: /[^"]+/, lineBreaks: true },
},
typeAttrSingle: {
langAttrEnd: { match: "'", pop: 1 },
type: { match: /[^']+/, lineBreaks: true },
},
attrDouble: { attrDouble: {
attrEnd: { match: '"', pop: 1 }, attrEnd: { match: '"', pop: 1 },
...text, ...text,
@ -156,6 +175,8 @@ export function getLanguageBoundaries(
boundaries.push({ type: defaultType, range: { start: position, end: undefined } }) boundaries.push({ type: defaultType, range: { start: position, end: undefined } })
} else if (token.type === 'lang') { } else if (token.type === 'lang') {
boundaries[boundaries.length - 1].type = token.text 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 offset += token.text.length