From 117668153b2fc6c8b105a9bd2eb3b7261003bd62 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 5 Apr 2023 14:17:01 -0700 Subject: [PATCH] [typescript-language-features] Support replacing Go to Definition with Go to Source Definition by preference (#178840) * Add preference for replacing Go to Definition with Go to Source Definition * Support replacing Go to Definition with Go to Source Definition by preference * Predicate call on TS version --- extensions/typescript-language-features/package.json | 12 ++++++++++++ .../typescript-language-features/package.nls.json | 1 + .../src/languageFeatures/definitions.ts | 12 +++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/extensions/typescript-language-features/package.json b/extensions/typescript-language-features/package.json index 8eb4a71a13df3..13846bdca1815 100644 --- a/extensions/typescript-language-features/package.json +++ b/extensions/typescript-language-features/package.json @@ -1210,6 +1210,18 @@ "tags": [ "experimental" ] + }, + "typescript.preferGoToSourceDefinition": { + "type": "boolean", + "default": false, + "description": "%configuration.preferGoToSourceDefinition%", + "scope": "window" + }, + "javascript.preferGoToSourceDefinition": { + "type": "boolean", + "default": false, + "description": "%configuration.preferGoToSourceDefinition%", + "scope": "window" } } }, diff --git a/extensions/typescript-language-features/package.nls.json b/extensions/typescript-language-features/package.nls.json index d489483363d22..b3a3b26798295 100644 --- a/extensions/typescript-language-features/package.nls.json +++ b/extensions/typescript-language-features/package.nls.json @@ -80,6 +80,7 @@ "configuration.implicitProjectConfig.strictFunctionTypes": "Enable/disable [strict function types](https://www.typescriptlang.org/tsconfig#strictFunctionTypes) in JavaScript and TypeScript files that are not part of a project. Existing `jsconfig.json` or `tsconfig.json` files override this setting.", "configuration.suggest.jsdoc.generateReturns": "Enable/disable generating `@returns` annotations for JSDoc templates.", "configuration.suggest.autoImports": "Enable/disable auto import suggestions.", + "configuration.preferGoToSourceDefinition": "Makes Go to Definition avoid type declaration files when possible by triggering Go to Source Definition instead. This allows Go to Source Definition to be triggered with the mouse gesture. Requires using TypeScript 4.7+ in the workspace.", "inlayHints.parameterNames.none": "Disable parameter name hints.", "inlayHints.parameterNames.literals": "Enable parameter name hints only for literal arguments.", "inlayHints.parameterNames.all": "Enable parameter name hints for literal and non-literal arguments.", diff --git a/extensions/typescript-language-features/src/languageFeatures/definitions.ts b/extensions/typescript-language-features/src/languageFeatures/definitions.ts index d7de10b1add18..76c5818be6cc0 100644 --- a/extensions/typescript-language-features/src/languageFeatures/definitions.ts +++ b/extensions/typescript-language-features/src/languageFeatures/definitions.ts @@ -5,6 +5,7 @@ import * as vscode from 'vscode'; import { DocumentSelector } from '../configuration/documentSelector'; +import { API } from '../tsServer/api'; import * as typeConverters from '../typeConverters'; import { ClientCapability, ITypeScriptServiceClient } from '../typescriptService'; import DefinitionProviderBase from './definitionProviderBase'; @@ -29,7 +30,16 @@ export default class TypeScriptDefinitionProvider extends DefinitionProviderBase } const span = response.body.textSpan ? typeConverters.Range.fromTextSpan(response.body.textSpan) : undefined; - return response.body.definitions + let definitions = response.body.definitions; + + if (vscode.workspace.getConfiguration(document.languageId).get('preferGoToSourceDefinition', false) && this.client.apiVersion.gte(API.v470)) { + const sourceDefinitionsResponse = await this.client.execute('findSourceDefinition', args, token); + if (sourceDefinitionsResponse.type === 'response' && sourceDefinitionsResponse.body?.length) { + definitions = sourceDefinitionsResponse.body; + } + } + + return definitions .map((location): vscode.DefinitionLink => { const target = typeConverters.Location.fromTextSpan(this.client.toResource(location.file), location); if (location.contextStart && location.contextEnd) {