Skip to content

Commit dd47f24

Browse files
author
Andy
authored
getSemanticDocumentHighlights: Use toMultiMap helper (microsoft#22059)
* getSemanticDocumentHighlights: Use `toMultiMap` helper * Rename to arrayToMultiMap and follow pattern of arrayToMap and arrayToNumericMap
1 parent 530d7e9 commit dd47f24

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

src/compiler/core.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -1333,20 +1333,20 @@ namespace ts {
13331333
*/
13341334
export function arrayToMap<T>(array: ReadonlyArray<T>, makeKey: (value: T) => string): Map<T>;
13351335
export function arrayToMap<T, U>(array: ReadonlyArray<T>, makeKey: (value: T) => string, makeValue: (value: T) => U): Map<U>;
1336-
export function arrayToMap<T, U>(array: ReadonlyArray<T>, makeKey: (value: T) => string, makeValue?: (value: T) => U): Map<T | U> {
1336+
export function arrayToMap<T, U>(array: ReadonlyArray<T>, makeKey: (value: T) => string, makeValue: (value: T) => T | U = identity): Map<T | U> {
13371337
const result = createMap<T | U>();
13381338
for (const value of array) {
1339-
result.set(makeKey(value), makeValue ? makeValue(value) : value);
1339+
result.set(makeKey(value), makeValue(value));
13401340
}
13411341
return result;
13421342
}
13431343

13441344
export function arrayToNumericMap<T>(array: ReadonlyArray<T>, makeKey: (value: T) => number): T[];
1345-
export function arrayToNumericMap<T, V>(array: ReadonlyArray<T>, makeKey: (value: T) => number, makeValue: (value: T) => V): V[];
1346-
export function arrayToNumericMap<T, V>(array: ReadonlyArray<T>, makeKey: (value: T) => number, makeValue?: (value: T) => V): V[] {
1347-
const result: V[] = [];
1345+
export function arrayToNumericMap<T, U>(array: ReadonlyArray<T>, makeKey: (value: T) => number, makeValue: (value: T) => U): U[];
1346+
export function arrayToNumericMap<T, U>(array: ReadonlyArray<T>, makeKey: (value: T) => number, makeValue: (value: T) => T | U = identity): (T | U)[] {
1347+
const result: (T | U)[] = [];
13481348
for (const value of array) {
1349-
result[makeKey(value)] = makeValue ? makeValue(value) : value as any as V;
1349+
result[makeKey(value)] = makeValue(value);
13501350
}
13511351
return result;
13521352
}
@@ -1362,6 +1362,20 @@ namespace ts {
13621362
return arrayToMap<any, true>(array, makeKey || (s => s), () => true);
13631363
}
13641364

1365+
export function arrayToMultiMap<T>(values: ReadonlyArray<T>, makeKey: (value: T) => string): MultiMap<T>;
1366+
export function arrayToMultiMap<T, U>(values: ReadonlyArray<T>, makeKey: (value: T) => string, makeValue: (value: T) => U): MultiMap<U>;
1367+
export function arrayToMultiMap<T, U>(values: ReadonlyArray<T>, makeKey: (value: T) => string, makeValue: (value: T) => T | U = identity): MultiMap<T | U> {
1368+
const result = createMultiMap<T | U>();
1369+
for (const value of values) {
1370+
result.add(makeKey(value), makeValue(value));
1371+
}
1372+
return result;
1373+
}
1374+
1375+
export function group<T>(values: ReadonlyArray<T>, getGroupId: (value: T) => string): ReadonlyArray<ReadonlyArray<T>> {
1376+
return arrayFrom(arrayToMultiMap(values, getGroupId).values());
1377+
}
1378+
13651379
export function cloneMap(map: SymbolTable): SymbolTable;
13661380
export function cloneMap<T>(map: ReadonlyMap<T>): Map<T>;
13671381
export function cloneMap<T>(map: ReadonlyUnderscoreEscapedMap<T>): UnderscoreEscapedMap<T>;
@@ -1438,14 +1452,6 @@ namespace ts {
14381452
}
14391453
}
14401454

1441-
export function group<T>(values: ReadonlyArray<T>, getGroupId: (value: T) => string): ReadonlyArray<ReadonlyArray<T>> {
1442-
const groupIdToGroup = createMultiMap<T>();
1443-
for (const value of values) {
1444-
groupIdToGroup.add(getGroupId(value), value);
1445-
}
1446-
return arrayFrom(groupIdToGroup.values());
1447-
}
1448-
14491455
/**
14501456
* Tests whether a value is an array.
14511457
*/

src/services/documentHighlights.ts

+4-16
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,11 @@ namespace ts.DocumentHighlights {
2121
};
2222
}
2323

24-
function getSemanticDocumentHighlights(position: number, node: Node, program: Program, cancellationToken: CancellationToken, sourceFilesToSearch: ReadonlyArray<SourceFile>): DocumentHighlights[] {
24+
function getSemanticDocumentHighlights(position: number, node: Node, program: Program, cancellationToken: CancellationToken, sourceFilesToSearch: ReadonlyArray<SourceFile>): DocumentHighlights[] | undefined {
2525
const referenceEntries = FindAllReferences.getReferenceEntriesForNode(position, node, program, sourceFilesToSearch, cancellationToken);
26-
return referenceEntries && convertReferencedSymbols(referenceEntries);
27-
}
28-
29-
function convertReferencedSymbols(referenceEntries: ReadonlyArray<FindAllReferences.Entry>): DocumentHighlights[] {
30-
const fileNameToDocumentHighlights = createMap<HighlightSpan[]>();
31-
for (const entry of referenceEntries) {
32-
const { fileName, span } = FindAllReferences.toHighlightSpan(entry);
33-
let highlightSpans = fileNameToDocumentHighlights.get(fileName);
34-
if (!highlightSpans) {
35-
fileNameToDocumentHighlights.set(fileName, highlightSpans = []);
36-
}
37-
highlightSpans.push(span);
38-
}
39-
40-
return arrayFrom(fileNameToDocumentHighlights.entries(), ([fileName, highlightSpans ]) => ({ fileName, highlightSpans }));
26+
if (!referenceEntries) return undefined;
27+
const map = arrayToMultiMap(referenceEntries.map(FindAllReferences.toHighlightSpan), e => e.fileName, e => e.span);
28+
return arrayFrom(map.entries(), ([fileName, highlightSpans]) => ({ fileName, highlightSpans }));
4129
}
4230

4331
function getSyntacticDocumentHighlights(node: Node, sourceFile: SourceFile): DocumentHighlights[] {

0 commit comments

Comments
 (0)