Skip to content

Commit 0625c6d

Browse files
authored
Fix duplicate color decorators (#652)
1 parent 19a550d commit 0625c6d

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getColor, getColorFromValue, culoriColorToVscodeColor } from './util/co
88
import { stringToPath } from './util/stringToPath'
99
import type { TextDocument, ColorInformation } from 'vscode-languageserver'
1010
import dlv from 'dlv'
11+
import { dedupeByRange } from './util/array'
1112

1213
export async function getDocumentColors(
1314
state: State,
@@ -45,5 +46,5 @@ export async function getDocumentColors(
4546
}
4647
})
4748

48-
return colors
49+
return dedupeByRange(colors)
4950
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type { Range } from 'vscode-languageserver'
2+
import { rangesEqual } from './rangesEqual'
3+
14
export function dedupe<T>(arr: Array<T>): Array<T> {
25
return arr.filter((value, index, self) => self.indexOf(value) === index)
36
}
@@ -6,6 +9,13 @@ export function dedupeBy<T>(arr: Array<T>, transform: (item: T) => any): Array<T
69
return arr.filter((value, index, self) => self.map(transform).indexOf(transform(value)) === index)
710
}
811

12+
export function dedupeByRange<T extends { range: Range }>(arr: Array<T>): Array<T> {
13+
return arr.filter(
14+
(classList, classListIndex) =>
15+
classListIndex === arr.findIndex((c) => rangesEqual(c.range, classList.range))
16+
)
17+
}
18+
919
export function ensureArray<T>(value: T | T[]): T[] {
1020
return Array.isArray(value) ? value : [value]
1121
}

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import { isCssContext, isCssDoc } from './css'
55
import { isHtmlContext } from './html'
66
import { isWithinRange } from './isWithinRange'
77
import { isJsxContext } from './js'
8-
import { flatten } from './array'
8+
import { dedupeByRange, flatten } from './array'
99
import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
1010
import { getLanguageBoundaries } from './getLanguageBoundaries'
1111
import { resolveRange } from './resolveRange'
12-
import dlv from 'dlv'
13-
import { rangesEqual } from './rangesEqual'
1412
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
1513
import { getTextWithoutComments } from './doc'
1614

@@ -282,13 +280,6 @@ export async function findClassListsInHtmlRange(
282280
return result
283281
}
284282

285-
function dedupeClassLists(classLists: DocumentClassList[]): DocumentClassList[] {
286-
return classLists.filter(
287-
(classList, classListIndex) =>
288-
classListIndex === classLists.findIndex((c) => rangesEqual(c.range, classList.range))
289-
)
290-
}
291-
292283
export async function findClassListsInRange(
293284
state: State,
294285
doc: TextDocument,
@@ -302,7 +293,7 @@ export async function findClassListsInRange(
302293
} else {
303294
classLists = await findClassListsInHtmlRange(state, doc, mode, range)
304295
}
305-
return dedupeClassLists([
296+
return dedupeByRange([
306297
...classLists,
307298
...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
308299
])
@@ -319,7 +310,7 @@ export async function findClassListsInDocument(
319310
let boundaries = getLanguageBoundaries(state, doc)
320311
if (!boundaries) return []
321312

322-
return dedupeClassLists(
313+
return dedupeByRange(
323314
flatten([
324315
...(await Promise.all(
325316
boundaries

0 commit comments

Comments
 (0)