From 0625c6de357074b1eafa659b7211a492eb74d047 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Fri, 4 Nov 2022 10:56:23 +0000 Subject: [PATCH] Fix duplicate color decorators (#652) --- .../src/documentColorProvider.ts | 3 ++- .../src/util/array.ts | 10 ++++++++++ .../tailwindcss-language-service/src/util/find.ts | 15 +++------------ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/tailwindcss-language-service/src/documentColorProvider.ts b/packages/tailwindcss-language-service/src/documentColorProvider.ts index dac832f..cab39e7 100644 --- a/packages/tailwindcss-language-service/src/documentColorProvider.ts +++ b/packages/tailwindcss-language-service/src/documentColorProvider.ts @@ -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, @@ -45,5 +46,5 @@ export async function getDocumentColors( } }) - return colors + return dedupeByRange(colors) } diff --git a/packages/tailwindcss-language-service/src/util/array.ts b/packages/tailwindcss-language-service/src/util/array.ts index f7c16b8..0c5b5f2 100644 --- a/packages/tailwindcss-language-service/src/util/array.ts +++ b/packages/tailwindcss-language-service/src/util/array.ts @@ -1,3 +1,6 @@ +import type { Range } from 'vscode-languageserver' +import { rangesEqual } from './rangesEqual' + export function dedupe(arr: Array): Array { return arr.filter((value, index, self) => self.indexOf(value) === index) } @@ -6,6 +9,13 @@ export function dedupeBy(arr: Array, transform: (item: T) => any): Array self.map(transform).indexOf(transform(value)) === index) } +export function dedupeByRange(arr: Array): Array { + return arr.filter( + (classList, classListIndex) => + classListIndex === arr.findIndex((c) => rangesEqual(c.range, classList.range)) + ) +} + export function ensureArray(value: T | T[]): T[] { return Array.isArray(value) ? value : [value] } diff --git a/packages/tailwindcss-language-service/src/util/find.ts b/packages/tailwindcss-language-service/src/util/find.ts index edcb595..8dd6ad9 100644 --- a/packages/tailwindcss-language-service/src/util/find.ts +++ b/packages/tailwindcss-language-service/src/util/find.ts @@ -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' @@ -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, @@ -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) : []), ]) @@ -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