microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/vscode/src/common.ts
153lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import { TextDocument, Uri, Range, Location } from "vscode"; |
| 5 | import { |
| 6 | getCompilerWorker, |
| 7 | ICompilerWorker, |
| 8 | ILocation, |
| 9 | IRange, |
| 10 | IWorkspaceEdit, |
| 11 | VSDiagnostic, |
| 12 | } from "qsharp-lang"; |
| 13 | import * as vscode from "vscode"; |
| 14 | |
| 15 | export const qsharpLanguageId = "qsharp"; |
| 16 | export const qsharpCircuitLanguageId = "qsharpcircuit"; |
| 17 | export const openqasmLanguageId = "openqasm"; |
| 18 | |
| 19 | // Returns true for all documents supported by the extension, including unsaved files, notebook cells, circuit files, qasm files, etc. |
| 20 | // excludes text documents we don't want to add support for at all, such as git/pr/chat "virtual" document views |
| 21 | export function isQdkDocument(document: TextDocument): boolean { |
| 22 | return ( |
| 23 | !isUnsupportedScheme(document.uri.scheme) && |
| 24 | isQdkSupportedLanguage(document) |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | function isQdkSupportedLanguage(document: TextDocument): boolean { |
| 29 | return ( |
| 30 | document.languageId === qsharpLanguageId || |
| 31 | document.languageId === qsharpCircuitLanguageId || |
| 32 | document.languageId === openqasmLanguageId |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | function isUnsupportedScheme(scheme: string): boolean { |
| 37 | return ( |
| 38 | scheme === "git" || |
| 39 | scheme === "pr" || |
| 40 | scheme === "review" || |
| 41 | scheme.startsWith("chat") |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | // Returns true for all Q# documents, including unsaved files, notebook cells, circuit files, etc. |
| 46 | export function isQsharpDocument(document: TextDocument): boolean { |
| 47 | return ( |
| 48 | !isUnsupportedScheme(document.uri.scheme) && |
| 49 | document.languageId === qsharpLanguageId |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | // Returns true for all circuit documents |
| 54 | export function isCircuitDocument(document: TextDocument): boolean { |
| 55 | return ( |
| 56 | !isUnsupportedScheme(document.uri.scheme) && |
| 57 | document.languageId === qsharpCircuitLanguageId |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | export function isQdkNotebookCell(document: TextDocument): boolean { |
| 62 | return isQdkDocument(document) && isNotebookCell(document); |
| 63 | } |
| 64 | |
| 65 | // Returns true for all OpenQASM documents, including unsaved files, notebook cells, etc. |
| 66 | export function isOpenQasmDocument(document: TextDocument): boolean { |
| 67 | return ( |
| 68 | !isUnsupportedScheme(document.uri.scheme) && |
| 69 | document.languageId === openqasmLanguageId |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | export function isNotebookCell(document: TextDocument): boolean { |
| 74 | return document.uri.scheme === "vscode-notebook-cell"; |
| 75 | } |
| 76 | |
| 77 | export const qsharpExtensionId = "qsharp-vscode"; |
| 78 | |
| 79 | export function basename(path: string): string | undefined { |
| 80 | return path.replace(/\/+$/, "").split("/").pop(); |
| 81 | } |
| 82 | |
| 83 | export function toVsCodeRange(range: IRange): Range { |
| 84 | return new Range( |
| 85 | range.start.line, |
| 86 | range.start.character, |
| 87 | range.end.line, |
| 88 | range.end.character, |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | export function toVsCodeLocation(location: ILocation): Location { |
| 93 | return new Location(Uri.parse(location.source), toVsCodeRange(location.span)); |
| 94 | } |
| 95 | |
| 96 | export function toVsCodeWorkspaceEdit( |
| 97 | iWorkspaceEdit: IWorkspaceEdit, |
| 98 | ): vscode.WorkspaceEdit { |
| 99 | const workspaceEdit = new vscode.WorkspaceEdit(); |
| 100 | for (const [source, edits] of iWorkspaceEdit.changes) { |
| 101 | const uri = vscode.Uri.parse(source, true); |
| 102 | const vsEdits = edits.map((edit) => { |
| 103 | return new vscode.TextEdit(toVsCodeRange(edit.range), edit.newText); |
| 104 | }); |
| 105 | workspaceEdit.set(uri, vsEdits); |
| 106 | } |
| 107 | return workspaceEdit; |
| 108 | } |
| 109 | |
| 110 | export function toVsCodeDiagnostic(d: VSDiagnostic): vscode.Diagnostic { |
| 111 | let severity; |
| 112 | switch (d.severity) { |
| 113 | case "error": |
| 114 | severity = vscode.DiagnosticSeverity.Error; |
| 115 | break; |
| 116 | case "warning": |
| 117 | severity = vscode.DiagnosticSeverity.Warning; |
| 118 | break; |
| 119 | case "info": |
| 120 | severity = vscode.DiagnosticSeverity.Information; |
| 121 | break; |
| 122 | } |
| 123 | const vscodeDiagnostic = new vscode.Diagnostic( |
| 124 | toVsCodeRange(d.range), |
| 125 | d.message, |
| 126 | severity, |
| 127 | ); |
| 128 | if (d.uri && d.code) { |
| 129 | vscodeDiagnostic.code = { |
| 130 | value: d.code, |
| 131 | target: vscode.Uri.parse(d.uri), |
| 132 | }; |
| 133 | } else if (d.code) { |
| 134 | vscodeDiagnostic.code = d.code; |
| 135 | } |
| 136 | if (d.related) { |
| 137 | vscodeDiagnostic.relatedInformation = d.related.map((r) => { |
| 138 | return new vscode.DiagnosticRelatedInformation( |
| 139 | toVsCodeLocation(r.location), |
| 140 | r.message, |
| 141 | ); |
| 142 | }); |
| 143 | } |
| 144 | return vscodeDiagnostic; |
| 145 | } |
| 146 | |
| 147 | export function loadCompilerWorker(extensionUri: vscode.Uri): ICompilerWorker { |
| 148 | const compilerWorkerScriptPath = vscode.Uri.joinPath( |
| 149 | extensionUri, |
| 150 | "./out/compilerWorker.js", |
| 151 | ).toString(); |
| 152 | return getCompilerWorker(compilerWorkerScriptPath); |
| 153 | } |
| 154 | |