From 44b5a77632fd284c2003bb3f2e136ee5d69b2e40 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 2 Jan 2019 11:54:42 -0800 Subject: [PATCH] Use TS's document highlight API instead of references api to get highlights Fixes #65921 Fixes #65051 --- .../src/features/documentHighlight.ts | 24 ++++++++++++------- .../src/typescriptService.ts | 1 + 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/extensions/typescript-language-features/src/features/documentHighlight.ts b/extensions/typescript-language-features/src/features/documentHighlight.ts index 8d6e9e26ce3..970a265f218 100644 --- a/extensions/typescript-language-features/src/features/documentHighlight.ts +++ b/extensions/typescript-language-features/src/features/documentHighlight.ts @@ -6,6 +6,7 @@ import * as vscode from 'vscode'; import * as Proto from '../protocol'; import { ITypeScriptServiceClient } from '../typescriptService'; +import { flatten } from '../utils/arrays'; import * as typeConverters from '../utils/typeConverters'; class TypeScriptDocumentHighlightProvider implements vscode.DocumentHighlightProvider { @@ -23,22 +24,27 @@ class TypeScriptDocumentHighlightProvider implements vscode.DocumentHighlightPro return []; } - const args = typeConverters.Position.toFileLocationRequestArgs(file, position); - const response = await this.client.execute('references', args, token); + const args = { + ...typeConverters.Position.toFileLocationRequestArgs(file, position), + filesToSearch: [file] + }; + const response = await this.client.execute('documentHighlights', args, token); if (response.type !== 'response' || !response.body) { return []; } - return response.body.refs - .filter(ref => ref.file === file) - .map(documentHighlightFromReference); + return flatten( + response.body + .filter(highlight => highlight.file === file) + .map(convertDocumentHighlight)); } } -function documentHighlightFromReference(reference: Proto.ReferencesResponseItem): vscode.DocumentHighlight { - return new vscode.DocumentHighlight( - typeConverters.Range.fromTextSpan(reference), - reference.isWriteAccess ? vscode.DocumentHighlightKind.Write : vscode.DocumentHighlightKind.Read); +function convertDocumentHighlight(highlight: Proto.DocumentHighlightsItem): ReadonlyArray { + return highlight.highlightSpans.map(span => + new vscode.DocumentHighlight( + typeConverters.Range.fromTextSpan(span), + span.kind === 'writtenReference' ? vscode.DocumentHighlightKind.Write : vscode.DocumentHighlightKind.Read)); } export function register( diff --git a/extensions/typescript-language-features/src/typescriptService.ts b/extensions/typescript-language-features/src/typescriptService.ts index a81024f0063..874cb020a94 100644 --- a/extensions/typescript-language-features/src/typescriptService.ts +++ b/extensions/typescript-language-features/src/typescriptService.ts @@ -34,6 +34,7 @@ interface TypeScriptRequestTypes { 'definition': [Proto.FileLocationRequestArgs, Proto.DefinitionResponse]; 'definitionAndBoundSpan': [Proto.FileLocationRequestArgs, Proto.DefinitionInfoAndBoundSpanReponse]; 'docCommentTemplate': [Proto.FileLocationRequestArgs, Proto.DocCommandTemplateResponse]; + 'documentHighlights': [Proto.DocumentHighlightsRequestArgs, Proto.DocumentHighlightsResponse]; 'format': [Proto.FormatRequestArgs, Proto.FormatResponse]; 'formatonkey': [Proto.FormatOnKeyRequestArgs, Proto.FormatResponse]; 'getApplicableRefactors': [Proto.GetApplicableRefactorsRequestArgs, Proto.GetApplicableRefactorsResponse];