Skip to content

Fix duplicate color decorators #652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getColor, getColorFromValue, culoriColorToVscodeColor } from './util/co
import { stringToPath } from './util/stringToPath'
import type { TextDocument, ColorInformation } from 'vscode-languageserver'
import dlv from 'dlv'
import { dedupeByRange } from './util/array'

export async function getDocumentColors(
state: State,
Expand Down Expand Up @@ -45,5 +46,5 @@ export async function getDocumentColors(
}
})

return colors
return dedupeByRange(colors)
}
10 changes: 10 additions & 0 deletions packages/tailwindcss-language-service/src/util/array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Range } from 'vscode-languageserver'
import { rangesEqual } from './rangesEqual'

export function dedupe<T>(arr: Array<T>): Array<T> {
return arr.filter((value, index, self) => self.indexOf(value) === index)
}
Expand All @@ -6,6 +9,13 @@ export function dedupeBy<T>(arr: Array<T>, transform: (item: T) => any): Array<T
return arr.filter((value, index, self) => self.map(transform).indexOf(transform(value)) === index)
}

export function dedupeByRange<T extends { range: Range }>(arr: Array<T>): Array<T> {
return arr.filter(
(classList, classListIndex) =>
classListIndex === arr.findIndex((c) => rangesEqual(c.range, classList.range))
)
}

export function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
Expand Down
15 changes: 3 additions & 12 deletions packages/tailwindcss-language-service/src/util/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import { isCssContext, isCssDoc } from './css'
import { isHtmlContext } from './html'
import { isWithinRange } from './isWithinRange'
import { isJsxContext } from './js'
import { flatten } from './array'
import { dedupeByRange, flatten } from './array'
import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
import { getLanguageBoundaries } from './getLanguageBoundaries'
import { resolveRange } from './resolveRange'
import dlv from 'dlv'
import { rangesEqual } from './rangesEqual'
import Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
import { getTextWithoutComments } from './doc'

Expand Down Expand Up @@ -282,13 +280,6 @@ export async function findClassListsInHtmlRange(
return result
}

function dedupeClassLists(classLists: DocumentClassList[]): DocumentClassList[] {
return classLists.filter(
(classList, classListIndex) =>
classListIndex === classLists.findIndex((c) => rangesEqual(c.range, classList.range))
)
}

export async function findClassListsInRange(
state: State,
doc: TextDocument,
Expand All @@ -302,7 +293,7 @@ export async function findClassListsInRange(
} else {
classLists = await findClassListsInHtmlRange(state, doc, mode, range)
}
return dedupeClassLists([
return dedupeByRange([
...classLists,
...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
])
Expand All @@ -319,7 +310,7 @@ export async function findClassListsInDocument(
let boundaries = getLanguageBoundaries(state, doc)
if (!boundaries) return []

return dedupeClassLists(
return dedupeByRange(
flatten([
...(await Promise.all(
boundaries
Expand Down