tailwind-ctp-intellisense @master -
refs -
log -
-
https://git.jolheiser.com/tailwind-ctp-intellisense.git
Signature
-----BEGIN PGP SIGNATURE-----
wsBcBAABCAAQBQJiIjM4CRBK7hj4Ov3rIwAAZkcIAKSqXRVsqhDX4wr46GCYokRC
jd0B7VhSDLdzqLf9AQyZuv8fapwNuD/XD448iOtIApUEHCuWezlsBkyHQdqbH0DE
+mH7LCIsHkzpY9j2UwsLqu/hi34jtunf5Rzm564iHHe7BU3fUXUSrAg1zSvx0MWF
TIG8Fb0Wn/QowzskqFhmVK9qZwD8Awbz+FbP0og5fC3mnJzc2W0JvfYZhf0qTVth
nfXZ1xqjPApsyf6WzVii6JIz3B67zYgHHUf1gBlpfA7Kgp4cZiPlJifoLu72PDCg
1b9tlwI8HPgM9mY+v9WBbjaNuPt7Unz07V4a5k+gDb+R+m/v4AhRih5fYt13bDk=
=QQDj
-----END PGP SIGNATURE-----
diff --git a/packages/tailwindcss-language-service/src/util/find.ts b/packages/tailwindcss-language-service/src/util/find.ts
index b52d6855d9601da03d4f73c625daf512b482e902..847191039fac5c6cf04b1a2d99792709ffc395e3 100644
--- a/packages/tailwindcss-language-service/src/util/find.ts
+++ b/packages/tailwindcss-language-service/src/util/find.ts
@@ -77,7 +77,15 @@ mode?: 'html' | 'css',
includeCustom: boolean = true
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
- return flatten(classLists.map(getClassNamesInClassList))
+ return flatten(
+ classLists.flatMap((classList) => {
+ if (Array.isArray(classList)) {
+ return classList.map(getClassNamesInClassList)
+ } else {
+ return [getClassNamesInClassList(classList)]
+ }
+ })
+ )
}
export async function findClassNamesInDocument(
@@ -85,7 +93,15 @@ state: State,
doc: TextDocument
): Promise<DocumentClassName[]> {
const classLists = await findClassListsInDocument(state, doc)
- return flatten(classLists.map(getClassNamesInClassList))
+ return flatten(
+ classLists.flatMap((classList) => {
+ if (Array.isArray(classList)) {
+ return classList.map(getClassNamesInClassList)
+ } else {
+ return [getClassNamesInClassList(classList)]
+ }
+ })
+ )
}
export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
@@ -182,7 +198,7 @@ export async function findClassListsInHtmlRange(
state: State,
doc: TextDocument,
range?: Range
-): Promise<DocumentClassList[]> {
+): Promise<Array<DocumentClassList | DocumentClassList[]>> {
const text = doc.getText(range)
const matches = matchClassAttributes(
@@ -190,7 +206,7 @@ text,
(await state.editor.getConfiguration(doc.uri)).tailwindCSS.classAttributes
)
- const result: DocumentClassList[] = []
+ const result: Array<DocumentClassList | DocumentClassList[]> = []
matches.forEach((match) => {
const subtext = text.substr(match.index + match[0].length - 1)
@@ -201,9 +217,11 @@ ? getComputedClassAttributeLexer()
: getClassAttributeLexer()
lexer.reset(subtext)
- let classLists: { value: string; offset: number }[] = []
- let token: moo.Token
+ let classLists: Array<{ value: string; offset: number } | { value: string; offset: number }[]> =
+ []
+ let rootClassList: { value: string; offset: number }[] = []
let currentClassList: { value: string; offset: number }
+ let depth = 0
try {
for (let token of lexer) {
@@ -218,56 +236,53 @@ }
}
} else {
if (currentClassList) {
- classLists.push({
- value: currentClassList.value,
- offset: currentClassList.offset,
- })
+ if (depth === 0) {
+ rootClassList.push({
+ value: currentClassList.value,
+ offset: currentClassList.offset,
+ })
+ } else {
+ classLists.push({
+ value: currentClassList.value,
+ offset: currentClassList.offset,
+ })
+ }
}
currentClassList = undefined
}
+ if (token.type === 'lbrace') {
+ depth += 1
+ } else if (token.type === 'rbrace') {
+ depth -= 1
+ }
}
} catch (_) {}
if (currentClassList) {
- classLists.push({
- value: currentClassList.value,
- offset: currentClassList.offset,
- })
+ if (depth === 0) {
+ rootClassList.push({
+ value: currentClassList.value,
+ offset: currentClassList.offset,
+ })
+ } else {
+ classLists.push({
+ value: currentClassList.value,
+ offset: currentClassList.offset,
+ })
+ }
}
+ classLists.push(rootClassList)
+
result.push(
...classLists
- .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,
- },
- },
+ .map((classList) => {
+ if (Array.isArray(classList)) {
+ return classList
+ .map((classList) => resolveClassList(classList, text, match, range))
+ .filter((x) => x !== null)
+ } else {
+ return resolveClassList(classList, text, match, range)
}
})
.filter((x) => x !== null)
@@ -277,14 +292,51 @@
return result
}
+function resolveClassList(
+ classList: { value: string; offset: number },
+ text: string,
+ match: RegExpMatchArray,
+ range?: Range
+): DocumentClassList {
+ let { value, offset } = classList
+ 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,
+ },
+ },
+ }
+}
+
export async function findClassListsInRange(
state: State,
doc: TextDocument,
range?: Range,
mode?: 'html' | 'css',
includeCustom: boolean = true
-): Promise<DocumentClassList[]> {
- let classLists: DocumentClassList[]
+): Promise<Array<DocumentClassList | DocumentClassList[]>> {
+ let classLists: Array<DocumentClassList | DocumentClassList[]>
if (mode === 'css') {
classLists = findClassListsInCssRange(doc, range)
} else {
@@ -296,7 +348,7 @@
export async function findClassListsInDocument(
state: State,
doc: TextDocument
-): Promise<DocumentClassList[]> {
+): Promise<Array<DocumentClassList | DocumentClassList[]>> {
if (isCssDoc(state, doc)) {
return findClassListsInCssRange(doc)
}