microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/vscode/src/language-service/codeActions.ts
67lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import { ILanguageService, ICodeAction } from "qsharp-lang"; |
| 5 | import * as vscode from "vscode"; |
| 6 | import { toVsCodeWorkspaceEdit } from "../common"; |
| 7 | |
| 8 | export function createCodeActionsProvider(languageService: ILanguageService) { |
| 9 | return new QSharpCodeActionProvider(languageService); |
| 10 | } |
| 11 | |
| 12 | class QSharpCodeActionProvider implements vscode.CodeActionProvider { |
| 13 | constructor(public languageService: ILanguageService) {} |
| 14 | async provideCodeActions( |
| 15 | document: vscode.TextDocument, |
| 16 | range: vscode.Range | vscode.Selection, |
| 17 | ) { |
| 18 | const iCodeActions = await this.languageService.getCodeActions( |
| 19 | document.uri.toString(), |
| 20 | range, |
| 21 | ); |
| 22 | |
| 23 | // Convert language-service type to vscode type |
| 24 | return iCodeActions.map(toCodeAction); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | function toCodeAction(iCodeAction: ICodeAction): vscode.CodeAction { |
| 29 | const codeAction = new vscode.CodeAction( |
| 30 | iCodeAction.title, |
| 31 | toCodeActionKind(iCodeAction.kind), |
| 32 | ); |
| 33 | if (iCodeAction.edit) { |
| 34 | codeAction.edit = toVsCodeWorkspaceEdit(iCodeAction.edit); |
| 35 | } |
| 36 | codeAction.isPreferred = iCodeAction.isPreferred; |
| 37 | return codeAction; |
| 38 | } |
| 39 | |
| 40 | function toCodeActionKind( |
| 41 | codeActionKind?: string, |
| 42 | ): vscode.CodeActionKind | undefined { |
| 43 | switch (codeActionKind) { |
| 44 | case "Empty": |
| 45 | return vscode.CodeActionKind.Empty; |
| 46 | case "QuickFix": |
| 47 | return vscode.CodeActionKind.QuickFix; |
| 48 | case "Refactor": |
| 49 | return vscode.CodeActionKind.Refactor; |
| 50 | case "RefactorExtract": |
| 51 | return vscode.CodeActionKind.RefactorExtract; |
| 52 | case "RefactorInline": |
| 53 | return vscode.CodeActionKind.RefactorInline; |
| 54 | case "RefactorMove": |
| 55 | return vscode.CodeActionKind.RefactorMove; |
| 56 | case "RefactorRewrite": |
| 57 | return vscode.CodeActionKind.RefactorRewrite; |
| 58 | case "Source": |
| 59 | return vscode.CodeActionKind.Source; |
| 60 | case "SourceOrganizeImports": |
| 61 | return vscode.CodeActionKind.SourceOrganizeImports; |
| 62 | case "SourceFixAll": |
| 63 | return vscode.CodeActionKind.SourceFixAll; |
| 64 | case "Notebook": |
| 65 | return vscode.CodeActionKind.Notebook; |
| 66 | } |
| 67 | } |
| 68 | |