Skip to content

Commit

Permalink
introduce import selector in vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
sezna committed Dec 27, 2024
1 parent 546b053 commit 0e88d5d
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 22 deletions.
22 changes: 0 additions & 22 deletions library/Registry.md

This file was deleted.

9 changes: 9 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@
"command": "qsharp-vscode.populateFilesList",
"when": "resourceFilename == qsharp.json"
},
{
"command": "qsharp-vscode.importExternalPackage",
"when": "resourceFilename == qsharp.json"
},
{
"command": "qsharp-vscode.addProjectReference",
"when": "resourceFilename == qsharp.json"
Expand Down Expand Up @@ -415,6 +419,11 @@
"category": "Q#",
"title": "Populate qsharp.json files list"
},
{
"command": "qsharp-vscode.importExternalPackage",
"category": "Q#",
"title": "Import external Q# package"
},
{
"command": "qsharp-vscode.addProjectReference",
"category": "Q#",
Expand Down
101 changes: 101 additions & 0 deletions vscode/src/createProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as vscode from "vscode";
import { log, samples } from "qsharp-lang";
import { EventType, sendTelemetryEvent } from "./telemetry";
import { qsharpExtensionId } from "./common";
import registryJson from "./registry.json";

export async function initProjectCreator(context: vscode.ExtensionContext) {
context.subscriptions.push(
Expand Down Expand Up @@ -167,6 +168,106 @@ export async function initProjectCreator(context: vscode.ExtensionContext) {
),
);

context.subscriptions.push(
vscode.commands.registerCommand(
`${qsharpExtensionId}.importExternalPackage`,
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
log.info("importExternalPackage called with", qsharpJsonUri);

qsharpJsonUri =
qsharpJsonUri ?? vscode.window.activeTextEditor?.document.uri;
if (!qsharpJsonUri) {
log.error(
"populateFilesList called, but argument or active editor is not qsharp.json",
);
return;
}

// First, verify the qsharp.json can be opened and is a valid json file
const qsharpJsonDoc =
await vscode.workspace.openTextDocument(qsharpJsonUri);
if (!qsharpJsonDoc) {
log.error("Unable to open the qsharp.json file at ", qsharpJsonDoc);
return;
}

let manifestObj: any = {};
try {
manifestObj = JSON.parse(qsharpJsonDoc.getText());
} catch {
await vscode.window.showErrorMessage(
`Unable to parse the contents of ${qsharpJsonUri.path}`,
);
return;
}

// ask the user to pick a package to import
const packageChoice = await vscode.window.showQuickPick(
registryJson.knownPackages.map(
(pkg: {
name: string;
description: string;
dependency: object;
}) => ({
label: pkg.name,
description: pkg.description,
}),
),
{ placeHolder: "Pick a package to import" },
);

if (!packageChoice) {
log.info("User cancelled package choice");
return;
}

const chosenPackage = registryJson.knownPackages.find(
(pkg: { name: string; description: string; dependency: object }) =>
pkg.name === packageChoice.label,
)!;

const versionChoice = await vscode.window.showQuickPick(
chosenPackage.dependency.github.refs,
{ placeHolder: "Pick a version to import" },
);

if (!versionChoice) {
log.info("User cancelled version choice");
return;
}

// Update the dependencies property of the qsharp.json and write back to the document
if (!manifestObj["dependencies"]) manifestObj["dependencies"] = {};
manifestObj["dependencies"][packageChoice.label] = {
github: {
ref: versionChoice,
...chosenPackage.dependency.github,
refs: undefined,
},
};

// Apply the edits to the qsharp.json
const edit = new vscode.WorkspaceEdit();
edit.replace(
qsharpJsonUri,
new vscode.Range(0, 0, qsharpJsonDoc.lineCount, 0),
JSON.stringify(manifestObj, null, 2),
);
if (!(await vscode.workspace.applyEdit(edit))) {
vscode.window.showErrorMessage(
"Unable to update the qsharp.json file. Check the file is writable",
);
return;
}

// Bring the qsharp.json to the front for the user to save
await vscode.window.showTextDocument(qsharpJsonDoc);
},
),
);

type LocalProjectRef = {
path: string; // Absolute or relative path to the project dir
};
Expand Down
52 changes: 52 additions & 0 deletions vscode/src/registry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"knownPackages": [
{
"name": "Signed",
"description": "Defines types and functions to work with signed qubit-based integers.",
"dependency": {
"github": {
"owner": "Microsoft",
"repo": "qsharp",
"refs": ["546b053"],
"path": "library/signed"
}
}
},
{
"name": "FixedPoint",
"description": "Types and functions for fixed-point arithmetic with qubits.",
"dependency": {
"github": {
"owner": "Microsoft",
"repo": "qsharp",
"refs": ["546b053"],
"path": "library/signed"
}
}
},
{
"name": "Rotations",
"description": "Defines types and functions to work with rotations.",
"dependency": {
"github": {
"owner": "Microsoft",
"repo": "qsharp",
"refs": ["546b053"],
"path": "library/rotations"
}
}
},
{
"name": "Qtest",
"description": "Utilities for writing and running Q# tests.",
"dependency": {
"github": {
"owner": "Microsoft",
"repo": "qsharp",
"refs": ["546b053"],
"path": "library/qtest"
}
}
}
]
}

0 comments on commit 0e88d5d

Please sign in to comment.