diff --git a/library/Registry.md b/library/Registry.md deleted file mode 100644 index 7440e4379e..0000000000 --- a/library/Registry.md +++ /dev/null @@ -1,22 +0,0 @@ -# Q# Library Registry - -This document is a list of notable Q# library projects. If you have a library that you would like to add to this list, please open a pull request. If you have a Q# project that you'd like to share as a library, please see the [documentation on how to do this](https://learn.microsoft.com/en-us/azure/quantum/how-to-work-with-qsharp-projects?tabs=tabid-qsharp%2Ctabid-qsharp-run#configuring-q-projects-as-external-dependencies). - -## Unstable - -[link](https://github.com/microsoft/qsharp/tree/main/library/unstable) - -A general staging ground for some useful APIs that have not yet been stabilized into the standard library. - -## Signed - -[link](https://github.com/microsoft/qsharp/tree/main/library/signed) - -Defines types and functions to work with signed qubit-based integers. - -## FixedPoint - -[link](https://github.com/microsoft/qsharp/tree/main/library/fixed_point) - -Types and functions for fixed-point arithmetic with qubits. - diff --git a/vscode/package.json b/vscode/package.json index a96876a704..e54bb3da2d 100644 --- a/vscode/package.json +++ b/vscode/package.json @@ -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" @@ -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#", diff --git a/vscode/src/createProject.ts b/vscode/src/createProject.ts index 2d6f59e48f..7f8c4d7b6c 100644 --- a/vscode/src/createProject.ts +++ b/vscode/src/createProject.ts @@ -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( @@ -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 }; diff --git a/vscode/src/registry.json b/vscode/src/registry.json new file mode 100644 index 0000000000..94a4ffd95c --- /dev/null +++ b/vscode/src/registry.json @@ -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" + } + } + } + ] +}