Skip to content

Commit

Permalink
add custom snippets PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
lukka committed Oct 8, 2024
1 parent 292d84f commit d0439b4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
9 changes: 8 additions & 1 deletion Extension/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
"--skip-release-notes",
"--disable-workspace-trust",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionDevelopmentPath=C:/Users/lucappa/.vscode-insiders/extensions/copilot-client",
"--disable-extension=ms.vscode.cpptools",
"--disable-extension=github.synth-lab",
"--disable-extension=github.copilot",
"--disable-extension=github.copilot-nightly",
"--log=github.copilot:debug",
],
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/dist/**"
"${workspaceFolder}/dist/**",
"C:/Users/lucappa/.vscode-insiders/extensions/copilot-client/dist/**"
],
// you can use a watch task as a prelaunch task and it works like you'd want it to.
"preLaunchTask": "watch"
Expand Down
54 changes: 47 additions & 7 deletions Extension/src/LanguageServer/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ interface CopilotTrait {
promptTextOverride?: string;
}

interface SnippetEntry {
uri: string;
text: string;
startLine: number;
endLine: number;
}

interface CopilotApi {
registerRelatedFilesProvider(
providerId: { extensionId: string; languageId: string },
Expand All @@ -49,6 +56,15 @@ interface CopilotApi {
cancellationToken: vscode.CancellationToken
) => Promise<{ entries: vscode.Uri[]; traits?: CopilotTrait[] }>
): Disposable;
registerSnippetsProvider(
providerId: { extensionId: string; languageId: string },
callback: (
uri: vscode.Uri,
context: { flags: Record<string, unknown> },
cancellationToken: vscode.CancellationToken
) => Promise<{ entries: SnippetEntry[] }>
): Disposable;

}

nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
Expand Down Expand Up @@ -290,6 +306,30 @@ export async function activate(): Promise<void> {
}
}
}

const isCustomSnippetProviderApiEnabled = await telemetry.isExperimentEnabled("CppToolsCustomSnippetsApi");
if (isCustomSnippetProviderApiEnabled) {
const api = await getCopilotApi();
if (util.extensionContext && api) {
try {
for (const languageId of ['c', 'cpp', 'cuda-cpp']) {
api.registerSnippetsProvider(
{ extensionId: util.extensionContext.extension.id, languageId },
async (_uri: vscode.Uri, _context: { flags: Record<string, unknown> }, _: vscode.CancellationToken) => ({
entries: [{
uri: "file:///Users/username/Code/cpp/test.cpp",
text: "This is line 42\nThis is line 43\nThis is line 44\nThis is line 45\n",
startLine: 42,
endLine: 45
}]
})
);
}
} catch {
console.log("Failed to register Copilot related files provider.");
}
}
}
}

export function updateLanguageConfigurations(): void {
Expand All @@ -302,8 +342,8 @@ export function updateLanguageConfigurations(): void {
}

/**
* workspace events
*/
* workspace events
*/
async function onDidChangeSettings(event: vscode.ConfigurationChangeEvent): Promise<void> {
const client: Client = clients.getDefaultClient();
if (client instanceof DefaultClient) {
Expand Down Expand Up @@ -384,8 +424,8 @@ function onInterval(): void {
}

/**
* registered commands
*/
* registered commands
*/
export function registerCommands(enabled: boolean, isRelatedFilesApiEnabled: boolean): void {
commandDisposables.forEach(d => d.dispose());
commandDisposables.length = 0;
Expand Down Expand Up @@ -513,9 +553,9 @@ async function onSwitchHeaderSource(): Promise<void> {
}

/**
* Allow the user to select a workspace when multiple workspaces exist and get the corresponding Client back.
* The resulting client is used to handle some command that was previously invoked.
*/
* Allow the user to select a workspace when multiple workspaces exist and get the corresponding Client back.
* The resulting client is used to handle some command that was previously invoked.
*/
async function selectClient(): Promise<Client> {
if (clients.Count === 1) {
return clients.ActiveClient;
Expand Down

0 comments on commit d0439b4

Please sign in to comment.