Skip to content

Commit ca12b97

Browse files
authored
Migrate to Copilot related files API (#12735)
1 parent e4ae0b7 commit ca12b97

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

Extension/src/LanguageServer/extension.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ import { CppSettings } from './settings';
3333
import { LanguageStatusUI, getUI } from './ui';
3434
import { makeLspRange, rangeEquals, showInstallCompilerWalkthrough } from './utils';
3535

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+
3643
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
3744
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
3845
export const CppSourceStr: string = "C/C++";
@@ -183,7 +190,8 @@ export async function activate(): Promise<void> {
183190

184191
void clients.ActiveClient.ready.then(() => intervalTimer = global.setInterval(onInterval, 2500));
185192

186-
registerCommands(true);
193+
const isRelatedFilesApiEnabled = await telemetry.isExperimentEnabled("CppToolsRelatedFilesApi");
194+
registerCommands(true, isRelatedFilesApiEnabled);
187195

188196
vscode.tasks.onDidStartTask(() => getActiveClient().PauseCodeAnalysis());
189197

@@ -254,6 +262,23 @@ export async function activate(): Promise<void> {
254262
const tool = vscode.lm.registerTool('cpptools-lmtool-configuration', new CppConfigurationLanguageModelTool());
255263
disposables.push(tool);
256264
}
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+
}
257282
}
258283

259284
export function updateLanguageConfigurations(): void {
@@ -350,7 +375,7 @@ function onInterval(): void {
350375
/**
351376
* registered commands
352377
*/
353-
export function registerCommands(enabled: boolean): void {
378+
export function registerCommands(enabled: boolean, isRelatedFilesApiEnabled: boolean = false): void {
354379
commandDisposables.forEach(d => d.dispose());
355380
commandDisposables.length = 0;
356381
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.SwitchHeaderSource', enabled ? onSwitchHeaderSource : onDisabledCommand));
@@ -408,7 +433,10 @@ export function registerCommands(enabled: boolean): void {
408433
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToFreeFunction', enabled ? () => onExtractToFunction(true, false) : onDisabledCommand));
409434
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ExtractToMemberFunction', enabled ? () => onExtractToFunction(false, true) : onDisabledCommand));
410435
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+
}
412440
}
413441

414442
function onDisabledCommand() {
@@ -1378,3 +1406,20 @@ export async function getIncludes(maxDepth: number): Promise<any> {
13781406
const includes = await clients.ActiveClient.getIncludes(maxDepth);
13791407
return includes;
13801408
}
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

Comments
 (0)