Skip to content

Commit

Permalink
code snippet provider
Browse files Browse the repository at this point in the history
  • Loading branch information
lukka committed Jan 13, 2025
1 parent fed7a82 commit 9ee7b88
Show file tree
Hide file tree
Showing 8 changed files with 1,164 additions and 727 deletions.
1 change: 1 addition & 0 deletions Extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6589,6 +6589,7 @@
"xml2js": "^0.6.2"
},
"dependencies": {
"@github/copilot-language-server": "^1.253.0",
"@vscode/extension-telemetry": "^0.9.6",
"chokidar": "^3.6.0",
"comment-json": "^4.2.3",
Expand Down
34 changes: 31 additions & 3 deletions Extension/src/LanguageServer/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ import {
} from './codeAnalysis';
import { Location, TextEdit, WorkspaceEdit } from './commonTypes';
import * as configs from './configurations';
import { CopilotCompletionContextProvider } from './copilotCompletionContextProvider';
import { DataBinding } from './dataBinding';
import { cachedEditorConfigSettings, getEditorConfigSettings } from './editorConfig';
import { CppSourceStr, clients, configPrefix, updateLanguageConfigurations, usesCrashHandler, watchForCrashes } from './extension';
import { CppSourceStr, SnippetEntry, clients, configPrefix, updateLanguageConfigurations, usesCrashHandler, watchForCrashes } from './extension';
import { LocalizeStringParams, getLocaleId, getLocalizedString } from './localization';
import { PersistentFolderState, PersistentState, PersistentWorkspaceState } from './persistentState';
import { RequestCancelled, ServerCancelled, createProtocolFilter } from './protocolFilter';
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 @@ -575,6 +577,18 @@ interface FilesEncodingChanged {
foldersFilesEncoding: FolderFilesEncodingChanged[];
}

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

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

// 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 @@ -597,6 +611,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 @@ -832,6 +847,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, token: vscode.CancellationToken): Promise<CopilotCompletionContextResult>;
}

export function createClient(workspaceFolder?: vscode.WorkspaceFolder): Client {
Expand Down Expand Up @@ -866,6 +882,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 @@ -1344,6 +1361,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 @@ -1875,6 +1895,7 @@ export class DefaultClient implements Client {
if (diagnosticsCollectionIntelliSense) {
diagnosticsCollectionIntelliSense.delete(document.uri);
}
this.copilotCompletionProvider?.removeFile(uri);
openFileVersions.delete(uri);
}

Expand Down Expand Up @@ -2254,7 +2275,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 @@ -2323,6 +2343,12 @@ export class DefaultClient implements Client {
() => this.languageClient.sendRequest(CppContextRequest, params, token), token);
}

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

/**
* a Promise that can be awaited to know when it's ok to proceed.
*
Expand Down Expand Up @@ -2695,7 +2721,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 @@ -4251,4 +4278,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, token: vscode.CancellationToken): Promise<CopilotCompletionContextResult> { return Promise.resolve({} as CopilotCompletionContextResult); }
}
Loading

0 comments on commit 9ee7b88

Please sign in to comment.