Skip to content

Commit 81df531

Browse files
author
Andy
authored
Simplify getOccurrencesAtPosition (#21977)
1 parent 0f697c3 commit 81df531

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

src/services/services.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,17 +1567,17 @@ namespace ts {
15671567

15681568
/// References and Occurrences
15691569
function getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[] {
1570-
let results = getOccurrencesAtPositionCore(fileName, position);
1571-
1572-
if (results) {
1573-
const sourceFile = getCanonicalFileName(normalizeSlashes(fileName));
1574-
1575-
// Get occurrences only supports reporting occurrences for the file queried. So
1576-
// filter down to that list.
1577-
results = filter(results, r => getCanonicalFileName(ts.normalizeSlashes(r.fileName)) === sourceFile);
1578-
}
1579-
1580-
return results;
1570+
const canonicalFileName = getCanonicalFileName(normalizeSlashes(fileName));
1571+
return flatMap(getDocumentHighlights(fileName, position, [fileName]), entry => entry.highlightSpans.map<ReferenceEntry>(highlightSpan => {
1572+
Debug.assert(getCanonicalFileName(normalizeSlashes(entry.fileName)) === canonicalFileName); // Get occurrences only supports reporting occurrences for the file queried.
1573+
return {
1574+
fileName: entry.fileName,
1575+
textSpan: highlightSpan.textSpan,
1576+
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference,
1577+
isDefinition: false,
1578+
isInString: highlightSpan.isInString,
1579+
};
1580+
}));
15811581
}
15821582

15831583
function getDocumentHighlights(fileName: string, position: number, filesToSearch: ReadonlyArray<string>): DocumentHighlights[] {
@@ -1587,31 +1587,6 @@ namespace ts {
15871587
return DocumentHighlights.getDocumentHighlights(program, cancellationToken, sourceFile, position, sourceFilesToSearch);
15881588
}
15891589

1590-
function getOccurrencesAtPositionCore(fileName: string, position: number): ReferenceEntry[] {
1591-
return convertDocumentHighlights(getDocumentHighlights(fileName, position, [fileName]));
1592-
1593-
function convertDocumentHighlights(documentHighlights: DocumentHighlights[]): ReferenceEntry[] {
1594-
if (!documentHighlights) {
1595-
return undefined;
1596-
}
1597-
1598-
const result: ReferenceEntry[] = [];
1599-
for (const entry of documentHighlights) {
1600-
for (const highlightSpan of entry.highlightSpans) {
1601-
result.push({
1602-
fileName: entry.fileName,
1603-
textSpan: highlightSpan.textSpan,
1604-
isWriteAccess: highlightSpan.kind === HighlightSpanKind.writtenReference,
1605-
isDefinition: false,
1606-
isInString: highlightSpan.isInString,
1607-
});
1608-
}
1609-
}
1610-
1611-
return result;
1612-
}
1613-
}
1614-
16151590
function findRenameLocations(fileName: string, position: number, findInStrings: boolean, findInComments: boolean): RenameLocation[] {
16161591
return getReferences(fileName, position, { findInStrings, findInComments, isForRename: true });
16171592
}

0 commit comments

Comments
 (0)