tailwind-ctp-intellisense/packages/tailwindcss-language-service/src/util/css.ts

43 lines
964 B
TypeScript
Raw Normal View History

import type { TextDocument, Position } from 'vscode-languageserver'
2020-05-17 15:50:41 +00:00
import { isInsideTag, isVueDoc, isSvelteDoc, isHtmlDoc } from './html'
import { State } from './state'
2020-04-11 21:20:45 +00:00
export const CSS_LANGUAGES = [
'css',
'less',
'postcss',
'sass',
'scss',
'stylus',
2020-05-10 11:13:50 +00:00
'sugarss',
2020-04-11 21:20:45 +00:00
]
export function isCssDoc(state: State, doc: TextDocument): boolean {
const userCssLanguages = Object.keys(
state.editor.userLanguages
).filter((lang) => CSS_LANGUAGES.includes(state.editor.userLanguages[lang]))
return [...CSS_LANGUAGES, ...userCssLanguages].indexOf(doc.languageId) !== -1
2020-04-11 21:20:45 +00:00
}
2020-04-11 22:34:03 +00:00
export function isCssContext(
state: State,
doc: TextDocument,
position: Position
): boolean {
if (isCssDoc(state, doc)) {
2020-04-11 22:34:03 +00:00
return true
}
2020-05-17 15:50:41 +00:00
if (isHtmlDoc(state, doc) || isVueDoc(doc) || isSvelteDoc(doc)) {
2020-04-11 22:34:03 +00:00
let str = doc.getText({
start: { line: 0, character: 0 },
end: position,
})
return isInsideTag(str, ['style'])
}
return false
}