From bbfe46c4aadaabe19a55a63dd957e3cac7f9cc74 Mon Sep 17 00:00:00 2001 From: Qiming Zhao Date: Thu, 1 Sep 2022 20:31:00 +0800 Subject: [PATCH] use LocationLink on getSymbolLocations --- src/server/features/definitionProvider.ts | 29 +++++++++++++++-------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/server/features/definitionProvider.ts b/src/server/features/definitionProvider.ts index ea57402..178e863 100644 --- a/src/server/features/definitionProvider.ts +++ b/src/server/features/definitionProvider.ts @@ -2,7 +2,7 @@ * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { TextDocument } from 'coc.nvim' +import { LocationLink, TextDocument } from 'coc.nvim' import { DefinitionProvider, CancellationToken, Definition, Location, Position, DefinitionLink, ImplementationProvider, TypeDefinitionProvider } from 'coc.nvim' import * as Proto from '../protocol' import { ITypeScriptServiceClient } from '../typescriptService' @@ -17,7 +17,7 @@ export default class TypeScriptDefinitionProvider implements DefinitionProvider, document: TextDocument, position: Position, token: CancellationToken - ): Promise { + ): Promise { const filepath = this.client.toPath(document.uri) if (!filepath) { return undefined @@ -29,12 +29,21 @@ export default class TypeScriptDefinitionProvider implements DefinitionProvider, ) try { const response = await this.client.execute(definitionType, args, token) - const locations: Proto.FileSpan[] = (response.type == 'response' && response.body) || [] - return locations.map(location => - typeConverters.Location.fromTextSpan( - this.client.toResource(location.file), - location - ) + if (response.type !== 'response' || !response.body) { + return undefined + } + const locations: Proto.FileSpanWithContext[] = (response.type == 'response' && response.body) || [] + return locations.map(location => { + const target = typeConverters.Location.fromTextSpan(this.client.toResource(location.file), location) + if (location.contextStart && location.contextEnd) { + return { + targetRange: typeConverters.Range.fromLocations(location.contextStart, location.contextEnd), + targetUri: target.uri, + targetSelectionRange: target.range, + } as any + } + return target + } ) } catch { return [] @@ -84,14 +93,14 @@ export default class TypeScriptDefinitionProvider implements DefinitionProvider, public provideTypeDefinition( document: TextDocument, position: Position, - token: CancellationToken): Promise { + token: CancellationToken): Promise { return this.getSymbolLocations('typeDefinition', document, position, token) } public provideImplementation( document: TextDocument, position: Position, - token: CancellationToken): Promise { + token: CancellationToken): Promise { return this.getSymbolLocations('implementation', document, position, token) } }