|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | | -import * as vscode from 'vscode'; |
7 | | -import { CSharpExtensionId } from '../../constants/csharpExtensionId'; |
8 | | -import { CopilotRelatedDocumentsReport, CopilotRelatedDocumentsRequest } from '../server/roslynProtocol'; |
9 | 6 | import { RoslynLanguageServer } from '../server/roslynLanguageServer'; |
10 | | -import { UriConverter } from '../utils/uriConverter'; |
11 | | -import { TextDocumentIdentifier } from 'vscode-languageserver-protocol'; |
12 | | - |
13 | | -interface CopilotTrait { |
14 | | - name: string; |
15 | | - value: string; |
16 | | - includeInPrompt?: boolean; |
17 | | - promptTextOverride?: string; |
18 | | -} |
19 | | - |
20 | | -interface CopilotRelatedFilesProviderRegistration { |
21 | | - registerRelatedFilesProvider( |
22 | | - providerId: { extensionId: string; languageId: string }, |
23 | | - callback: ( |
24 | | - uri: vscode.Uri, |
25 | | - context: { flags: Record<string, unknown> }, |
26 | | - cancellationToken?: vscode.CancellationToken |
27 | | - ) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }> |
28 | | - ): vscode.Disposable; |
29 | | -} |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import { registerCopilotContextProviders } from './contextProviders'; |
| 9 | +import { registerCopilotRelatedFilesProvider } from './relatedFilesProvider'; |
30 | 10 |
|
31 | | -export function registerCopilotExtension(languageServer: RoslynLanguageServer, channel: vscode.LogOutputChannel) { |
| 11 | +export function registerCopilotExtensions( |
| 12 | + context: vscode.ExtensionContext, |
| 13 | + languageServer: RoslynLanguageServer, |
| 14 | + copilotPluginPath: string | undefined, |
| 15 | + channel: vscode.LogOutputChannel |
| 16 | +) { |
32 | 17 | const ext = vscode.extensions.getExtension('github.copilot'); |
33 | 18 | if (!ext) { |
34 | | - channel.debug('GitHub Copilot extension not installed. Skip registeration of C# related files provider.'); |
| 19 | + channel.debug('GitHub Copilot extension not installed. Skip registeration of Copilot related functionalities.'); |
35 | 20 | return; |
36 | 21 | } |
37 | | - ext.activate().then(() => { |
38 | | - const relatedAPI = ext.exports as CopilotRelatedFilesProviderRegistration | undefined; |
39 | | - if (!relatedAPI) { |
40 | | - channel.debug( |
41 | | - 'Incompatible GitHub Copilot extension installed. Skip registeration of C# related files provider.' |
42 | | - ); |
43 | | - return; |
44 | | - } |
45 | | - |
46 | | - channel.debug('registration of C# related files provider for GitHub Copilot extension succeeded.'); |
47 | | - |
48 | | - const id = { |
49 | | - extensionId: CSharpExtensionId, |
50 | | - languageId: 'csharp', |
51 | | - }; |
52 | 22 |
|
53 | | - relatedAPI.registerRelatedFilesProvider(id, async (uri, _, token) => { |
54 | | - const buildResult = ( |
55 | | - activeDocumentUri: vscode.Uri, |
56 | | - reports: CopilotRelatedDocumentsReport[], |
57 | | - builder: vscode.Uri[] |
58 | | - ) => { |
59 | | - if (reports) { |
60 | | - for (const report of reports) { |
61 | | - if (report._vs_file_paths) { |
62 | | - for (const filePath of report._vs_file_paths) { |
63 | | - // The Roslyn related document service would return the active document as related file to itself |
64 | | - // if the code contains reference to the types defined in the same document. Skip it so the active file |
65 | | - // won't be used as additonal context. |
66 | | - const relatedUri = vscode.Uri.file(filePath); |
67 | | - if (relatedUri.fsPath !== activeDocumentUri.fsPath) { |
68 | | - builder.push(relatedUri); |
69 | | - } |
70 | | - } |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - }; |
75 | | - const relatedFiles: vscode.Uri[] = []; |
76 | | - const uriString = UriConverter.serialize(uri); |
77 | | - const textDocument = TextDocumentIdentifier.create(uriString); |
| 23 | + ext.activate().then(async (copilotExt) => { |
| 24 | + if (copilotPluginPath) { |
78 | 25 | try { |
79 | | - await languageServer.sendRequestWithProgress( |
80 | | - CopilotRelatedDocumentsRequest.type, |
81 | | - { |
82 | | - _vs_textDocument: textDocument, |
83 | | - position: { |
84 | | - line: 0, |
85 | | - character: 0, |
86 | | - }, |
87 | | - }, |
88 | | - async (r) => buildResult(uri, r, relatedFiles), |
89 | | - token |
90 | | - ); |
91 | | - } catch (e) { |
92 | | - if (e instanceof Error) { |
93 | | - channel.appendLine(e.message); |
94 | | - } |
| 26 | + await registerCopilotContextProviders(copilotExt, context, languageServer, channel); |
| 27 | + } catch (error) { |
| 28 | + channel.error('Failed to register Copilot context providers', error); |
95 | 29 | } |
96 | | - return { entries: relatedFiles }; |
97 | | - }); |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + await registerCopilotRelatedFilesProvider(copilotExt, context, languageServer, channel); |
| 34 | + } catch (error) { |
| 35 | + channel.error('Failed to register Copilot context providers', error); |
| 36 | + } |
98 | 37 | }); |
99 | 38 | } |
0 commit comments