From d316cfc78258b808d158b2f1a217a8c6d6231068 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Mon, 5 Aug 2024 09:18:48 +0200 Subject: [PATCH] Allow setting formatter via configuration Will work with Regal v0.25.0 once that is released. Signed-off-by: Anders Eknert --- README.md | 3 ++- package.json | 7 +++++++ src/ls/clients/regal.ts | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ebe4e8e..3760f3b 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Additionally, users may choose to install [Regal](https://docs.styra.com/regal), * Folding ranges (expand/collapse blocks, imports, comments) * Document and workspace symbols (navigate to rules, functions, packages) * Inlay hints (show names of built-in function arguments next to their values) -* Formatting +* Formatting (`opa fmt`, `opa fmt --rego-v1` or `regal fix`, see [Configuration](#configuration) below) * Code actions (quick fixes for linting issues) * Code completions @@ -56,6 +56,7 @@ or search for "Open Policy Agent" in the 'Extensions' panel. | `opa.schema` | `null` | Path to the [schema](https://www.openpolicyagent.org/docs/latest/policy-language/#using-schemas-to-enhance-the-rego-type-checker) file or directory. If set to `null`, schema evaluation is disabled. As for `opa.roots`, `${workspaceFolder}` and `${fileDirname}` variables can be used in the path. | | `opa.languageServers` | `null` | An array of enabled language servers (currently `["regal"]` is supported) | | `opa.env` | `{}` | Object of environment variables passed to the process running OPA (e.g. `{"key": "value"}`) | +| `opa.formatter` | `opa-fmt` | Name of the OPA formatter to use. Requires Regal. One of `opa-fmt`, `opa-fmt-rego-v1` and `regal-fix`. This value is sent as an initialization option to the language server, so the change won't take effect before the project (or VS Code) is reloaded. See the documentation for the [regal fix](https://docs.styra.com/regal/fixing) command for more information | Note that the `${workspaceFolder}` variable will expand to a full URI of the workspace, as expected by most VS Code commands. The `${workspacePath}` variable may additionally be used where only the path component (i.e. without the `file://` schema component) of the workspace URI is required. diff --git a/package.json b/package.json index b594642..9dbb919 100644 --- a/package.json +++ b/package.json @@ -153,6 +153,13 @@ }, "default": {}, "description": "Environment variables passed to the process running OPA." + }, + "opa.formatter": { + "type": [ + "string" + ], + "default": "opa-fmt", + "description": "The formatter to use for Rego. Supports: ['opa-fmt', 'opa-fmt-rego-v1', 'regal-fix']." } } }, diff --git a/src/ls/clients/regal.ts b/src/ls/clients/regal.ts index 2b42a09..86e9727 100644 --- a/src/ls/clients/regal.ts +++ b/src/ls/clients/regal.ts @@ -188,6 +188,9 @@ export function activateRegal(_context: ExtensionContext) { onChange: true, onSave: true, }, + initializationOptions: { + formatter: vscode.workspace.getConfiguration('opa').get('formatter', 'opa-fmt'), + } }; client = new LanguageClient(