microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/vscode/src/language-service/codeLens.ts
79lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import { |
| 5 | ICodeLens, |
| 6 | ILanguageService, |
| 7 | qsharpLibraryUriScheme, |
| 8 | } from "qsharp-lang"; |
| 9 | import * as vscode from "vscode"; |
| 10 | import { toVsCodeRange } from "../common"; |
| 11 | |
| 12 | export function createQdkCodeLensProvider(languageService: ILanguageService) { |
| 13 | return new CodeLensProvider(languageService, mapCodeLens); |
| 14 | } |
| 15 | |
| 16 | class CodeLensProvider implements vscode.CodeLensProvider { |
| 17 | constructor( |
| 18 | public languageService: ILanguageService, |
| 19 | private commandMapper: (value: ICodeLens) => vscode.CodeLens, |
| 20 | ) {} |
| 21 | // We could raise events when code lenses change, |
| 22 | // but there's no need as the editor seems to query often enough. |
| 23 | // onDidChangeCodeLenses?: vscode.Event<void> | undefined; |
| 24 | async provideCodeLenses( |
| 25 | document: vscode.TextDocument, |
| 26 | ): Promise<vscode.CodeLens[]> { |
| 27 | if (document.uri.scheme === qsharpLibraryUriScheme) { |
| 28 | // Don't show any code lenses for library files, none of the actions |
| 29 | // would work since compiling library files through the editor is unsupported. |
| 30 | return []; |
| 31 | } |
| 32 | |
| 33 | const codeLenses = await this.languageService.getCodeLenses( |
| 34 | document.uri.toString(), |
| 35 | ); |
| 36 | |
| 37 | return codeLenses.map((cl) => this.commandMapper(cl)); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | function mapCodeLens(cl: ICodeLens): vscode.CodeLens { |
| 42 | let command; |
| 43 | let title; |
| 44 | let tooltip; |
| 45 | switch (cl.command) { |
| 46 | case "histogram": |
| 47 | title = "Histogram"; |
| 48 | command = "qsharp-vscode.showHistogram"; |
| 49 | tooltip = "Run and show histogram"; |
| 50 | break; |
| 51 | case "estimate": |
| 52 | title = "Estimate"; |
| 53 | command = "qsharp-vscode.showRe"; |
| 54 | tooltip = "Calculate resource estimates"; |
| 55 | break; |
| 56 | case "debug": |
| 57 | title = "Debug"; |
| 58 | command = "qsharp-vscode.debugProgram"; |
| 59 | tooltip = "Debug callable"; |
| 60 | break; |
| 61 | case "run": |
| 62 | title = "Run"; |
| 63 | command = "qsharp-vscode.runProgram"; |
| 64 | tooltip = "Run callable"; |
| 65 | break; |
| 66 | case "circuit": |
| 67 | title = "Circuit"; |
| 68 | command = "qsharp-vscode.showCircuit"; |
| 69 | tooltip = "Show circuit"; |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | return new vscode.CodeLens(toVsCodeRange(cl.range), { |
| 74 | title, |
| 75 | command, |
| 76 | arguments: cl.args ?? [], |
| 77 | tooltip, |
| 78 | }); |
| 79 | } |
| 80 | |