Add find all references command for JS/TS

Fixes #66150
This commit is contained in:
Matt Bierner
2021-01-04 16:55:11 -08:00
parent 0f9ee98846
commit b565c422aa
7 changed files with 163 additions and 1 deletions

View File

@@ -0,0 +1,94 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as nls from 'vscode-nls';
import { Command, CommandManager } from '../commands/commandManager';
import { ITypeScriptServiceClient } from '../typescriptService';
import API from '../utils/api';
import { isSupportedLanguageMode } from '../utils/languageModeIds';
import * as typeConverters from '../utils/typeConverters';
const localize = nls.loadMessageBundle();
class FileReferencesCommand implements Command {
public static readonly context = 'tsSupportsFileReferences';
public static readonly minVersion = API.v420;
public readonly id = 'typescript.findAllFileReferences';
public constructor(
private readonly client: ITypeScriptServiceClient
) { }
public async execute(resource?: vscode.Uri) {
if (this.client.apiVersion.lt(FileReferencesCommand.minVersion)) {
vscode.window.showErrorMessage(localize('error.unsupportedVersion', "Find file references failed. Requires TypeScript 4.2+."));
return;
}
if (!resource) {
resource = vscode.window.activeTextEditor?.document.uri;
}
if (!resource) {
vscode.window.showErrorMessage(localize('error.noResource', "Find file references failed. No resource provided."));
return;
}
const document = await vscode.workspace.openTextDocument(resource);
if (!isSupportedLanguageMode(document)) {
vscode.window.showErrorMessage(localize('error.unsupportedLanguage', "Find file references failed. Unsupported file type."));
return;
}
const openedFiledPath = this.client.toOpenedFilePath(document);
if (!openedFiledPath) {
vscode.window.showErrorMessage(localize('error.unknownFile', "Find file references failed. Unknown file type."));
return;
}
await vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: localize('progress.title', "Finding file references")
}, async (_progress, token) => {
const response = await this.client.execute('fileReferences', {
file: openedFiledPath
}, token);
if (response.type !== 'response' || !response.body) {
return;
}
const locations: vscode.Location[] = response.body.refs.map(reference =>
typeConverters.Location.fromTextSpan(this.client.toResource(reference.file), reference));
const config = vscode.workspace.getConfiguration('references');
const existingSetting = config.get('preferredLocation', undefined);
await config.update('preferredLocation', 'view');
try {
await vscode.commands.executeCommand('editor.action.showReferences', resource, new vscode.Position(0, 0), locations);
} finally {
await config.update('preferredLocation', existingSetting);
}
});
}
}
export function register(
client: ITypeScriptServiceClient,
commandManager: CommandManager
) {
function updateContext() {
vscode.commands.executeCommand('setContext', FileReferencesCommand.context, client.apiVersion.gte(FileReferencesCommand.minVersion));
}
updateContext();
commandManager.register(new FileReferencesCommand(client));
return client.onTsServerStarted(() => updateContext());
}

View File

@@ -63,6 +63,7 @@ export default class LanguageProvider extends Disposable {
import('./languageFeatures/directiveCommentCompletions').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/documentHighlight').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/documentSymbol').then(provider => this._register(provider.register(selector, this.client, cachedResponse))),
import('./languageFeatures/fileReferences').then(provider => this._register(provider.register(this.client, this.commandManager))),
import('./languageFeatures/folding').then(provider => this._register(provider.register(selector, this.client))),
import('./languageFeatures/formatting').then(provider => this._register(provider.register(selector, this.description.id, this.client, this.fileConfigurationManager))),
import('./languageFeatures/hover').then(provider => this._register(provider.register(selector, this.client))),

View File

@@ -9,4 +9,22 @@ declare module 'typescript/lib/protocol' {
interface Response {
readonly _serverType?: ServerType;
}
interface FileReferencesRequest extends FileRequest {
command: CommandTypes.FileReferences;
}
interface FileReferencesResponseBody {
/**
* The file locations referencing the symbol.
*/
refs: readonly ReferencesResponseItem[];
/**
* The name of the symbol.
*/
symbolName: string;
}
interface FileReferencesResponse extends Response {
body?: FileReferencesResponseBody;
}
}

View File

@@ -68,6 +68,7 @@ interface StandardTsServerRequests {
'prepareCallHierarchy': [Proto.FileLocationRequestArgs, Proto.PrepareCallHierarchyResponse];
'provideCallHierarchyIncomingCalls': [Proto.FileLocationRequestArgs, Proto.ProvideCallHierarchyIncomingCallsResponse];
'provideCallHierarchyOutgoingCalls': [Proto.FileLocationRequestArgs, Proto.ProvideCallHierarchyOutgoingCallsResponse];
'fileReferences': [Proto.FileRequestArgs, Proto.FileReferencesResponse];
}
interface NoResponseTsServerRequests {

View File

@@ -34,6 +34,7 @@ export default class API {
public static readonly v390 = API.fromSimpleString('3.9.0');
public static readonly v400 = API.fromSimpleString('4.0.0');
public static readonly v401 = API.fromSimpleString('4.0.1');
public static readonly v420 = API.fromSimpleString('4.2.0');
public static fromVersionString(versionString: string): API {
let version = semver.valid(versionString);