@@ -33,6 +33,13 @@ import { CppSettings } from './settings';
33
33
import { LanguageStatusUI , getUI } from './ui' ;
34
34
import { makeLspRange , rangeEquals , showInstallCompilerWalkthrough } from './utils' ;
35
35
36
+ interface CopilotApi {
37
+ registerRelatedFilesProvider (
38
+ providerId : { extensionId : string ; languageId : string } ,
39
+ callback : ( uri : vscode . Uri ) => Promise < { entries : vscode . Uri [ ] ; traits ?: { name : string ; value : string } [ ] } >
40
+ ) : void ;
41
+ }
42
+
36
43
nls . config ( { messageFormat : nls . MessageFormat . bundle , bundleFormat : nls . BundleFormat . standalone } ) ( ) ;
37
44
const localize : nls . LocalizeFunc = nls . loadMessageBundle ( ) ;
38
45
export const CppSourceStr : string = "C/C++" ;
@@ -183,7 +190,8 @@ export async function activate(): Promise<void> {
183
190
184
191
void clients . ActiveClient . ready . then ( ( ) => intervalTimer = global . setInterval ( onInterval , 2500 ) ) ;
185
192
186
- registerCommands ( true ) ;
193
+ const isRelatedFilesApiEnabled = await telemetry . isExperimentEnabled ( "CppToolsRelatedFilesApi" ) ;
194
+ registerCommands ( true , isRelatedFilesApiEnabled ) ;
187
195
188
196
vscode . tasks . onDidStartTask ( ( ) => getActiveClient ( ) . PauseCodeAnalysis ( ) ) ;
189
197
@@ -254,6 +262,23 @@ export async function activate(): Promise<void> {
254
262
const tool = vscode . lm . registerTool ( 'cpptools-lmtool-configuration' , new CppConfigurationLanguageModelTool ( ) ) ;
255
263
disposables . push ( tool ) ;
256
264
}
265
+
266
+ if ( isRelatedFilesApiEnabled ) {
267
+ const api = await getCopilotApi ( ) ;
268
+ if ( util . extensionContext && api ) {
269
+ try {
270
+ for ( const languageId of [ 'c' , 'cpp' , 'cuda-cpp' ] ) {
271
+ api . registerRelatedFilesProvider (
272
+ { extensionId : util . extensionContext . extension . id , languageId } ,
273
+ async ( _uri : vscode . Uri ) =>
274
+ ( { entries : ( await clients . ActiveClient . getIncludes ( 1 ) ) ?. includedFiles . map ( file => vscode . Uri . file ( file ) ) ?? [ ] } )
275
+ ) ;
276
+ }
277
+ } catch {
278
+ console . log ( "Failed to register Copilot related files provider." ) ;
279
+ }
280
+ }
281
+ }
257
282
}
258
283
259
284
export function updateLanguageConfigurations ( ) : void {
@@ -350,7 +375,7 @@ function onInterval(): void {
350
375
/**
351
376
* registered commands
352
377
*/
353
- export function registerCommands ( enabled : boolean ) : void {
378
+ export function registerCommands ( enabled : boolean , isRelatedFilesApiEnabled : boolean = false ) : void {
354
379
commandDisposables . forEach ( d => d . dispose ( ) ) ;
355
380
commandDisposables . length = 0 ;
356
381
commandDisposables . push ( vscode . commands . registerCommand ( 'C_Cpp.SwitchHeaderSource' , enabled ? onSwitchHeaderSource : onDisabledCommand ) ) ;
@@ -408,7 +433,10 @@ export function registerCommands(enabled: boolean): void {
408
433
commandDisposables . push ( vscode . commands . registerCommand ( 'C_Cpp.ExtractToFreeFunction' , enabled ? ( ) => onExtractToFunction ( true , false ) : onDisabledCommand ) ) ;
409
434
commandDisposables . push ( vscode . commands . registerCommand ( 'C_Cpp.ExtractToMemberFunction' , enabled ? ( ) => onExtractToFunction ( false , true ) : onDisabledCommand ) ) ;
410
435
commandDisposables . push ( vscode . commands . registerCommand ( 'C_Cpp.ExpandSelection' , enabled ? ( r : Range ) => onExpandSelection ( r ) : onDisabledCommand ) ) ;
411
- commandDisposables . push ( vscode . commands . registerCommand ( 'C_Cpp.getIncludes' , enabled ? ( maxDepth : number ) => getIncludes ( maxDepth ) : ( ) => Promise . resolve ( ) ) ) ;
436
+
437
+ if ( ! isRelatedFilesApiEnabled ) {
438
+ commandDisposables . push ( vscode . commands . registerCommand ( 'C_Cpp.getIncludes' , enabled ? ( maxDepth : number ) => getIncludes ( maxDepth ) : ( ) => Promise . resolve ( ) ) ) ;
439
+ }
412
440
}
413
441
414
442
function onDisabledCommand ( ) {
@@ -1378,3 +1406,20 @@ export async function getIncludes(maxDepth: number): Promise<any> {
1378
1406
const includes = await clients . ActiveClient . getIncludes ( maxDepth ) ;
1379
1407
return includes ;
1380
1408
}
1409
+
1410
+ async function getCopilotApi ( ) : Promise < CopilotApi | undefined > {
1411
+ const copilotExtension = vscode . extensions . getExtension < CopilotApi > ( 'github.copilot' ) ;
1412
+ if ( ! copilotExtension ) {
1413
+ return undefined ;
1414
+ }
1415
+
1416
+ if ( ! copilotExtension . isActive ) {
1417
+ try {
1418
+ return await copilotExtension . activate ( ) ;
1419
+ } catch {
1420
+ return undefined ;
1421
+ }
1422
+ } else {
1423
+ return copilotExtension . exports ;
1424
+ }
1425
+ }
0 commit comments