Skip to content

Commit d09a4b1

Browse files
authored
Deduplicate classlist candidates (tailwindlabs#572)
1 parent 05a8685 commit d09a4b1

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getLanguageBoundaries } from './getLanguageBoundaries'
1111
import { resolveRange } from './resolveRange'
1212
import dlv from 'dlv'
1313
import { createMultiRegexp } from './createMultiRegexp'
14+
import { rangesEqual } from './rangesEqual'
1415

1516
export function findAll(re: RegExp, str: string): RegExpMatchArray[] {
1617
let match: RegExpMatchArray
@@ -277,6 +278,13 @@ export async function findClassListsInHtmlRange(
277278
return result
278279
}
279280

281+
function dedupeClassLists(classLists: DocumentClassList[]): DocumentClassList[] {
282+
return classLists.filter(
283+
(classList, classListIndex) =>
284+
classListIndex === classLists.findIndex((c) => rangesEqual(c.range, classList.range))
285+
)
286+
}
287+
280288
export async function findClassListsInRange(
281289
state: State,
282290
doc: TextDocument,
@@ -290,7 +298,10 @@ export async function findClassListsInRange(
290298
} else {
291299
classLists = await findClassListsInHtmlRange(state, doc, range)
292300
}
293-
return [...classLists, ...(includeCustom ? await findCustomClassLists(state, doc, range) : [])]
301+
return dedupeClassLists([
302+
...classLists,
303+
...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
304+
])
294305
}
295306

296307
export async function findClassListsInDocument(
@@ -304,17 +315,19 @@ export async function findClassListsInDocument(
304315
let boundaries = getLanguageBoundaries(state, doc)
305316
if (!boundaries) return []
306317

307-
return flatten([
308-
...(await Promise.all(
309-
boundaries
310-
.filter((b) => b.type === 'html' || b.type === 'jsx')
311-
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
312-
)),
313-
...boundaries
314-
.filter((b) => b.type === 'css')
315-
.map(({ range }) => findClassListsInCssRange(doc, range)),
316-
await findCustomClassLists(state, doc),
317-
])
318+
return dedupeClassLists(
319+
flatten([
320+
...(await Promise.all(
321+
boundaries
322+
.filter((b) => b.type === 'html' || b.type === 'jsx')
323+
.map(({ range }) => findClassListsInHtmlRange(state, doc, range))
324+
)),
325+
...boundaries
326+
.filter((b) => b.type === 'css')
327+
.map(({ range }) => findClassListsInCssRange(doc, range)),
328+
await findCustomClassLists(state, doc),
329+
])
330+
)
318331
}
319332

320333
export function findHelperFunctionsInDocument(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Range } from 'vscode-languageserver'
1+
import type { Range } from 'vscode-languageserver'
22

33
export function rangesEqual(a: Range, b: Range): boolean {
44
return (

0 commit comments

Comments
 (0)