|
| 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 | +import { ContextProviderApiV1, ResolveRequest, SupportedContextItem } from '@github/copilot-language-server'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import * as lsp from 'vscode-languageserver-protocol'; |
| 8 | +import { RoslynLanguageServer } from '../server/roslynLanguageServer'; |
| 9 | +import { CSharpExtensionId } from '../../constants/csharpExtensionId'; |
| 10 | +import { csharpDevkitExtensionId, getCSharpDevKit } from '../../utils/getCSharpDevKit'; |
| 11 | +import path from 'path'; |
| 12 | +import { readJsonSync } from 'fs-extra'; |
| 13 | + |
| 14 | +export const copilotLanguageServerExtensionComponentName = '@microsoft/visualstudio.copilot.roslyn.languageserver'; |
| 15 | +export const copilotLanguageServerExtensionAssemblyName = 'Microsoft.VisualStudio.Copilot.Roslyn.LanguageServer.dll'; |
| 16 | +const copilotLanguageServerExtensionCapabilitiesFileName = 'capabilities.json'; |
| 17 | + |
| 18 | +export interface DocumentContext { |
| 19 | + textDocument: lsp.TextDocumentIdentifier; |
| 20 | + position: lsp.Position; |
| 21 | +} |
| 22 | + |
| 23 | +export interface ContextResolveParam { |
| 24 | + documentContext: DocumentContext; |
| 25 | + completionId: string; |
| 26 | + timeBudget: number; |
| 27 | + data?: any; |
| 28 | +} |
| 29 | + |
| 30 | +const resolveContextMethodName = 'roslyn/resolveContext'; |
| 31 | +const resolveContextMethodSupportedVersion = '1'; |
| 32 | +const resolveContextRequest = new lsp.RequestType<ContextResolveParam, SupportedContextItem[], void>( |
| 33 | + resolveContextMethodName, |
| 34 | + lsp.ParameterStructures.auto |
| 35 | +); |
| 36 | + |
| 37 | +interface CopilotApi { |
| 38 | + getContextProviderAPI(version: string): Promise<ContextProviderApiV1 | undefined>; |
| 39 | +} |
| 40 | + |
| 41 | +function createContextResolveParam(request: ResolveRequest): ContextResolveParam | undefined { |
| 42 | + let document: vscode.TextDocument | undefined; |
| 43 | + if (vscode.window.activeTextEditor?.document.uri.toString() === request.documentContext.uri) { |
| 44 | + document = vscode.window.activeTextEditor.document; |
| 45 | + } else { |
| 46 | + document = vscode.workspace.textDocuments.find((doc) => doc.uri.toString() === request.documentContext.uri); |
| 47 | + } |
| 48 | + if (document === undefined) { |
| 49 | + return undefined; |
| 50 | + } |
| 51 | + |
| 52 | + const position = document.positionAt(request.documentContext.offset); |
| 53 | + const uri = vscode.Uri.parse(request.documentContext.uri); |
| 54 | + const textDocument = lsp.TextDocumentIdentifier.create(uri.fsPath); |
| 55 | + |
| 56 | + const contextResolveParam: ContextResolveParam = { |
| 57 | + documentContext: { |
| 58 | + textDocument: textDocument, |
| 59 | + position: position, |
| 60 | + }, |
| 61 | + completionId: request.completionId, |
| 62 | + timeBudget: request.timeBudget, |
| 63 | + }; |
| 64 | + return contextResolveParam; |
| 65 | +} |
| 66 | + |
| 67 | +export function registerCopilotContextProviders( |
| 68 | + context: vscode.ExtensionContext, |
| 69 | + languageServer: RoslynLanguageServer, |
| 70 | + channel: vscode.LogOutputChannel |
| 71 | +) { |
| 72 | + const devkit = getCSharpDevKit(); |
| 73 | + if (!devkit) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + devkit.activate().then(async (devKitExports) => { |
| 78 | + try { |
| 79 | + // Check if the Copilot Language Server extension is installed and has the correct capabilities |
| 80 | + let hasCapabilities = false; |
| 81 | + const copilotServerExtensionfolder = devKitExports.components[copilotLanguageServerExtensionComponentName]; |
| 82 | + if (copilotServerExtensionfolder) { |
| 83 | + const capabilitiesFilePath = path.join( |
| 84 | + copilotServerExtensionfolder, |
| 85 | + copilotLanguageServerExtensionCapabilitiesFileName |
| 86 | + ); |
| 87 | + const capabilitiesContent = await readJsonSync(capabilitiesFilePath); |
| 88 | + if ( |
| 89 | + capabilitiesContent?.capabilities?.find( |
| 90 | + (capability: any) => |
| 91 | + capability?.method === resolveContextMethodName && |
| 92 | + capability?.version === resolveContextMethodSupportedVersion |
| 93 | + ) |
| 94 | + ) { |
| 95 | + hasCapabilities = true; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (!hasCapabilities) { |
| 100 | + channel.debug( |
| 101 | + `Failed to find compatible version of context provider from installed version of ${csharpDevkitExtensionId}.` |
| 102 | + ); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const copilotApi = vscode.extensions.getExtension<CopilotApi>('github.copilot'); |
| 107 | + if (!copilotApi) { |
| 108 | + channel.debug( |
| 109 | + 'Failed to find compatible version of GitHub Copilot extension installed. Skip registeration of Copilot context provider.' |
| 110 | + ); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + const api = await copilotApi.activate(); |
| 115 | + const contextProviderApi = await api.getContextProviderAPI('v1'); |
| 116 | + |
| 117 | + if (!contextProviderApi) { |
| 118 | + channel.debug( |
| 119 | + 'Incompatible GitHub Copilot extension installed. Skip registeration of C# context providers.' |
| 120 | + ); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + context.subscriptions.push( |
| 125 | + contextProviderApi.registerContextProvider<SupportedContextItem>({ |
| 126 | + id: CSharpExtensionId, // use extension id as provider id for now |
| 127 | + selector: [{ language: 'csharp' }], |
| 128 | + resolver: { |
| 129 | + resolve: async (request, token) => { |
| 130 | + const contextResolveParam = createContextResolveParam(request); |
| 131 | + if (!contextResolveParam) { |
| 132 | + return []; |
| 133 | + } |
| 134 | + const items = await languageServer.sendRequest( |
| 135 | + resolveContextRequest, |
| 136 | + contextResolveParam, |
| 137 | + token |
| 138 | + ); |
| 139 | + channel.trace(`Copilot context provider resolved ${items.length} items`); |
| 140 | + return items; |
| 141 | + }, |
| 142 | + }, |
| 143 | + }) |
| 144 | + ); |
| 145 | + |
| 146 | + channel.debug('Registration of C# context provider for GitHub Copilot extension succeeded.'); |
| 147 | + } catch (error) { |
| 148 | + channel.error('Failed to register Copilot context providers', error); |
| 149 | + } |
| 150 | + }); |
| 151 | +} |
0 commit comments