2020-10-08 15:20:54 +00:00
|
|
|
import type { TextDocument, Range, Position } from 'vscode-languageserver'
|
2021-05-04 11:40:50 +00:00
|
|
|
import { DocumentClassName, DocumentClassList, State, DocumentHelperFunction } from './state'
|
2020-04-17 17:59:19 +00:00
|
|
|
import lineColumn from 'line-column'
|
2020-05-24 17:32:25 +00:00
|
|
|
import { isCssContext, isCssDoc } from './css'
|
2021-05-04 11:41:13 +00:00
|
|
|
import { isHtmlContext } from './html'
|
2020-05-17 16:13:14 +00:00
|
|
|
import { isWithinRange } from './isWithinRange'
|
2022-03-02 17:16:35 +00:00
|
|
|
import { isJsxContext } from './js'
|
2020-10-08 15:20:54 +00:00
|
|
|
import { flatten } from './array'
|
2021-05-04 11:40:50 +00:00
|
|
|
import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
|
2020-06-15 10:24:05 +00:00
|
|
|
import { getLanguageBoundaries } from './getLanguageBoundaries'
|
2020-08-12 17:45:36 +00:00
|
|
|
import { resolveRange } from './resolveRange'
|
2022-04-13 16:10:47 +00:00
|
|
|
import dlv from 'dlv'
|
2022-07-06 15:40:29 +00:00
|
|
|
import { rangesEqual } from './rangesEqual'
|
2022-08-05 15:58:50 +00:00
|
|
|
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
|
2022-09-01 14:06:47 +00:00
|
|
|
import { getTextWithoutComments } from './doc'
|
2020-04-17 17:59:19 +00:00
|
|
|
|
2020-04-11 21:20:45 +00:00
|
|
|
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
|
|
|
|
let match: RegExpMatchArray
|
|
|
|
let matches: RegExpMatchArray[] = []
|
|
|
|
while ((match = re.exec(str)) !== null) {
|
|
|
|
matches.push({ ...match })
|
|
|
|
}
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
|
|
|
export function findLast(re: RegExp, str: string): RegExpMatchArray {
|
|
|
|
const matches = findAll(re, str)
|
|
|
|
if (matches.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return matches[matches.length - 1]
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:41:39 +00:00
|
|
|
export function getClassNamesInClassList({
|
|
|
|
classList,
|
|
|
|
range,
|
2020-06-17 17:34:53 +00:00
|
|
|
important,
|
2020-06-12 10:41:39 +00:00
|
|
|
}: DocumentClassList): DocumentClassName[] {
|
|
|
|
const parts = classList.split(/(\s+)/)
|
|
|
|
const names: DocumentClassName[] = []
|
|
|
|
let index = 0
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
if (i % 2 === 0) {
|
|
|
|
const start = indexToPosition(classList, index)
|
|
|
|
const end = indexToPosition(classList, index + parts[i].length)
|
|
|
|
names.push({
|
|
|
|
className: parts[i],
|
2020-06-17 17:34:53 +00:00
|
|
|
classList: {
|
|
|
|
classList,
|
|
|
|
range,
|
|
|
|
important,
|
|
|
|
},
|
|
|
|
relativeRange: {
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
},
|
2020-06-12 10:41:39 +00:00
|
|
|
range: {
|
|
|
|
start: {
|
|
|
|
line: range.start.line + start.line,
|
2021-05-04 11:40:50 +00:00
|
|
|
character: (end.line === 0 ? range.start.character : 0) + start.character,
|
2020-06-12 10:41:39 +00:00
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line: range.start.line + end.line,
|
2021-05-04 11:40:50 +00:00
|
|
|
character: (end.line === 0 ? range.start.character : 0) + end.character,
|
2020-06-12 10:41:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
index += parts[i].length
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
export async function findClassNamesInRange(
|
|
|
|
state: State,
|
2020-04-17 17:59:19 +00:00
|
|
|
doc: TextDocument,
|
2020-05-24 17:32:25 +00:00
|
|
|
range?: Range,
|
2022-09-01 14:06:47 +00:00
|
|
|
mode?: 'html' | 'css' | 'jsx',
|
2020-12-07 15:39:44 +00:00
|
|
|
includeCustom: boolean = true
|
|
|
|
): Promise<DocumentClassName[]> {
|
2021-05-04 11:40:50 +00:00
|
|
|
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
|
2022-04-13 21:24:02 +00:00
|
|
|
return flatten(classLists.map(getClassNamesInClassList))
|
2020-06-12 10:41:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
export async function findClassNamesInDocument(
|
2020-06-12 10:41:39 +00:00
|
|
|
state: State,
|
|
|
|
doc: TextDocument
|
2020-12-07 15:39:44 +00:00
|
|
|
): Promise<DocumentClassName[]> {
|
|
|
|
const classLists = await findClassListsInDocument(state, doc)
|
2022-04-13 21:24:02 +00:00
|
|
|
return flatten(classLists.map(getClassNamesInClassList))
|
2020-04-17 17:59:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 11:40:50 +00:00
|
|
|
export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
|
2022-09-01 14:06:47 +00:00
|
|
|
const text = getTextWithoutComments(doc, 'css', range)
|
2020-06-17 17:34:53 +00:00
|
|
|
const matches = findAll(
|
|
|
|
/(@apply\s+)(?<classList>[^;}]+?)(?<important>\s*!important)?\s*[;}]/g,
|
|
|
|
text
|
|
|
|
)
|
2020-05-24 17:32:25 +00:00
|
|
|
const globalStart: Position = range ? range.start : { line: 0, character: 0 }
|
2020-04-17 17:59:19 +00:00
|
|
|
|
|
|
|
return matches.map((match) => {
|
|
|
|
const start = indexToPosition(text, match.index + match[1].length)
|
2021-05-04 11:40:50 +00:00
|
|
|
const end = indexToPosition(text, match.index + match[1].length + match.groups.classList.length)
|
2020-04-17 17:59:19 +00:00
|
|
|
return {
|
|
|
|
classList: match.groups.classList,
|
2020-06-17 17:34:53 +00:00
|
|
|
important: Boolean(match.groups.important),
|
2020-04-17 17:59:19 +00:00
|
|
|
range: {
|
|
|
|
start: {
|
2020-05-24 17:32:25 +00:00
|
|
|
line: globalStart.line + start.line,
|
2021-05-04 11:40:50 +00:00
|
|
|
character: (end.line === 0 ? globalStart.character : 0) + start.character,
|
2020-04-17 17:59:19 +00:00
|
|
|
},
|
|
|
|
end: {
|
2020-05-24 17:32:25 +00:00
|
|
|
line: globalStart.line + end.line,
|
2021-05-04 11:40:50 +00:00
|
|
|
character: (end.line === 0 ? globalStart.character : 0) + end.character,
|
2020-04-17 17:59:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
async function findCustomClassLists(
|
|
|
|
state: State,
|
|
|
|
doc: TextDocument,
|
|
|
|
range?: Range
|
|
|
|
): Promise<DocumentClassList[]> {
|
2021-05-03 17:00:04 +00:00
|
|
|
const settings = await state.editor.getConfiguration(doc.uri)
|
2022-09-13 16:31:09 +00:00
|
|
|
const regexes = settings.tailwindCSS.experimental.classRegex
|
2020-12-07 15:39:44 +00:00
|
|
|
|
|
|
|
if (!Array.isArray(regexes) || regexes.length === 0) return []
|
|
|
|
|
|
|
|
const text = doc.getText(range)
|
|
|
|
const result: DocumentClassList[] = []
|
|
|
|
|
|
|
|
for (let i = 0; i < regexes.length; i++) {
|
|
|
|
try {
|
2022-08-05 15:58:50 +00:00
|
|
|
let [containerRegexString, classRegexString] = Array.isArray(regexes[i])
|
|
|
|
? regexes[i]
|
|
|
|
: [regexes[i]]
|
2020-12-07 15:39:44 +00:00
|
|
|
|
2022-08-05 15:58:50 +00:00
|
|
|
let containerRegex = new Regex(containerRegexString, 'g')
|
|
|
|
let containerMatch: ReturnType<Regex['exec']>
|
2020-12-07 15:39:44 +00:00
|
|
|
|
2022-08-05 15:58:50 +00:00
|
|
|
while ((containerMatch = containerRegex.exec(text)) !== null) {
|
2021-05-04 11:40:50 +00:00
|
|
|
const searchStart = doc.offsetAt(range?.start || { line: 0, character: 0 })
|
2022-08-05 15:58:50 +00:00
|
|
|
const matchStart = searchStart + containerMatch.index[1]
|
|
|
|
const matchEnd = matchStart + containerMatch[1].length
|
2020-12-07 15:39:44 +00:00
|
|
|
|
2022-08-05 15:58:50 +00:00
|
|
|
if (classRegexString) {
|
|
|
|
let classRegex = new Regex(classRegexString, 'g')
|
|
|
|
let classMatch: ReturnType<Regex['exec']>
|
2020-12-07 15:39:44 +00:00
|
|
|
|
2022-08-05 15:58:50 +00:00
|
|
|
while ((classMatch = classRegex.exec(containerMatch[1])) !== null) {
|
|
|
|
const classMatchStart = matchStart + classMatch.index[1]
|
|
|
|
const classMatchEnd = classMatchStart + classMatch[1].length
|
2020-12-07 15:39:44 +00:00
|
|
|
result.push({
|
2022-08-05 15:58:50 +00:00
|
|
|
classList: classMatch[1],
|
2020-12-07 15:39:44 +00:00
|
|
|
range: {
|
|
|
|
start: doc.positionAt(classMatchStart),
|
|
|
|
end: doc.positionAt(classMatchEnd),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push({
|
2022-08-05 15:58:50 +00:00
|
|
|
classList: containerMatch[1],
|
2020-12-07 15:39:44 +00:00
|
|
|
range: {
|
|
|
|
start: doc.positionAt(matchStart),
|
|
|
|
end: doc.positionAt(matchEnd),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (_) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-10-08 15:51:14 +00:00
|
|
|
export function matchClassAttributes(text: string, attributes: string[]): RegExpMatchArray[] {
|
|
|
|
const attrs = attributes.filter((x) => typeof x === 'string').flatMap((a) => [a, `\\[${a}\\]`])
|
|
|
|
const re = /(?:\s|:|\()(ATTRS)\s*=\s*['"`{]/
|
|
|
|
return findAll(new RegExp(re.source.replace('ATTRS', attrs.join('|')), 'gi'), text)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function findClassListsInHtmlRange(
|
|
|
|
state: State,
|
|
|
|
doc: TextDocument,
|
2022-09-01 14:06:47 +00:00
|
|
|
type: 'html' | 'js' | 'jsx',
|
2021-10-08 15:51:14 +00:00
|
|
|
range?: Range
|
2022-04-13 21:24:02 +00:00
|
|
|
): Promise<DocumentClassList[]> {
|
2022-09-01 14:06:47 +00:00
|
|
|
const text = getTextWithoutComments(doc, type, range)
|
2021-10-08 15:51:14 +00:00
|
|
|
|
|
|
|
const matches = matchClassAttributes(
|
|
|
|
text,
|
|
|
|
(await state.editor.getConfiguration(doc.uri)).tailwindCSS.classAttributes
|
|
|
|
)
|
|
|
|
|
2022-04-13 21:24:02 +00:00
|
|
|
const result: DocumentClassList[] = []
|
2020-05-17 16:13:14 +00:00
|
|
|
|
|
|
|
matches.forEach((match) => {
|
2020-06-29 18:06:14 +00:00
|
|
|
const subtext = text.substr(match.index + match[0].length - 1)
|
2020-05-17 16:13:14 +00:00
|
|
|
|
2020-06-12 11:30:12 +00:00
|
|
|
let lexer =
|
2021-10-08 15:51:14 +00:00
|
|
|
match[0][0] === ':' || (match[1].startsWith('[') && match[1].endsWith(']'))
|
2020-06-12 11:30:12 +00:00
|
|
|
? getComputedClassAttributeLexer()
|
|
|
|
: getClassAttributeLexer()
|
2020-05-17 16:13:14 +00:00
|
|
|
lexer.reset(subtext)
|
|
|
|
|
2022-04-13 21:24:02 +00:00
|
|
|
let classLists: { value: string; offset: number }[] = []
|
|
|
|
let token: moo.Token
|
2020-05-17 16:13:14 +00:00
|
|
|
let currentClassList: { value: string; offset: number }
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let token of lexer) {
|
2022-05-26 10:31:22 +00:00
|
|
|
if (token.type === 'classlist' || token.type.startsWith('arb')) {
|
2020-05-17 16:13:14 +00:00
|
|
|
if (currentClassList) {
|
|
|
|
currentClassList.value += token.value
|
|
|
|
} else {
|
|
|
|
currentClassList = {
|
|
|
|
value: token.value,
|
|
|
|
offset: token.offset,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (currentClassList) {
|
2022-04-13 21:24:02 +00:00
|
|
|
classLists.push({
|
|
|
|
value: currentClassList.value,
|
|
|
|
offset: currentClassList.offset,
|
|
|
|
})
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
|
|
|
currentClassList = undefined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (_) {}
|
|
|
|
|
|
|
|
if (currentClassList) {
|
2022-04-13 21:24:02 +00:00
|
|
|
classLists.push({
|
|
|
|
value: currentClassList.value,
|
|
|
|
offset: currentClassList.offset,
|
|
|
|
})
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result.push(
|
|
|
|
...classLists
|
2022-04-13 21:24:02 +00:00
|
|
|
.map(({ value, offset }) => {
|
|
|
|
if (value.trim() === '') {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const before = value.match(/^\s*/)
|
|
|
|
const beforeOffset = before === null ? 0 : before[0].length
|
|
|
|
const after = value.match(/\s*$/)
|
|
|
|
const afterOffset = after === null ? 0 : -after[0].length
|
|
|
|
|
|
|
|
const start = indexToPosition(
|
|
|
|
text,
|
|
|
|
match.index + match[0].length - 1 + offset + beforeOffset
|
|
|
|
)
|
|
|
|
const end = indexToPosition(
|
|
|
|
text,
|
|
|
|
match.index + match[0].length - 1 + offset + value.length + afterOffset
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
classList: value.substr(beforeOffset, value.length + afterOffset),
|
|
|
|
range: {
|
|
|
|
start: {
|
|
|
|
line: (range?.start.line || 0) + start.line,
|
|
|
|
character: (end.line === 0 ? range?.start.character || 0 : 0) + start.character,
|
|
|
|
},
|
|
|
|
end: {
|
|
|
|
line: (range?.start.line || 0) + end.line,
|
|
|
|
character: (end.line === 0 ? range?.start.character || 0 : 0) + end.character,
|
|
|
|
},
|
|
|
|
},
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter((x) => x !== null)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-07-06 15:40:29 +00:00
|
|
|
function dedupeClassLists(classLists: DocumentClassList[]): DocumentClassList[] {
|
|
|
|
return classLists.filter(
|
|
|
|
(classList, classListIndex) =>
|
|
|
|
classListIndex === classLists.findIndex((c) => rangesEqual(c.range, classList.range))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
export async function findClassListsInRange(
|
|
|
|
state: State,
|
2020-05-17 16:13:14 +00:00
|
|
|
doc: TextDocument,
|
2020-06-12 10:41:39 +00:00
|
|
|
range?: Range,
|
2022-09-01 14:06:47 +00:00
|
|
|
mode?: 'html' | 'css' | 'jsx',
|
2020-12-07 15:39:44 +00:00
|
|
|
includeCustom: boolean = true
|
2022-04-13 21:24:02 +00:00
|
|
|
): Promise<DocumentClassList[]> {
|
|
|
|
let classLists: DocumentClassList[]
|
2020-05-17 16:13:14 +00:00
|
|
|
if (mode === 'css') {
|
2020-12-07 15:39:44 +00:00
|
|
|
classLists = findClassListsInCssRange(doc, range)
|
|
|
|
} else {
|
2022-09-01 14:06:47 +00:00
|
|
|
classLists = await findClassListsInHtmlRange(state, doc, mode, range)
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
2022-07-06 15:40:29 +00:00
|
|
|
return dedupeClassLists([
|
|
|
|
...classLists,
|
|
|
|
...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
|
|
|
|
])
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
export async function findClassListsInDocument(
|
2020-05-24 17:32:25 +00:00
|
|
|
state: State,
|
|
|
|
doc: TextDocument
|
2022-04-13 21:24:02 +00:00
|
|
|
): Promise<DocumentClassList[]> {
|
2020-05-24 17:32:25 +00:00
|
|
|
if (isCssDoc(state, doc)) {
|
|
|
|
return findClassListsInCssRange(doc)
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:24:05 +00:00
|
|
|
let boundaries = getLanguageBoundaries(state, doc)
|
|
|
|
if (!boundaries) return []
|
2020-05-24 17:32:25 +00:00
|
|
|
|
2022-07-06 15:40:29 +00:00
|
|
|
return dedupeClassLists(
|
|
|
|
flatten([
|
|
|
|
...(await Promise.all(
|
|
|
|
boundaries
|
|
|
|
.filter((b) => b.type === 'html' || b.type === 'jsx')
|
2022-09-01 14:06:47 +00:00
|
|
|
.map(({ type, range }) =>
|
|
|
|
findClassListsInHtmlRange(state, doc, type === 'html' ? 'html' : 'jsx', range)
|
|
|
|
)
|
2022-07-06 15:40:29 +00:00
|
|
|
)),
|
|
|
|
...boundaries
|
|
|
|
.filter((b) => b.type === 'css')
|
|
|
|
.map(({ range }) => findClassListsInCssRange(doc, range)),
|
|
|
|
await findCustomClassLists(state, doc),
|
|
|
|
])
|
|
|
|
)
|
2020-05-24 17:32:25 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 17:45:36 +00:00
|
|
|
export function findHelperFunctionsInDocument(
|
|
|
|
state: State,
|
|
|
|
doc: TextDocument
|
|
|
|
): DocumentHelperFunction[] {
|
|
|
|
if (isCssDoc(state, doc)) {
|
|
|
|
return findHelperFunctionsInRange(doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
let boundaries = getLanguageBoundaries(state, doc)
|
|
|
|
if (!boundaries) return []
|
|
|
|
|
2022-03-02 17:16:35 +00:00
|
|
|
return flatten(
|
|
|
|
boundaries
|
|
|
|
.filter((b) => b.type === 'css')
|
|
|
|
.map(({ range }) => findHelperFunctionsInRange(doc, range))
|
|
|
|
)
|
2020-08-12 17:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function findHelperFunctionsInRange(
|
|
|
|
doc: TextDocument,
|
|
|
|
range?: Range
|
|
|
|
): DocumentHelperFunction[] {
|
2022-09-01 14:06:47 +00:00
|
|
|
const text = getTextWithoutComments(doc, 'css', range)
|
2022-10-17 16:56:00 +00:00
|
|
|
let matches = findAll(
|
|
|
|
/(?<prefix>\s|^)(?<helper>config|theme)(?<innerPrefix>\(\s*)(?<path>[^)]*?)\s*\)/g,
|
2020-08-12 17:45:36 +00:00
|
|
|
text
|
|
|
|
)
|
|
|
|
|
|
|
|
return matches.map((match) => {
|
2022-10-17 16:56:00 +00:00
|
|
|
let quotesBefore = ''
|
|
|
|
let path = match.groups.path.replace(/['"]+$/, '').replace(/^['"]+/, (m) => {
|
|
|
|
quotesBefore = m
|
|
|
|
return ''
|
|
|
|
})
|
|
|
|
let matches = path.match(/^([^\s]+)(?![^\[]*\])(?:\s*\/\s*([^\/\s]+))$/)
|
|
|
|
if (matches) {
|
|
|
|
path = matches[1]
|
|
|
|
}
|
|
|
|
path = path.replace(/['"]*\s*$/, '')
|
|
|
|
|
|
|
|
let startIndex =
|
|
|
|
match.index +
|
|
|
|
match.groups.prefix.length +
|
|
|
|
match.groups.helper.length +
|
|
|
|
match.groups.innerPrefix.length
|
|
|
|
|
2020-08-12 17:45:36 +00:00
|
|
|
return {
|
|
|
|
helper: match.groups.helper === 'theme' ? 'theme' : 'config',
|
2022-10-17 16:56:00 +00:00
|
|
|
path,
|
|
|
|
ranges: {
|
|
|
|
full: resolveRange(
|
|
|
|
{
|
|
|
|
start: indexToPosition(text, startIndex),
|
|
|
|
end: indexToPosition(text, startIndex + match.groups.path.length),
|
|
|
|
},
|
|
|
|
range
|
|
|
|
),
|
|
|
|
path: resolveRange(
|
|
|
|
{
|
|
|
|
start: indexToPosition(text, startIndex + quotesBefore.length),
|
|
|
|
end: indexToPosition(text, startIndex + quotesBefore.length + path.length),
|
|
|
|
},
|
|
|
|
range
|
|
|
|
),
|
|
|
|
},
|
2020-08-12 17:45:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:41:49 +00:00
|
|
|
export function indexToPosition(str: string, index: number): Position {
|
2020-04-17 17:59:19 +00:00
|
|
|
const { line, col } = lineColumn(str + '\n', index)
|
|
|
|
return { line: line - 1, character: col - 1 }
|
|
|
|
}
|
2020-05-17 16:13:14 +00:00
|
|
|
|
2020-12-07 15:39:44 +00:00
|
|
|
export async function findClassNameAtPosition(
|
2020-05-17 16:13:14 +00:00
|
|
|
state: State,
|
|
|
|
doc: TextDocument,
|
|
|
|
position: Position
|
2020-12-07 15:39:44 +00:00
|
|
|
): Promise<DocumentClassName> {
|
2020-05-17 16:13:14 +00:00
|
|
|
let classNames = []
|
2021-08-13 16:59:14 +00:00
|
|
|
const positionOffset = doc.offsetAt(position)
|
|
|
|
const searchRange: Range = {
|
2021-10-08 16:36:17 +00:00
|
|
|
start: doc.positionAt(Math.max(0, positionOffset - 1000)),
|
|
|
|
end: doc.positionAt(positionOffset + 1000),
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isCssContext(state, doc, position)) {
|
2020-12-07 15:39:44 +00:00
|
|
|
classNames = await findClassNamesInRange(state, doc, searchRange, 'css')
|
2022-09-01 14:06:47 +00:00
|
|
|
} else if (isHtmlContext(state, doc, position)) {
|
2020-12-07 15:39:44 +00:00
|
|
|
classNames = await findClassNamesInRange(state, doc, searchRange, 'html')
|
2022-09-01 14:06:47 +00:00
|
|
|
} else if (isJsxContext(state, doc, position)) {
|
|
|
|
classNames = await findClassNamesInRange(state, doc, searchRange, 'jsx')
|
2020-05-17 16:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (classNames.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-05-04 11:40:50 +00:00
|
|
|
const className = classNames.find(({ range }) => isWithinRange(position, range))
|
2020-05-17 16:13:14 +00:00
|
|
|
|
|
|
|
if (!className) return null
|
|
|
|
|
|
|
|
return className
|
|
|
|
}
|