Skip to content

Commit

Permalink
[typescript-language-features] Support replacing Go to Definition wit…
Browse files Browse the repository at this point in the history
…h 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
  • Loading branch information
andrewbranch authored Apr 5, 2023
1 parent 6d96ceb commit 1176681
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
12 changes: 12 additions & 0 deletions extensions/typescript-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions extensions/typescript-language-features/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand Down

0 comments on commit 1176681

Please sign in to comment.