|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +/** |
| 3 | + * Vörðr VSCode Extension |
| 4 | + * Fully ported to ReScript v12 |
| 5 | + */ |
| 6 | + |
| 7 | +module VsCode = { |
| 8 | + type disposable = {"dispose": unit => unit} |
| 9 | + |
| 10 | + type extensionContext = { |
| 11 | + subscriptions: array<disposable>, |
| 12 | + asAbsolutePath: string => string, |
| 13 | + extensionPath: string, |
| 14 | + } |
| 15 | + |
| 16 | + type textDocument = { |
| 17 | + languageId: string, |
| 18 | + fileName: string, |
| 19 | + } |
| 20 | + |
| 21 | + type textEditor = {document: textDocument} |
| 22 | + |
| 23 | + module Window = { |
| 24 | + @module("vscode") @scope("window") |
| 25 | + external showInputBox: {"prompt": string, "placeHolder": string} => promise<option<string>> = |
| 26 | + "showInputBox" |
| 27 | + |
| 28 | + @module("vscode") @scope("window") |
| 29 | + external registerCommand: (string, unit => promise<unit>) => disposable = "registerCommand" |
| 30 | + |
| 31 | + @module("vscode") @scope("window") @val |
| 32 | + external activeTextEditor: option<textEditor> = "activeTextEditor" |
| 33 | + } |
| 34 | + |
| 35 | + module Workspace = { |
| 36 | + type configuration = {get: string => bool} |
| 37 | + |
| 38 | + @module("vscode") @scope("workspace") |
| 39 | + external getConfiguration: string => configuration = "getConfiguration" |
| 40 | + |
| 41 | + @module("vscode") @scope("workspace") |
| 42 | + external createFileSystemWatcher: string => disposable = "createFileSystemWatcher" |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +module Lsp = { |
| 47 | + type languageClient |
| 48 | + type serverOptions = { |
| 49 | + run: {"command": string, "args": array<string>, "transport": int}, |
| 50 | + debug: {"command": string, "args": array<string>, "transport": int}, |
| 51 | + } |
| 52 | + type clientOptions = { |
| 53 | + documentSelector: array<{"scheme": string, "language": string}>, |
| 54 | + synchronize: {"fileEvents": VsCode.disposable}, |
| 55 | + } |
| 56 | + |
| 57 | + @module("vscode-languageclient/node") @new |
| 58 | + external makeLanguageClient: (string, string, serverOptions, clientOptions) => languageClient = |
| 59 | + "LanguageClient" |
| 60 | + |
| 61 | + @send external start: languageClient => unit = "start" |
| 62 | + @send external stop: languageClient => promise<unit> = "stop" |
| 63 | + @send |
| 64 | + external sendRequest: (languageClient, string, {"command": string, "arguments": array<string>}) => promise< |
| 65 | + unit, |
| 66 | + > = "sendRequest" |
| 67 | +} |
| 68 | + |
| 69 | +module Path = { |
| 70 | + @module("path") external join: (string, string) => string = "join" |
| 71 | +} |
| 72 | + |
| 73 | +let client: ref<option<Lsp.languageClient>> = ref(None) |
| 74 | + |
| 75 | +let signImage = async () => { |
| 76 | + switch (VsCode.Window.activeTextEditor, client.contents) { |
| 77 | + | (Some(_editor), Some(c)) => { |
| 78 | + let imageName = await VsCode.Window.showInputBox({ |
| 79 | + "prompt": "Enter container image name", |
| 80 | + "placeHolder": "alpine:latest", |
| 81 | + }) |
| 82 | + |
| 83 | + switch imageName { |
| 84 | + | Some(name) => |
| 85 | + await Lsp.sendRequest( |
| 86 | + c, |
| 87 | + "workspace/executeCommand", |
| 88 | + {"command": "vordr.signImage", "arguments": [name]}, |
| 89 | + ) |
| 90 | + | None => () |
| 91 | + } |
| 92 | + } |
| 93 | + | _ => () |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +let verifyImage = async () => { |
| 98 | + switch client.contents { |
| 99 | + | Some(c) => { |
| 100 | + let imageName = await VsCode.Window.showInputBox({ |
| 101 | + "prompt": "Enter container image name", |
| 102 | + "placeHolder": "alpine:latest", |
| 103 | + }) |
| 104 | + |
| 105 | + switch imageName { |
| 106 | + | Some(name) => |
| 107 | + await Lsp.sendRequest( |
| 108 | + c, |
| 109 | + "workspace/executeCommand", |
| 110 | + {"command": "vordr.verifyImage", "arguments": [name]}, |
| 111 | + ) |
| 112 | + | None => () |
| 113 | + } |
| 114 | + } |
| 115 | + | _ => () |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +let activate = (context: VsCode.extensionContext) => { |
| 120 | + let config = VsCode.Workspace.getConfiguration("vordr") |
| 121 | + |
| 122 | + if config.get("lsp.enable") { |
| 123 | + let lspPath = Path.join(context.extensionPath, "../../src/lsp/Main.res.js") |
| 124 | + |
| 125 | + let serverOptions: Lsp.serverOptions = { |
| 126 | + run: { |
| 127 | + "command": "deno", |
| 128 | + "args": ["run", "--allow-read", "--allow-write", lspPath], |
| 129 | + "transport": 1, // TransportKind.stdio |
| 130 | + }, |
| 131 | + debug: { |
| 132 | + "command": "deno", |
| 133 | + "args": ["run", "--allow-read", "--allow-write", lspPath], |
| 134 | + "transport": 1, |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + let clientOptions: Lsp.clientOptions = { |
| 139 | + documentSelector: [ |
| 140 | + {"scheme": "file", "language": "ctp"}, |
| 141 | + {"scheme": "file", "language": "gatekeeper"}, |
| 142 | + {"scheme": "file", "language": "dockerfile"}, |
| 143 | + ], |
| 144 | + synchronize: { |
| 145 | + "fileEvents": VsCode.Workspace.createFileSystemWatcher( |
| 146 | + "**/.{ctp,gatekeeper.yaml,gatekeeper.yml}", |
| 147 | + ), |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + let c = Lsp.makeLanguageClient("vordrLsp", "Vörðr Language Server", serverOptions, clientOptions) |
| 152 | + |
| 153 | + client := Some(c) |
| 154 | + Lsp.start(c) |
| 155 | + |
| 156 | + // Register commands |
| 157 | + let _ = Array.push(context.subscriptions, VsCode.Window.registerCommand("vordr.signImage", signImage)) |
| 158 | + let _ = Array.push( |
| 159 | + context.subscriptions, |
| 160 | + VsCode.Window.registerCommand("vordr.verifyImage", verifyImage), |
| 161 | + ) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +let deactivate = () => { |
| 166 | + switch client.contents { |
| 167 | + | None => Promise.resolve() |
| 168 | + | Some(c) => Lsp.stop(c) |
| 169 | + } |
| 170 | +} |
0 commit comments