|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { CSharpExtensionId } from '../constants/csharpExtensionId'; |
| 8 | +import { CopilotRelatedDocumentsReport, CopilotRelatedDocumentsRequest } from './roslynProtocol'; |
| 9 | +import { RoslynLanguageServer } from './roslynLanguageServer'; |
| 10 | +import { UriConverter } from './uriConverter'; |
| 11 | +import { TextDocumentIdentifier } from 'vscode-languageserver-protocol'; |
| 12 | + |
| 13 | +export async function registerCopilotExtension(languageServer: RoslynLanguageServer, channel: vscode.OutputChannel) { |
| 14 | + const ext = vscode.extensions.getExtension('github.copilot'); |
| 15 | + if (!ext) { |
| 16 | + channel.appendLine('GitHub Copilot extension not installed. Skipping call to `registerRelatedFilesProvider`'); |
| 17 | + return; |
| 18 | + } |
| 19 | + await ext.activate(); |
| 20 | + const relatedAPI = ext.exports as |
| 21 | + | { |
| 22 | + registerRelatedFilesProvider( |
| 23 | + providerId: { extensionId: string; languageId: string }, |
| 24 | + callback: ( |
| 25 | + uri: vscode.Uri |
| 26 | + ) => Promise<{ entries: vscode.Uri[]; traits?: { name: string; value: string }[] }> |
| 27 | + ): void; |
| 28 | + } |
| 29 | + | undefined; |
| 30 | + if (!relatedAPI) { |
| 31 | + channel.appendLine( |
| 32 | + 'Incompatible GitHub Copilot extension installed. Skipping call to `registerRelatedFilesProvider`' |
| 33 | + ); |
| 34 | + return; |
| 35 | + } |
| 36 | + channel.appendLine('registerRelatedFilesProvider succeeded.'); |
| 37 | + |
| 38 | + const id = { |
| 39 | + extensionId: CSharpExtensionId, |
| 40 | + languageId: 'csharp', |
| 41 | + }; |
| 42 | + |
| 43 | + relatedAPI.registerRelatedFilesProvider(id, async (uri) => { |
| 44 | + const writeOutput = (output: CopilotRelatedDocumentsReport[], builder: vscode.Uri[] | null) => { |
| 45 | + if (output) { |
| 46 | + for (const report of output) { |
| 47 | + if (report._vs_file_paths) { |
| 48 | + for (const filePath of report._vs_file_paths) { |
| 49 | + channel.appendLine('found related file: ' + filePath); |
| 50 | + builder?.push(vscode.Uri.file(filePath)); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + const relatedFiles: vscode.Uri[] = []; |
| 58 | + const uriString = UriConverter.serialize(uri); |
| 59 | + const textDocument = TextDocumentIdentifier.create(uriString); |
| 60 | + const responsePromise = languageServer.sendRequestWithProgress( |
| 61 | + CopilotRelatedDocumentsRequest.type, |
| 62 | + { |
| 63 | + _vs_textDocument: textDocument, |
| 64 | + position: { |
| 65 | + line: 0, |
| 66 | + character: 0, |
| 67 | + }, |
| 68 | + }, |
| 69 | + async (p) => { |
| 70 | + writeOutput(p, relatedFiles); |
| 71 | + } |
| 72 | + ); |
| 73 | + |
| 74 | + await responsePromise.then( |
| 75 | + (result) => { |
| 76 | + writeOutput(result, null); |
| 77 | + return; |
| 78 | + }, |
| 79 | + (err) => { |
| 80 | + channel.appendLine(err); |
| 81 | + return; |
| 82 | + } |
| 83 | + ); |
| 84 | + |
| 85 | + return { |
| 86 | + entries: relatedFiles, |
| 87 | + }; |
| 88 | + }); |
| 89 | +} |
0 commit comments