TS extension: add related-files call to CopilotRelated

registerRelatedFilesProvider is a new method on the copilot extension.
CopilotRelated is a new tsserver command that provides information from
tsserver to copilot. (The information is not necessarily
copilot-specific, of course.)
This commit is contained in:
Nathan Shively-Sanders
2024-09-13 15:07:41 -07:00
committed by Matt Bierner
parent cd1f8db59e
commit 7fcf402604
2 changed files with 58 additions and 1 deletions

View File

@@ -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<string>();
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(() => {

View File

@@ -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];
}