Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*---------------------------------------------------------------------------------------------
* 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 { nulToken } from '../utils/cancellation';
import { jsTsLanguageModes, isSupportedLanguageMode } from '../configuration/languageIds';
import { DocumentSelector } from '../configuration/documentSelector';
import { API } from '../tsServer/api';
import type * as Proto from '../tsServer/protocol/protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
import { conditionalRegistration, requireMinVersion } from './util/dependentRegistration';

const minVersion = API.v570;
const dummyDisposable = new vscode.Disposable(() => { });

export function register(
_selector: DocumentSelector,
client: ITypeScriptServiceClient,
) {
return conditionalRegistration([
requireMinVersion(client, minVersion),
], () => {
const ext = vscode.extensions.getExtension('github.copilot');
if (!ext) {
return dummyDisposable;
}
ext.activate().then(() => {
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?.registerRelatedFilesProvider) {
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("Related files provider failed. No active text editor."));
return { entries: [] };
}
// 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("Related files provider failed. Copilot requested file with unsupported language mode."));
return { entries: [] };
}

const file = client.toOpenTsFilePath(document);
if (!file) {
return { entries: [] };
}
// @ts-expect-error until ts5.7
const response = await client.execute('copilotRelated', { file, }, nulToken) as Proto.CopilotRelatedResponse;
if (response.type !== 'response' || !response.body) {
return { entries: [] };
}
return { entries: response.body.relatedFiles.map(f => client.toResource(f)), traits: [] };
});
}
}
});
return dummyDisposable;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class API {
public static readonly v540 = API.fromSimpleString('5.4.0');
public static readonly v550 = API.fromSimpleString('5.5.0');
public static readonly v560 = API.fromSimpleString('5.6.0');
public static readonly v570 = API.fromSimpleString('5.7.0');

public static fromVersionString(versionString: string): API {
let version = semver.valid(versionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ interface StandardTsServerRequests {
'getMoveToRefactoringFileSuggestions': [Proto.GetMoveToRefactoringFileSuggestionsRequestArgs, Proto.GetMoveToRefactoringFileSuggestions];
'linkedEditingRange': [Proto.FileLocationRequestArgs, Proto.LinkedEditingRangeResponse];
'mapCode': [Proto.MapCodeRequestArgs, Proto.MapCodeResponse];
// @ts-expect-error until ts5.7
'copilotRelated': [Proto.FileRequestArgs, Proto.CopilotRelatedResponse];
'getPasteEdits': [Proto.GetPasteEditsRequestArgs, Proto.GetPasteEditsResponse];
}

Expand Down