Fix duplicate color decorators (#652)

master
Brad Cornes 2022-11-04 10:56:23 +00:00 committed by GitHub
parent 19a550d0de
commit 0625c6de35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import { getColor, getColorFromValue, culoriColorToVscodeColor } from './util/co
import { stringToPath } from './util/stringToPath' import { stringToPath } from './util/stringToPath'
import type { TextDocument, ColorInformation } from 'vscode-languageserver' import type { TextDocument, ColorInformation } from 'vscode-languageserver'
import dlv from 'dlv' import dlv from 'dlv'
import { dedupeByRange } from './util/array'
export async function getDocumentColors( export async function getDocumentColors(
state: State, state: State,
@ -45,5 +46,5 @@ export async function getDocumentColors(
} }
}) })
return colors return dedupeByRange(colors)
} }

View File

@ -1,3 +1,6 @@
import type { Range } from 'vscode-languageserver'
import { rangesEqual } from './rangesEqual'
export function dedupe<T>(arr: Array<T>): Array<T> { export function dedupe<T>(arr: Array<T>): Array<T> {
return arr.filter((value, index, self) => self.indexOf(value) === index) return arr.filter((value, index, self) => self.indexOf(value) === index)
} }
@ -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) 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[] { export function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value] return Array.isArray(value) ? value : [value]
} }

View File

@ -5,12 +5,10 @@ import { isCssContext, isCssDoc } from './css'
import { isHtmlContext } from './html' import { isHtmlContext } from './html'
import { isWithinRange } from './isWithinRange' import { isWithinRange } from './isWithinRange'
import { isJsxContext } from './js' import { isJsxContext } from './js'
import { flatten } from './array' import { dedupeByRange, flatten } from './array'
import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers' import { getClassAttributeLexer, getComputedClassAttributeLexer } from './lexers'
import { getLanguageBoundaries } from './getLanguageBoundaries' import { getLanguageBoundaries } from './getLanguageBoundaries'
import { resolveRange } from './resolveRange' 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 Regex from 'becke-ch--regex--s0-0-v1--base--pl--lib'
import { getTextWithoutComments } from './doc' import { getTextWithoutComments } from './doc'
@ -282,13 +280,6 @@ export async function findClassListsInHtmlRange(
return result 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( export async function findClassListsInRange(
state: State, state: State,
doc: TextDocument, doc: TextDocument,
@ -302,7 +293,7 @@ export async function findClassListsInRange(
} else { } else {
classLists = await findClassListsInHtmlRange(state, doc, mode, range) classLists = await findClassListsInHtmlRange(state, doc, mode, range)
} }
return dedupeClassLists([ return dedupeByRange([
...classLists, ...classLists,
...(includeCustom ? await findCustomClassLists(state, doc, range) : []), ...(includeCustom ? await findCustomClassLists(state, doc, range) : []),
]) ])
@ -319,7 +310,7 @@ export async function findClassListsInDocument(
let boundaries = getLanguageBoundaries(state, doc) let boundaries = getLanguageBoundaries(state, doc)
if (!boundaries) return [] if (!boundaries) return []
return dedupeClassLists( return dedupeByRange(
flatten([ flatten([
...(await Promise.all( ...(await Promise.all(
boundaries boundaries