-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Following the excellent work on debugger support by @johanfylling, this PR introduces a code lens for debugging, which triggers a command that simply returns a launch configuration where the package or rule clicked is set as the entrypoint. The code lens provider is now also rewritten in Rego. Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
d754094
commit 822e42c
Showing
7 changed files
with
381 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# METADATA | ||
# description: | | ||
# the code lens provider decides where code lenses should be placed in the given input file | ||
# schemas: | ||
# - input: schema.regal.ast | ||
package regal.lsp.codelens | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
import data.regal.util | ||
|
||
import data.regal.lsp.util.location | ||
|
||
# code lenses are displayed in the order they come back in the returned | ||
# array, and 'evaluate' somehow feels better to the left of 'debug' | ||
lenses := array.concat( | ||
[l | some l in _eval_lenses], | ||
[l | some l in _debug_lenses], | ||
) | ||
|
||
_eval_lenses contains { | ||
"range": location.to_range(result.ranged_location_from_text(input["package"]).location), | ||
"command": { | ||
"title": "Evaluate", | ||
"command": "regal.eval", | ||
"arguments": [ | ||
input.regal.file.name, | ||
ast.ref_to_string(input["package"].path), | ||
util.to_location_object(input["package"].location).row, | ||
], | ||
}, | ||
} | ||
|
||
_eval_lenses contains _rule_lens(rule, "regal.eval", "Evaluate") if { | ||
some rule in ast.rules | ||
} | ||
|
||
_debug_lenses contains { | ||
"range": location.to_range(result.ranged_location_from_text(input["package"]).location), | ||
"command": { | ||
"title": "Debug", | ||
"command": "regal.debug", | ||
"arguments": [ | ||
input.regal.file.name, | ||
ast.ref_to_string(input["package"].path), | ||
util.to_location_object(input["package"].location).row, | ||
], | ||
}, | ||
} | ||
|
||
_debug_lenses contains _rule_lens(rule, "regal.debug", "Debug") if { | ||
some rule in ast.rules | ||
|
||
# no need to add a debug lens for a rule like `pi := 3.14` | ||
not _unconditional_constant(rule) | ||
} | ||
|
||
_rule_lens(rule, command, title) := { | ||
"range": location.to_range(result.ranged_location_from_text(rule).location), | ||
"command": { | ||
"title": title, | ||
"command": command, | ||
"arguments": [ | ||
input.regal.file.name, # regal ignore:external-reference | ||
sprintf("%s.%s", [ast.ref_to_string(input["package"].path), ast.ref_static_to_string(rule.head.ref)]), | ||
util.to_location_object(rule.head.location).row, | ||
], | ||
}, | ||
} | ||
|
||
_rule_lens_args(filename, rule) := [ | ||
filename, | ||
sprintf("%s.%s", [ast.ref_to_string(input["package"].path), ast.ref_static_to_string(rule.head.ref)]), | ||
util.to_location_object(rule.head.location).row, | ||
] | ||
|
||
_unconditional_constant(rule) if { | ||
not rule.body | ||
ast.is_constant(rule.head.value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package regal.lsp.codelens_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.codelens | ||
|
||
# regal ignore:rule-length | ||
test_code_lenses_for_module if { | ||
module := regal.parse_module("policy.rego", ` | ||
package foo | ||
import rego.v1 | ||
rule1 := 1 | ||
rule2 if 1 + rule1 == 2 | ||
`) | ||
lenses := codelens.lenses with input as module | ||
|
||
lenses == [ | ||
{ | ||
"command": { | ||
"arguments": ["policy.rego", "data.foo", 2], | ||
"command": "regal.eval", | ||
"title": "Evaluate", | ||
}, | ||
"range": {"end": {"character": 8, "line": 1}, "start": {"character": 1, "line": 1}}, | ||
}, | ||
{ | ||
"command": { | ||
"arguments": ["policy.rego", "data.foo.rule1", 6], | ||
"command": "regal.eval", | ||
"title": "Evaluate", | ||
}, | ||
"range": {"end": {"character": 11, "line": 5}, "start": {"character": 1, "line": 5}}, | ||
}, | ||
{ | ||
"command": { | ||
"arguments": ["policy.rego", "data.foo.rule2", 8], | ||
"command": "regal.eval", "title": "Evaluate", | ||
}, | ||
"range": {"end": {"character": 24, "line": 7}, "start": {"character": 1, "line": 7}}, | ||
}, | ||
{ | ||
"command": { | ||
"arguments": ["policy.rego", "data.foo", 2], | ||
"command": "regal.debug", | ||
"title": "Debug", | ||
}, | ||
"range": {"end": {"character": 8, "line": 1}, "start": {"character": 1, "line": 1}}, | ||
}, | ||
{ | ||
"command": { | ||
"arguments": ["policy.rego", "data.foo.rule2", 8], | ||
"command": "regal.debug", | ||
"title": "Debug", | ||
}, | ||
"range": {"end": {"character": 24, "line": 7}, "start": {"character": 1, "line": 7}}, | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package regal.lsp.util.location | ||
|
||
import rego.v1 | ||
|
||
# METADATA | ||
# description: turns an AST location _with end attribute_ into an LSP range | ||
to_range(location) := { | ||
"start": { | ||
"line": location.row - 1, | ||
"character": location.col - 1, | ||
}, | ||
"end": { | ||
"line": location.end.row - 1, | ||
"character": location.end.col - 1, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.