Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code snippet provider #13018

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6590,6 +6590,7 @@
"xml2js": "^0.6.2"
},
"dependencies": {
"@github/copilot-language-server": "^1.253.0",
lukka marked this conversation as resolved.
Show resolved Hide resolved
"@vscode/extension-telemetry": "^0.9.6",
"chokidar": "^3.6.0",
"comment-json": "^4.2.3",
Expand Down
37 changes: 35 additions & 2 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
} from './codeAnalysis';
import { Location, TextEdit, WorkspaceEdit } from './commonTypes';
import * as configs from './configurations';
import { CopilotCompletionContextFeatureFlag, CopilotCompletionContextProvider, SnippetEntry } from './copilotCompletionContextProvider';
import { DataBinding } from './dataBinding';
import { cachedEditorConfigSettings, getEditorConfigSettings } from './editorConfig';
import { CppSourceStr, clients, configPrefix, updateLanguageConfigurations, usesCrashHandler, watchForCrashes } from './extension';
Expand Down Expand Up @@ -183,6 +184,7 @@ interface TelemetryPayload {
event: string;
properties?: Record<string, string>;
metrics?: Record<string, number>;
signedMetrics?: Record<string, number>;
}

interface ReportStatusNotificationBody extends WorkspaceFolderParams {
Expand Down Expand Up @@ -577,6 +579,21 @@ interface FilesEncodingChanged {
foldersFilesEncoding: FolderFilesEncodingChanged[];
}

export interface CopilotCompletionContextResult {
requestId: number;
isResultMissing: boolean;
snippets: SnippetEntry[];
translationUnitUri: string;
caretOffset: number;
featureFlag: CopilotCompletionContextFeatureFlag;
}

export interface CopilotCompletionContextParams {
uri: string;
caretOffset: number;
featureFlag: CopilotCompletionContextFeatureFlag;
}

// Requests
const PreInitializationRequest: RequestType<void, string, void> = new RequestType<void, string, void>('cpptools/preinitialize');
const InitializationRequest: RequestType<CppInitializationParams, void, void> = new RequestType<CppInitializationParams, void, void>('cpptools/initialize');
Expand All @@ -599,6 +616,7 @@ const ChangeCppPropertiesRequest: RequestType<CppPropertiesParams, void, void> =
const IncludesRequest: RequestType<GetIncludesParams, GetIncludesResult, void> = new RequestType<GetIncludesParams, GetIncludesResult, void>('cpptools/getIncludes');
const CppContextRequest: RequestType<TextDocumentIdentifier, ChatContextResult, void> = new RequestType<TextDocumentIdentifier, ChatContextResult, void>('cpptools/getChatContext');
const ProjectContextRequest: RequestType<TextDocumentIdentifier, ProjectContextResult, void> = new RequestType<TextDocumentIdentifier, ProjectContextResult, void>('cpptools/getProjectContext');
const CopilotCompletionContextRequest: RequestType<CopilotCompletionContextParams, CopilotCompletionContextResult, void> = new RequestType<CopilotCompletionContextParams, CopilotCompletionContextResult, void>('cpptools/getCompletionContext');

// Notifications to the server
const DidOpenNotification: NotificationType<DidOpenTextDocumentParams> = new NotificationType<DidOpenTextDocumentParams>('textDocument/didOpen');
Expand Down Expand Up @@ -834,6 +852,7 @@ export interface Client {
getChatContext(uri: vscode.Uri, token: vscode.CancellationToken): Promise<ChatContextResult>;
getProjectContext(uri: vscode.Uri): Promise<ProjectContextResult>;
filesEncodingChanged(filesEncodingChanged: FilesEncodingChanged): void;
getCompletionContext(fileName: vscode.Uri, caretOffset: number, featureFlag: CopilotCompletionContextFeatureFlag, token: vscode.CancellationToken): Promise<CopilotCompletionContextResult>;
}

export function createClient(workspaceFolder?: vscode.WorkspaceFolder): Client {
Expand Down Expand Up @@ -868,6 +887,7 @@ export class DefaultClient implements Client {
private configurationProvider?: string;
private hoverProvider: HoverProvider | undefined;
private copilotHoverProvider: CopilotHoverProvider | undefined;
private copilotCompletionProvider?: CopilotCompletionContextProvider;

public lastCustomBrowseConfiguration: PersistentFolderState<WorkspaceBrowseConfiguration | undefined> | undefined;
public lastCustomBrowseConfigurationProviderId: PersistentFolderState<string | undefined> | undefined;
Expand Down Expand Up @@ -1346,6 +1366,9 @@ export class DefaultClient implements Client {
this.semanticTokensProviderDisposable = vscode.languages.registerDocumentSemanticTokensProvider(util.documentSelector, this.semanticTokensProvider, semanticTokensLegend);
}

this.copilotCompletionProvider = await CopilotCompletionContextProvider.Create();
this.disposables.push(this.copilotCompletionProvider);

// Listen for messages from the language server.
this.registerNotifications();

Expand Down Expand Up @@ -1878,6 +1901,7 @@ export class DefaultClient implements Client {
if (diagnosticsCollectionIntelliSense) {
diagnosticsCollectionIntelliSense.delete(document.uri);
}
this.copilotCompletionProvider?.removeFile(uri);
openFileVersions.delete(uri);
}

Expand Down Expand Up @@ -2259,7 +2283,6 @@ export class DefaultClient implements Client {
return util.extractCompilerPathAndArgs(!!settings.legacyCompilerArgsBehavior,
this.configuration.CurrentConfiguration?.compilerPath,
this.configuration.CurrentConfiguration?.compilerArgs);

}

public async getVcpkgInstalled(): Promise<boolean> {
Expand Down Expand Up @@ -2328,6 +2351,14 @@ export class DefaultClient implements Client {
() => this.languageClient.sendRequest(CppContextRequest, params, token), token);
}

public async getCompletionContext(file: vscode.Uri, caretOffset: number, featureFlag: CopilotCompletionContextFeatureFlag,
token: vscode.CancellationToken): Promise<CopilotCompletionContextResult> {
await withCancellation(this.ready, token);
return DefaultClient.withLspCancellationHandling(
() => this.languageClient.sendRequest(CopilotCompletionContextRequest,
{ uri: file.toString(), caretOffset, featureFlag }, token), token);
}

/**
* a Promise that can be awaited to know when it's ok to proceed.
*
Expand Down Expand Up @@ -2700,7 +2731,8 @@ export class DefaultClient implements Client {
if (notificationBody.event === "includeSquiggles" && this.configurationProvider && notificationBody.properties) {
notificationBody.properties["providerId"] = this.configurationProvider;
}
telemetry.logLanguageServerEvent(notificationBody.event, notificationBody.properties, notificationBody.metrics);
const metrics_unified: Record<string, number> = { ...notificationBody.metrics, ...notificationBody.signedMetrics };
telemetry.logLanguageServerEvent(notificationBody.event, notificationBody.properties, metrics_unified);
}

private async updateStatus(notificationBody: ReportStatusNotificationBody): Promise<void> {
Expand Down Expand Up @@ -4256,4 +4288,5 @@ class NullClient implements Client {
getChatContext(uri: vscode.Uri, token: vscode.CancellationToken): Promise<ChatContextResult> { return Promise.resolve({} as ChatContextResult); }
getProjectContext(uri: vscode.Uri): Promise<ProjectContextResult> { return Promise.resolve({} as ProjectContextResult); }
filesEncodingChanged(filesEncodingChanged: FilesEncodingChanged): void { }
getCompletionContext(file: vscode.Uri, caretOffset: number, featureFlag: CopilotCompletionContextFeatureFlag, token: vscode.CancellationToken): Promise<CopilotCompletionContextResult> { return Promise.resolve({} as CopilotCompletionContextResult); }
}
Loading
Loading