Skip to content

Commit

Permalink
use common extension ID instead of hardcoded one
Browse files Browse the repository at this point in the history
  • Loading branch information
sezna committed Dec 12, 2024
1 parent 502ae34 commit 96e91ab
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 88 deletions.
65 changes: 36 additions & 29 deletions vscode/src/azure/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import { getQirForActiveWindow } from "../qirGeneration";
import { supportsAdaptive, targetSupportQir } from "./providerProperties";
import { startRefreshCycle } from "./treeRefresher";
import { getTokenForWorkspace } from "./auth";
import { qsharpExtensionId } from "../common";

const workspacesSecret = "qsharp-vscode.workspaces";
const workspacesSecret = `${qsharpExtensionId}.workspaces`;

export async function initAzureWorkspaces(context: vscode.ExtensionContext) {
const workspaceTreeProvider = new WorkspaceTreeProvider();
Expand Down Expand Up @@ -81,25 +82,25 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {

await vscode.commands.executeCommand(
"setContext",
"qsharp-vscode.treeItemSupportsQir",
`${qsharpExtensionId}.treeItemSupportsQir`,
supportsQir,
);
await vscode.commands.executeCommand(
"setContext",
"qsharp-vscode.treeItemSupportsDownload",
`${qsharpExtensionId}.treeItemSupportsDownload`,
supportsDownload,
);
await vscode.commands.executeCommand(
"setContext",
"qsharp-vscode.treeItemIsWorkspace",
`${qsharpExtensionId}.treeItemIsWorkspace`,
isWorkspace,
);
}),
);

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.targetSubmit",
`${qsharpExtensionId}.targetSubmit`,
async (arg: WorkspaceTreeItem) => {
// Could be run via the treeItem icon or the menu command.
const treeItem = arg || currentTreeItem;
Expand Down Expand Up @@ -156,17 +157,20 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {
);

context.subscriptions.push(
vscode.commands.registerCommand("qsharp-vscode.workspacesRefresh", () => {
// The user manually triggered a refresh. Start a cycle for each workspace
const workspaceIds = workspaceTreeProvider.getWorkspaceIds();

workspaceIds.forEach((id) => {
const workspace = workspaceTreeProvider.getWorkspace(id);
if (workspace) {
startRefreshCycle(workspaceTreeProvider, workspace);
}
});
}),
vscode.commands.registerCommand(
`${qsharpExtensionId}.workspacesRefresh`,
() => {
// The user manually triggered a refresh. Start a cycle for each workspace
const workspaceIds = workspaceTreeProvider.getWorkspaceIds();

workspaceIds.forEach((id) => {
const workspace = workspaceTreeProvider.getWorkspace(id);
if (workspace) {
startRefreshCycle(workspaceTreeProvider, workspace);
}
});
},
),
);

async function saveWorkspaceList() {
Expand Down Expand Up @@ -196,20 +200,23 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {
}

context.subscriptions.push(
vscode.commands.registerCommand("qsharp-vscode.workspacesAdd", async () => {
const workspace = await queryWorkspaces();
if (workspace) {
workspaceTreeProvider.updateWorkspace(workspace);
await saveWorkspaceList();
// Just kick off the refresh loop, no need to await
startRefreshCycle(workspaceTreeProvider, workspace);
}
}),
vscode.commands.registerCommand(
`${qsharpExtensionId}.workspacesAdd`,
async () => {
const workspace = await queryWorkspaces();
if (workspace) {
workspaceTreeProvider.updateWorkspace(workspace);
await saveWorkspaceList();
// Just kick off the refresh loop, no need to await
startRefreshCycle(workspaceTreeProvider, workspace);
}
},
),
);

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.workspacesRemove",
`${qsharpExtensionId}.workspacesRemove`,
async (arg: WorkspaceTreeItem) => {
// Could be run via the treeItem icon or the menu command.
const treeItem = arg || currentTreeItem;
Expand All @@ -224,7 +231,7 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.downloadResults",
`${qsharpExtensionId}.downloadResults`,
async (arg: WorkspaceTreeItem) => {
// Could be run via the treeItem icon or the menu command.
const treeItem = arg || currentTreeItem;
Expand Down Expand Up @@ -273,7 +280,7 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.workspacePythonCode",
`${qsharpExtensionId}.workspacePythonCode`,
async (arg: WorkspaceTreeItem) => {
// Could be run via the treeItem icon or the menu command.
const treeItem = arg || currentTreeItem;
Expand All @@ -300,7 +307,7 @@ export async function initAzureWorkspaces(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.workspaceOpenPortal",
`${qsharpExtensionId}.workspaceOpenPortal`,
async (arg: WorkspaceTreeItem) => {
// Could be run via the treeItem icon or the menu command.
const treeItem = arg || currentTreeItem;
Expand Down
7 changes: 4 additions & 3 deletions vscode/src/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import * as vscode from "vscode";
import { log, samples } from "qsharp-lang";
import { EventType, sendTelemetryEvent } from "./telemetry";
import { qsharpExtensionId } from "./common";

export async function initProjectCreator(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.createProject",
`${qsharpExtensionId}.createProject`,
async (folderUri: vscode.Uri | undefined) => {
sendTelemetryEvent(EventType.CreateProject, {}, {});

Expand Down Expand Up @@ -66,7 +67,7 @@ export async function initProjectCreator(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.populateFilesList",
`${qsharpExtensionId}.populateFilesList`,
async (qsharpJsonUri: vscode.Uri | undefined) => {
// If called from the content menu qsharpJsonUri will be the full qsharp.json uri
// If called from the command palette is will be undefined, so use the active editor
Expand Down Expand Up @@ -228,7 +229,7 @@ export async function initProjectCreator(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.addProjectReference",
`${qsharpExtensionId}.addProjectReference`,
async (qsharpJsonUri: vscode.Uri | undefined) => {
// If called from the content menu qsharpJsonUri will be the full qsharp.json uri
// If called from the command palette is will be undefined, so use the active editor
Expand Down
10 changes: 7 additions & 3 deletions vscode/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

import { IQSharpError, log, qsharpLibraryUriScheme } from "qsharp-lang";
import * as vscode from "vscode";
import { qsharpLanguageId, toVsCodeDiagnostic } from "./common.js";
import {
qsharpExtensionId,
qsharpLanguageId,
toVsCodeDiagnostic,
} from "./common.js";

/**
* Initialize diagnostics for `qsharp.json` files and failures
Expand Down Expand Up @@ -84,7 +88,7 @@ function startCommandDiagnostics(): vscode.Disposable[] {
vscode.languages.createDiagnosticCollection(qsharpLanguageId);

const dismissCommand = vscode.commands.registerCommand(
"qsharp-vscode.dismissCommandDiagnostics",
`${qsharpExtensionId}.dismissCommandDiagnostics`,
clearCommandDiagnostics,
);

Expand All @@ -102,7 +106,7 @@ function startCommandDiagnostics(): vscode.Disposable[] {
);
action.diagnostics = commandErrors;
action.command = {
command: "qsharp-vscode.dismissCommandDiagnostics",
command: `${qsharpExtensionId}.dismissCommandDiagnostics`,
title: "Dismiss errors for the last run Q# command",
};
action.isPreferred = true;
Expand Down
84 changes: 45 additions & 39 deletions vscode/src/memfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { log, samples } from "qsharp-lang";
import * as vscode from "vscode";
import { qsharpExtensionId } from "./common";

export const scheme = "qsharp-vfs";

Expand Down Expand Up @@ -69,7 +70,7 @@ export async function initFileSystem(context: vscode.ExtensionContext) {

context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.openPlayground",
`${qsharpExtensionId}.openPlayground`,
async () => {
await vscode.commands.executeCommand(
"vscode.openFolder",
Expand All @@ -80,47 +81,52 @@ export async function initFileSystem(context: vscode.ExtensionContext) {
);

context.subscriptions.push(
vscode.commands.registerCommand("qsharp-vscode.webOpener", async (uri) => {
log.debug(`webOpener called with URI ${uri}`);

// Open the README if the user has navigated to the playground
if (typeof uri === "string" && uri.endsWith("/playground/")) {
// Nice to have: First check if the readme is already open from a prior visit
await vscode.commands.executeCommand(
"markdown.showPreview",
playgroundRootUri.with({ path: "/README.md" }),
);
return;
}
vscode.commands.registerCommand(
`${qsharpExtensionId}.webOpener`,
async (uri) => {
log.debug(`webOpener called with URI ${uri}`);

// Open the README if the user has navigated to the playground
if (typeof uri === "string" && uri.endsWith("/playground/")) {
// Nice to have: First check if the readme is already open from a prior visit
await vscode.commands.executeCommand(
"markdown.showPreview",
playgroundRootUri.with({ path: "/README.md" }),
);
return;
}

// Example: https://vscode.dev/quantum?code=H4sIAAAAAAAAEz2Ouw6CQBRE%2B%2F2KITbQSI%2BNhRYWhkc0FoTiBm5kE9kld3c1xPDvEjSe8mQmM2mKS68dWtsxXuTgehLu8NQEwrU6RZFShgZ2I7WM81QGMj4Mhdi70IC3UljYH42XqbDa%2BDhZjR1ZyGtrcCZt4gQZKnbh4etmKeFHusznhzzDTbRnTDYIys33Tc%2FC239S2AcxqJvdqmY1qw8FRbBxvAAAAA%3D%3D
let linkedCode: string | undefined;
if (typeof uri === "string") {
const uriObj = vscode.Uri.parse(uri);
log.debug("uri query component: " + uriObj.query);

// The query appears to be URIDecoded already, which is causing issues with URLSearchParams, so extract with a regex for now.
const code = uriObj.query.match(/code=([^&]*)/)?.[1];

if (code) {
log.debug("code from query: " + code);
try {
linkedCode = await compressedBase64ToCode(code);
const codeFile = vscode.Uri.parse(`${scheme}:/code.qs`);

const encoder = new TextEncoder();
vfs.writeFile(codeFile, encoder.encode(linkedCode), {
create: true,
overwrite: true,
});
await vscode.commands.executeCommand("vscode.open", codeFile);
await vscode.commands.executeCommand("qsharp-vscode.showHelp");
} catch (err) {
log.warn("Unable to decode the code in the URL. ", err);
// Example: https://vscode.dev/quantum?code=H4sIAAAAAAAAEz2Ouw6CQBRE%2B%2F2KITbQSI%2BNhRYWhkc0FoTiBm5kE9kld3c1xPDvEjSe8mQmM2mKS68dWtsxXuTgehLu8NQEwrU6RZFShgZ2I7WM81QGMj4Mhdi70IC3UljYH42XqbDa%2BDhZjR1ZyGtrcCZt4gQZKnbh4etmKeFHusznhzzDTbRnTDYIys33Tc%2FC239S2AcxqJvdqmY1qw8FRbBxvAAAAA%3D%3D
let linkedCode: string | undefined;
if (typeof uri === "string") {
const uriObj = vscode.Uri.parse(uri);
log.debug("uri query component: " + uriObj.query);

// The query appears to be URIDecoded already, which is causing issues with URLSearchParams, so extract with a regex for now.
const code = uriObj.query.match(/code=([^&]*)/)?.[1];

if (code) {
log.debug("code from query: " + code);
try {
linkedCode = await compressedBase64ToCode(code);
const codeFile = vscode.Uri.parse(`${scheme}:/code.qs`);

const encoder = new TextEncoder();
vfs.writeFile(codeFile, encoder.encode(linkedCode), {
create: true,
overwrite: true,
});
await vscode.commands.executeCommand("vscode.open", codeFile);
await vscode.commands.executeCommand(
`${qsharpExtensionId}.showHelp`,
);
} catch (err) {
log.warn("Unable to decode the code in the URL. ", err);
}
}
}
}
}),
},
),
);
}

Expand Down
4 changes: 2 additions & 2 deletions vscode/src/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { log } from "qsharp-lang";
import * as vscode from "vscode";
import { WorkspaceTreeProvider } from "./azure/treeView.js";
import { getPythonCodeForWorkspace } from "./azure/workspaceActions.js";
import { qsharpLanguageId } from "./common.js";
import { qsharpExtensionId, qsharpLanguageId } from "./common.js";
import { notebookTemplate } from "./notebookTemplate.js";

const qsharpCellMagic = "%%qsharp";
Expand Down Expand Up @@ -130,7 +130,7 @@ export function registerCreateNotebookCommand(
) {
context.subscriptions.push(
vscode.commands.registerCommand(
"qsharp-vscode.createNotebook",
`${qsharpExtensionId}.createNotebook`,
async () => {
// Update the workspace connection info in the notebook if workspaces are already connected to
const tree = WorkspaceTreeProvider.instance;
Expand Down
3 changes: 2 additions & 1 deletion vscode/src/qirGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { invokeAndReportCommandDiagnostics } from "./diagnostics";
import { getActiveProgram } from "./programConfig";
import { EventType, sendTelemetryEvent } from "./telemetry";
import { getRandomGuid } from "./utils";
import { qsharpExtensionId } from "./common";

const generateQirTimeoutMs = 120000;

Expand Down Expand Up @@ -142,7 +143,7 @@ export function initCodegen(context: vscode.ExtensionContext) {
).toString();

context.subscriptions.push(
vscode.commands.registerCommand("qsharp-vscode.getQir", async () => {
vscode.commands.registerCommand(`${qsharpExtensionId}.getQir`, async () => {
try {
const qir = await getQirForActiveWindow();
const qirDoc = await vscode.workspace.openTextDocument({
Expand Down
6 changes: 3 additions & 3 deletions vscode/src/statusbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { log, TargetProfile } from "qsharp-lang";
import * as vscode from "vscode";
import { isQsharpDocument } from "./common";
import { isQsharpDocument, qsharpExtensionId } from "./common";
import { getTarget, getTargetFriendlyName, setTarget } from "./config";
import { getActiveQSharpDocumentUri } from "./programConfig";

Expand All @@ -18,7 +18,7 @@ export function activateTargetProfileStatusBarItem(): vscode.Disposable[] {
);
disposables.push(statusBarItem);

statusBarItem.command = "qsharp-vscode.setTargetProfile";
statusBarItem.command = `${qsharpExtensionId}.setTargetProfile`;

disposables.push(
vscode.window.onDidChangeActiveTextEditor((editor) => {
Expand Down Expand Up @@ -75,7 +75,7 @@ export function activateTargetProfileStatusBarItem(): vscode.Disposable[] {

function registerTargetProfileCommand() {
return vscode.commands.registerCommand(
"qsharp-vscode.setTargetProfile",
`${qsharpExtensionId}.setTargetProfile`,
async () => {
const target = await vscode.window.showQuickPick(
targetProfiles.map((profile) => ({
Expand Down
Loading

0 comments on commit 96e91ab

Please sign in to comment.