Skip to content

Commit fc0dc04

Browse files
authoredAug 13, 2024··
Document the Code Lens Evaluation feature (#983)
Signed-off-by: Anders Eknert <anders@styra.com>
1 parent 04b8a75 commit fc0dc04

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,8 @@ The Regal language server currently supports the following LSP features:
648648
- [x] [use-rego-v1](https://docs.styra.com/regal/rules/imports/use-rego-v1)
649649
- [x] [use-assignment-operator](https://docs.styra.com/regal/rules/style/use-assignment-operator)
650650
- [x] [no-whitespace-comment](https://docs.styra.com/regal/rules/style/no-whitespace-comment)
651+
- [x] [Code lenses](https://github.com/StyraInc/regal/blob/main/docs/language-server.md#code-lenses-evaluation)
652+
(click to evaluate any package or rule directly in the editor)
651653

652654
See the
653655
[documentation page for the language server](https://github.com/StyraInc/regal/blob/main/docs/language-server.md)

‎docs/assets/lsp/evalcodelens.png

130 KB
Loading

‎docs/assets/lsp/evalcodelensprint.png

151 KB
Loading

‎docs/editor-support.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ The plugin will automatically find and use [Regal config](https://docs.styra.com
1515
[Zed](https://zed.dev) is a modern open-source code editor with focus on performance and simplicity.
1616

1717
Zed supports Rego via Regal and the [zed-rego](https://github.com/StyraInc/zed-rego) extension developed by the Styra
18-
community. The extension provides syntax highlighting, linting, and all the other language server features provided by
19-
Regal.
18+
community. The extension provides syntax highlighting, linting, and most of the other language server features provided
19+
by Regal.
2020

2121
## Neovim
2222

‎docs/language-server.md

+48
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,54 @@ Regal currently provides quick fix code actions for the following linter rules:
138138
- [use-assignment-operator](https://docs.styra.com/regal/rules/style/use-assignment-operator)
139139
- [no-whitespace-comment](https://docs.styra.com/regal/rules/style/no-whitespace-comment)
140140

141+
### Code lenses (Evaluation)
142+
143+
The code lens feature provides language servers a way to add actionable commands just next to the code that the action
144+
belongs to. Regal provides code lenses for doing **evaluation** of any package or rule directly in the editor. This
145+
allows for an extremely fast feedback loop, where you can see the result of writing of modifying rules directly as you
146+
work with them, and without having to launch external commands or terminals. In any editor that supports code lenses,
147+
simply press `Evaluate` on top of a package or rule declaration to have it evaluated. The result is displayed on the
148+
same line.
149+
150+
<img
151+
src={require('./assets/lsp/evalcodelens.png').default}
152+
alt="Screenshot of evaluation performed via code lens"/>
153+
154+
Once evaluation has completed, the result is also pretty-printed in a tooltip when hovering the rule. This is
155+
particularly useful when the result contains more data than can fit on a single line!
156+
157+
Note that when evaluating incrementally defined rules, the result reflects evaluation of the whole **document**,
158+
not a single rule definition. To make this clear, the result will be displayed next to each definition of the
159+
same rule.
160+
161+
In addition to showing the result of evaluation, the "Evaluate" code lens will also display the output of any
162+
`print` calls made in rule bodies. This can be really helpful when trying to figure out _why_ the rule evaluated
163+
the way it did, or where rule evaluation failed.
164+
165+
<img
166+
src={require('./assets/lsp/evalcodelensprint.png').default}
167+
alt="Screenshot of evaluation with print call performed via code lens"/>
168+
169+
Policy evaluation often depends on **input**. This can be provided via an `input.json` file which Regal will search
170+
for first in the same directory as the policy file evaluated. If not found there, Regal will proceed to search each
171+
parent directory up until the workspace root directory. It is recommended to add `input.json` to your `.gitignore`
172+
file so that you can work freely with evaluation in any directory without having your input accidentally commited.
173+
174+
#### Editor support
175+
176+
While the code lens feature is part of the LSP specification, the action that is triggered by a code lens
177+
isn't necessarily part of the standard. The language server protocol does not provide a native method for requesting
178+
evaluation, so Regal will handle that on its own, and differently depending on what the client supports.
179+
180+
- Currently, only the [OPA VS Code extension](https://github.com/open-policy-agent/vscode-opa) is capable of handling
181+
the request to display evaluation results on the same line as the package or rule evaluated.
182+
- [Zed](https://github.com/StyraInc/zed-rego) does not support the code lens feature at all at this point in time. As
183+
soon as it does, Regal will provide them.
184+
- Displaying the result of evaluation requires customized code in the client. Currently only VS Code has the required
185+
modifications to handle this, and is thus the only editor to currently support "inline display" of the result.
186+
For other editors that support the code lens feature, Regal will instead write the result of evaluation to an
187+
`output.json` file.
188+
141189
## Unsupported features
142190

143191
See the

0 commit comments

Comments
 (0)
Please sign in to comment.