@@ -28,71 +28,77 @@ interface CopilotRelatedFilesProviderRegistration {
2828 ) : vscode . Disposable ;
2929}
3030
31- export async function registerCopilotRelatedFilesProvider (
32- copilotExt : CopilotRelatedFilesProviderRegistration | undefined ,
31+ export function registerCopilotRelatedFilesProvider (
3332 context : vscode . ExtensionContext ,
3433 languageServer : RoslynLanguageServer ,
3534 channel : vscode . LogOutputChannel
3635) {
37- if ( ! copilotExt ) {
36+ const copilotApi = vscode . extensions . getExtension < CopilotRelatedFilesProviderRegistration > ( 'github.copilot' ) ;
37+ if ( ! copilotApi ) {
3838 channel . debug (
39- 'Incompatible GitHub Copilot extension installed. Skip registeration of C# related files provider.'
39+ 'Failed to find comnpatible version of GitHub Copilot extension installed. Skip registeration of Copilot related files provider.'
4040 ) ;
4141 return ;
4242 }
4343
44- const id = {
45- extensionId : CSharpExtensionId ,
46- languageId : 'csharp' ,
47- } ;
44+ copilotApi . activate ( ) . then ( async ( api ) => {
45+ try {
46+ const id = {
47+ extensionId : CSharpExtensionId ,
48+ languageId : 'csharp' ,
49+ } ;
4850
49- context . subscriptions . push (
50- copilotExt . registerRelatedFilesProvider ( id , async ( uri , _ , token ) => {
51- const buildResult = (
52- activeDocumentUri : vscode . Uri ,
53- reports : CopilotRelatedDocumentsReport [ ] ,
54- builder : vscode . Uri [ ]
55- ) => {
56- if ( reports ) {
57- for ( const report of reports ) {
58- if ( report . _vs_file_paths ) {
59- for ( const filePath of report . _vs_file_paths ) {
60- // The Roslyn related document service would return the active document as related file to itself
61- // if the code contains reference to the types defined in the same document. Skip it so the active file
62- // won't be used as additonal context.
63- const relatedUri = vscode . Uri . file ( filePath ) ;
64- if ( relatedUri . fsPath !== activeDocumentUri . fsPath ) {
65- builder . push ( relatedUri ) ;
51+ context . subscriptions . push (
52+ api . registerRelatedFilesProvider ( id , async ( uri , _ , token ) => {
53+ const buildResult = (
54+ activeDocumentUri : vscode . Uri ,
55+ reports : CopilotRelatedDocumentsReport [ ] ,
56+ builder : vscode . Uri [ ]
57+ ) => {
58+ if ( reports ) {
59+ for ( const report of reports ) {
60+ if ( report . _vs_file_paths ) {
61+ for ( const filePath of report . _vs_file_paths ) {
62+ // The Roslyn related document service would return the active document as related file to itself
63+ // if the code contains reference to the types defined in the same document. Skip it so the active file
64+ // won't be used as additonal context.
65+ const relatedUri = vscode . Uri . file ( filePath ) ;
66+ if ( relatedUri . fsPath !== activeDocumentUri . fsPath ) {
67+ builder . push ( relatedUri ) ;
68+ }
69+ }
6670 }
6771 }
6872 }
73+ } ;
74+ const relatedFiles : vscode . Uri [ ] = [ ] ;
75+ const uriString = UriConverter . serialize ( uri ) ;
76+ const textDocument = TextDocumentIdentifier . create ( uriString ) ;
77+ try {
78+ await languageServer . sendRequestWithProgress (
79+ CopilotRelatedDocumentsRequest . type ,
80+ {
81+ _vs_textDocument : textDocument ,
82+ position : {
83+ line : 0 ,
84+ character : 0 ,
85+ } ,
86+ } ,
87+ async ( r ) => buildResult ( uri , r , relatedFiles ) ,
88+ token
89+ ) ;
90+ } catch ( e ) {
91+ if ( e instanceof Error ) {
92+ channel . appendLine ( e . message ) ;
93+ }
6994 }
70- }
71- } ;
72- const relatedFiles : vscode . Uri [ ] = [ ] ;
73- const uriString = UriConverter . serialize ( uri ) ;
74- const textDocument = TextDocumentIdentifier . create ( uriString ) ;
75- try {
76- await languageServer . sendRequestWithProgress (
77- CopilotRelatedDocumentsRequest . type ,
78- {
79- _vs_textDocument : textDocument ,
80- position : {
81- line : 0 ,
82- character : 0 ,
83- } ,
84- } ,
85- async ( r ) => buildResult ( uri , r , relatedFiles ) ,
86- token
87- ) ;
88- } catch ( e ) {
89- if ( e instanceof Error ) {
90- channel . appendLine ( e . message ) ;
91- }
92- }
93- return { entries : relatedFiles } ;
94- } )
95- ) ;
95+ return { entries : relatedFiles } ;
96+ } )
97+ ) ;
9698
97- channel . debug ( 'Registration of C# related files provider for GitHub Copilot extension succeeded.' ) ;
99+ channel . debug ( 'Registration of C# related files provider for GitHub Copilot extension succeeded.' ) ;
100+ } catch ( error ) {
101+ channel . error ( 'Failed to register Copilot related files providers' , error ) ;
102+ }
103+ } ) ;
98104}
0 commit comments