Skip to content

Commit 183cc2f

Browse files
authored
Revert "Improve conflict diagnostics (#503)" (#525)
This reverts commit ddfaea2.
1 parent 055b890 commit 183cc2f

File tree

4 files changed

+51
-105
lines changed

4 files changed

+51
-105
lines changed

packages/tailwindcss-language-service/src/diagnostics/getCssConflictDiagnostics.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ export async function getCssConflictDiagnostics(
2020
const classLists = await findClassListsInDocument(state, document)
2121

2222
classLists.forEach((classList) => {
23-
const classNames = Array.isArray(classList)
24-
? classList.flatMap(getClassNamesInClassList)
25-
: getClassNamesInClassList(classList)
23+
const classNames = getClassNamesInClassList(classList)
2624

2725
classNames.forEach((className, index) => {
2826
if (state.jit) {

packages/tailwindcss-language-service/src/diagnostics/getRecommendedVariantOrderDiagnostics.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function getRecommendedVariantOrderDiagnostics(
2222
let diagnostics: RecommendedVariantOrderDiagnostic[] = []
2323
const classLists = await findClassListsInDocument(state, document)
2424

25-
classLists.flat().forEach((classList) => {
25+
classLists.forEach((classList) => {
2626
const classNames = getClassNamesInClassList(classList)
2727
classNames.forEach((className) => {
2828
let { rules } = jit.generateRules(state, [className.className])

packages/tailwindcss-language-service/src/documentColorProvider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export async function getDocumentColors(
2020
if (settings.tailwindCSS.colorDecorators === false) return colors
2121

2222
let classLists = await findClassListsInDocument(state, document)
23-
classLists.flat().forEach((classList) => {
23+
classLists.forEach((classList) => {
2424
let classNames = getClassNamesInClassList(classList)
2525
classNames.forEach((className) => {
2626
let color = getColor(state, className.className)

packages/tailwindcss-language-service/src/util/find.ts

+48-100
Original file line numberDiff line numberDiff line change
@@ -77,31 +77,15 @@ export async function findClassNamesInRange(
7777
includeCustom: boolean = true
7878
): Promise<DocumentClassName[]> {
7979
const classLists = await findClassListsInRange(state, doc, range, mode, includeCustom)
80-
return flatten(
81-
classLists.flatMap((classList) => {
82-
if (Array.isArray(classList)) {
83-
return classList.map(getClassNamesInClassList)
84-
} else {
85-
return [getClassNamesInClassList(classList)]
86-
}
87-
})
88-
)
80+
return flatten(classLists.map(getClassNamesInClassList))
8981
}
9082

9183
export async function findClassNamesInDocument(
9284
state: State,
9385
doc: TextDocument
9486
): Promise<DocumentClassName[]> {
9587
const classLists = await findClassListsInDocument(state, doc)
96-
return flatten(
97-
classLists.flatMap((classList) => {
98-
if (Array.isArray(classList)) {
99-
return classList.map(getClassNamesInClassList)
100-
} else {
101-
return [getClassNamesInClassList(classList)]
102-
}
103-
})
104-
)
88+
return flatten(classLists.map(getClassNamesInClassList))
10589
}
10690

10791
export function findClassListsInCssRange(doc: TextDocument, range?: Range): DocumentClassList[] {
@@ -198,15 +182,15 @@ export async function findClassListsInHtmlRange(
198182
state: State,
199183
doc: TextDocument,
200184
range?: Range
201-
): Promise<Array<DocumentClassList | DocumentClassList[]>> {
185+
): Promise<DocumentClassList[]> {
202186
const text = doc.getText(range)
203187

204188
const matches = matchClassAttributes(
205189
text,
206190
(await state.editor.getConfiguration(doc.uri)).tailwindCSS.classAttributes
207191
)
208192

209-
const result: Array<DocumentClassList | DocumentClassList[]> = []
193+
const result: DocumentClassList[] = []
210194

211195
matches.forEach((match) => {
212196
const subtext = text.substr(match.index + match[0].length - 1)
@@ -217,11 +201,9 @@ export async function findClassListsInHtmlRange(
217201
: getClassAttributeLexer()
218202
lexer.reset(subtext)
219203

220-
let classLists: Array<{ value: string; offset: number } | { value: string; offset: number }[]> =
221-
[]
222-
let rootClassList: { value: string; offset: number }[] = []
204+
let classLists: { value: string; offset: number }[] = []
205+
let token: moo.Token
223206
let currentClassList: { value: string; offset: number }
224-
let depth = 0
225207

226208
try {
227209
for (let token of lexer) {
@@ -236,53 +218,56 @@ export async function findClassListsInHtmlRange(
236218
}
237219
} else {
238220
if (currentClassList) {
239-
if (depth === 0) {
240-
rootClassList.push({
241-
value: currentClassList.value,
242-
offset: currentClassList.offset,
243-
})
244-
} else {
245-
classLists.push({
246-
value: currentClassList.value,
247-
offset: currentClassList.offset,
248-
})
249-
}
221+
classLists.push({
222+
value: currentClassList.value,
223+
offset: currentClassList.offset,
224+
})
250225
}
251226
currentClassList = undefined
252227
}
253-
if (token.type === 'lbrace') {
254-
depth += 1
255-
} else if (token.type === 'rbrace') {
256-
depth -= 1
257-
}
258228
}
259229
} catch (_) {}
260230

261231
if (currentClassList) {
262-
if (depth === 0) {
263-
rootClassList.push({
264-
value: currentClassList.value,
265-
offset: currentClassList.offset,
266-
})
267-
} else {
268-
classLists.push({
269-
value: currentClassList.value,
270-
offset: currentClassList.offset,
271-
})
272-
}
232+
classLists.push({
233+
value: currentClassList.value,
234+
offset: currentClassList.offset,
235+
})
273236
}
274237

275-
classLists.push(rootClassList)
276-
277238
result.push(
278239
...classLists
279-
.map((classList) => {
280-
if (Array.isArray(classList)) {
281-
return classList
282-
.map((classList) => resolveClassList(classList, text, match, range))
283-
.filter((x) => x !== null)
284-
} else {
285-
return resolveClassList(classList, text, match, range)
240+
.map(({ value, offset }) => {
241+
if (value.trim() === '') {
242+
return null
243+
}
244+
245+
const before = value.match(/^\s*/)
246+
const beforeOffset = before === null ? 0 : before[0].length
247+
const after = value.match(/\s*$/)
248+
const afterOffset = after === null ? 0 : -after[0].length
249+
250+
const start = indexToPosition(
251+
text,
252+
match.index + match[0].length - 1 + offset + beforeOffset
253+
)
254+
const end = indexToPosition(
255+
text,
256+
match.index + match[0].length - 1 + offset + value.length + afterOffset
257+
)
258+
259+
return {
260+
classList: value.substr(beforeOffset, value.length + afterOffset),
261+
range: {
262+
start: {
263+
line: (range?.start.line || 0) + start.line,
264+
character: (end.line === 0 ? range?.start.character || 0 : 0) + start.character,
265+
},
266+
end: {
267+
line: (range?.start.line || 0) + end.line,
268+
character: (end.line === 0 ? range?.start.character || 0 : 0) + end.character,
269+
},
270+
},
286271
}
287272
})
288273
.filter((x) => x !== null)
@@ -292,51 +277,14 @@ export async function findClassListsInHtmlRange(
292277
return result
293278
}
294279

295-
function resolveClassList(
296-
classList: { value: string; offset: number },
297-
text: string,
298-
match: RegExpMatchArray,
299-
range?: Range
300-
): DocumentClassList {
301-
let { value, offset } = classList
302-
if (value.trim() === '') {
303-
return null
304-
}
305-
306-
const before = value.match(/^\s*/)
307-
const beforeOffset = before === null ? 0 : before[0].length
308-
const after = value.match(/\s*$/)
309-
const afterOffset = after === null ? 0 : -after[0].length
310-
311-
const start = indexToPosition(text, match.index + match[0].length - 1 + offset + beforeOffset)
312-
const end = indexToPosition(
313-
text,
314-
match.index + match[0].length - 1 + offset + value.length + afterOffset
315-
)
316-
317-
return {
318-
classList: value.substr(beforeOffset, value.length + afterOffset),
319-
range: {
320-
start: {
321-
line: (range?.start.line || 0) + start.line,
322-
character: (end.line === 0 ? range?.start.character || 0 : 0) + start.character,
323-
},
324-
end: {
325-
line: (range?.start.line || 0) + end.line,
326-
character: (end.line === 0 ? range?.start.character || 0 : 0) + end.character,
327-
},
328-
},
329-
}
330-
}
331-
332280
export async function findClassListsInRange(
333281
state: State,
334282
doc: TextDocument,
335283
range?: Range,
336284
mode?: 'html' | 'css',
337285
includeCustom: boolean = true
338-
): Promise<Array<DocumentClassList | DocumentClassList[]>> {
339-
let classLists: Array<DocumentClassList | DocumentClassList[]>
286+
): Promise<DocumentClassList[]> {
287+
let classLists: DocumentClassList[]
340288
if (mode === 'css') {
341289
classLists = findClassListsInCssRange(doc, range)
342290
} else {
@@ -348,7 +296,7 @@ export async function findClassListsInRange(
348296
export async function findClassListsInDocument(
349297
state: State,
350298
doc: TextDocument
351-
): Promise<Array<DocumentClassList | DocumentClassList[]>> {
299+
): Promise<DocumentClassList[]> {
352300
if (isCssDoc(state, doc)) {
353301
return findClassListsInCssRange(doc)
354302
}

0 commit comments

Comments
 (0)