Skip to content

Commit 0ef5409

Browse files
committed
Minor code cleanup
1 parent b0f5f46 commit 0ef5409

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

Extension/src/LanguageServer/copilotCompletionContextProvider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { getOutputChannelLogger, Logger } from '../logger';
1111
import * as telemetry from '../telemetry';
1212
import { CopilotCompletionContextResult } from './client';
1313
import { CopilotCompletionContextTelemetry } from './copilotCompletionContextTelemetry';
14-
import { getCopilotApi, getCopilotChatApi, type CopilotChatApi } from './copilotProviders';
14+
import { getCopilotChatApi, getCopilotClientApi, type CopilotContextProviderAPI } from './copilotProviders';
1515
import { clients } from './extension';
1616
import { CppSettings } from './settings';
1717

@@ -449,7 +449,7 @@ ${copilotCompletionContext?.areSnippetsMissing ? "(missing code snippets)" : ""}
449449
const properties: Record<string, string> = {};
450450
const registerCopilotContextProvider = 'registerCopilotContextProvider';
451451
try {
452-
const copilotApi = await getCopilotApi();
452+
const copilotApi = await getCopilotClientApi();
453453
const copilotChatApi = await getCopilotChatApi();
454454
if (!copilotApi && !copilotChatApi) { throw new CopilotContextProviderException("getCopilotApi() returned null, Copilot is missing or inactive."); }
455455
const contextProvider = {
@@ -486,8 +486,8 @@ ${copilotCompletionContext?.areSnippetsMissing ? "(missing code snippets)" : ""}
486486
}
487487
}
488488

489-
private async installContextProvider(copilotAPI: CopilotChatApi, contextProvider: ContextProvider<SupportedContextItem>): Promise<{ hasGetContextProviderAPI: boolean; hasAPI: boolean }> {
490-
const hasGetContextProviderAPI = "getContextProviderAPI" in copilotAPI;
489+
private async installContextProvider(copilotAPI: CopilotContextProviderAPI, contextProvider: ContextProvider<SupportedContextItem>): Promise<{ hasGetContextProviderAPI: boolean; hasAPI: boolean }> {
490+
const hasGetContextProviderAPI = typeof copilotAPI.getContextProviderAPI === 'function';
491491
if (hasGetContextProviderAPI) {
492492
const contextAPI = await copilotAPI.getContextProviderAPI("v1");
493493
if (contextAPI) {

Extension/src/LanguageServer/copilotProviders.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ export interface CopilotTrait {
2424
promptTextOverride?: string;
2525
}
2626

27-
export interface CopilotChatApi {
27+
export interface CopilotContextProviderAPI {
2828
getContextProviderAPI(version: string): Promise<ContextProviderApiV1 | undefined>;
2929
}
3030

31-
export interface CopilotApi extends CopilotChatApi {
31+
export interface CopilotApi extends CopilotContextProviderAPI {
3232
registerRelatedFilesProvider(
3333
providerId: { extensionId: string; languageId: string },
3434
callback: (
@@ -40,7 +40,7 @@ export interface CopilotApi extends CopilotChatApi {
4040
}
4141

4242
export async function registerRelatedFilesProvider(): Promise<void> {
43-
const api = await getCopilotApi();
43+
const api = await getCopilotClientApi();
4444
if (util.extensionContext && api) {
4545
try {
4646
for (const languageId of ['c', 'cpp', 'cuda-cpp']) {
@@ -132,7 +132,7 @@ async function getIncludes(uri: vscode.Uri, maxDepth: number): Promise<GetInclud
132132
return includes;
133133
}
134134

135-
export async function getCopilotApi(): Promise<CopilotApi | undefined> {
135+
export async function getCopilotClientApi(): Promise<CopilotApi | undefined> {
136136
const copilotExtension = vscode.extensions.getExtension<CopilotApi>('github.copilot');
137137
if (!copilotExtension) {
138138
return undefined;
@@ -149,19 +149,30 @@ export async function getCopilotApi(): Promise<CopilotApi | undefined> {
149149
}
150150
}
151151

152-
export async function getCopilotChatApi(): Promise<CopilotChatApi | undefined> {
152+
export async function getCopilotChatApi(): Promise<CopilotContextProviderAPI | undefined> {
153+
type CopilotChatApi = { getAPI?(version: number): CopilotContextProviderAPI | undefined };
153154
const copilotExtension = vscode.extensions.getExtension<CopilotChatApi>('github.copilot-chat');
154155
if (!copilotExtension) {
155156
return undefined;
156157
}
157158

159+
let exports: CopilotChatApi | undefined;
158160
if (!copilotExtension.isActive) {
159161
try {
160-
return await copilotExtension.activate();
162+
exports = await copilotExtension.activate();
161163
} catch {
162164
return undefined;
163165
}
164166
} else {
165-
return copilotExtension.exports;
167+
exports = copilotExtension.exports;
168+
}
169+
if (!exports || typeof exports.getAPI !== 'function') {
170+
return undefined;
166171
}
172+
const result = exports.getAPI(1);
173+
return result;
174+
}
175+
176+
interface Disposable {
177+
dispose(): void;
167178
}

0 commit comments

Comments
 (0)