diff --git a/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts b/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts index c44bfc3ac3f..4897be97c22 100644 --- a/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts +++ b/extensions/typescript-language-features/src/typeScriptServiceClientHost.ts @@ -9,6 +9,7 @@ * ------------------------------------------------------------------------------------------ */ import * as vscode from 'vscode'; +import { URI } from 'vscode-uri'; import { CommandManager } from './commands/commandManager'; import { ServiceConfigurationProvider } from './configuration/configuration'; import { DiagnosticLanguage, LanguageDescription } from './configuration/languageDescription'; @@ -35,6 +36,7 @@ import TypingsStatus, { AtaProgressReporter } from './ui/typingsStatus'; import { VersionStatus } from './ui/versionStatus'; import { coalesce } from './utils/arrays'; import { Disposable } from './utils/dispose'; +import { jsTsLanguageModes, isSupportedLanguageMode } from './configuration/languageIds'; // Style check diagnostics that can be reported as warnings const styleCheckDiagnostics = new Set([ @@ -119,7 +121,7 @@ export default class TypeScriptServiceClientHost extends Disposable { this._register(module.register(this.client, allModeIds))); this.client.ensureServiceStarted(); - this.client.onReady(() => { + this.client.onReady(async () => { const languages = new Set(); for (const plugin of services.pluginManager.plugins) { if (plugin.configNamespace && plugin.languages.length) { @@ -150,6 +152,60 @@ export default class TypeScriptServiceClientHost extends Disposable { standardFileExtensions: [], }, onCompletionAccepted); } + // TODO: Still probably isn't the right place for this + const ext = vscode.extensions.getExtension('github.copilot'); + if (!ext) { + vscode.window.showErrorMessage(vscode.l10n.t('Could not find related-files extension')); + return; + } + await ext.activate(); + const relatedAPI = ext.exports as { + registerRelatedFilesProvider( + providerId: { extensionId: string; languageId: string }, + callback: (uri: vscode.Uri) => Promise<{ entries: vscode.Uri[]; traits?: { name: string; value: string }[] }> + ): void; + } | undefined; + if (relatedAPI) { + for (const languageId of jsTsLanguageModes) { + const id = { + extensionId: 'vscode.typescript-language-features', + languageId + }; + relatedAPI.registerRelatedFilesProvider(id, async uri => { + let document; + try { + document = await vscode.workspace.openTextDocument(uri); + } catch { + if (!vscode.window.activeTextEditor) { + vscode.window.showErrorMessage(vscode.l10n.t("Go to Source Definition failed. No resource provided.")); + return []; + } + // something is REALLY wrong if you can't open the active text editor's document, so don't catch that + document = await vscode.workspace.openTextDocument(vscode.window.activeTextEditor.document.uri); + } + + if (!isSupportedLanguageMode(document)) { + vscode.window.showErrorMessage(vscode.l10n.t("Go to Source Definition failed. Unsupported file type.")); + return []; + } + + const file = this.client.toOpenTsFilePath(document); + if (!file) { + return []; + } + // TODO: This compiles but will never cancel; this needs a token that somebody might actually cancel. + const cancel = new vscode.CancellationTokenSource(); + const response = await this.client.execute('getImports', { file, }, cancel.token); + if (response.type !== 'response' || !response.body) { + return []; + } + return response.body.map(URI.file); + }); + } + } + else { + vscode.window.showErrorMessage(vscode.l10n.t('Could not find github.copilot extension')); + } }); this.client.onTsServerStarted(() => { diff --git a/extensions/typescript-language-features/src/typescriptService.ts b/extensions/typescript-language-features/src/typescriptService.ts index 90528ee47dc..367dda853e8 100644 --- a/extensions/typescript-language-features/src/typescriptService.ts +++ b/extensions/typescript-language-features/src/typescriptService.ts @@ -77,6 +77,7 @@ interface StandardTsServerRequests { 'getMoveToRefactoringFileSuggestions': [Proto.GetMoveToRefactoringFileSuggestionsRequestArgs, Proto.GetMoveToRefactoringFileSuggestions]; 'linkedEditingRange': [Proto.FileLocationRequestArgs, Proto.LinkedEditingRangeResponse]; 'mapCode': [Proto.MapCodeRequestArgs, Proto.MapCodeResponse]; + 'getImports': [Proto.GetImportRequestArgs, Proto.GetImportResponse]; 'getPasteEdits': [Proto.GetPasteEditsRequestArgs, Proto.GetPasteEditsResponse]; 'preparePasteEdits': [Proto.PreparePasteEditsRequestArgs, Proto.PreparePasteEditsResponse]; }