Skip to content

Commit 3a2f3ec

Browse files
authored
Port input completion provider to Rego (#1505)
Tiny one, but it's honest work! Signed-off-by: Anders Eknert <anders@styra.com>
1 parent d2b4b94 commit 3a2f3ec

6 files changed

Lines changed: 82 additions & 129 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# METADATA
2+
# description: provides completion suggestions for the `input` keyword where applicable
3+
package regal.lsp.completion.providers.input
4+
5+
import data.regal.lsp.completion.kind
6+
import data.regal.lsp.completion.location
7+
8+
# METADATA
9+
# description: all completion suggestions for the input keyword
10+
items contains item if {
11+
position := location.to_position(input.regal.context.location)
12+
line := input.regal.file.lines[position.line]
13+
14+
line != ""
15+
location.in_rule_body(line)
16+
17+
word := location.word_at(line, input.regal.context.location.col)
18+
19+
startswith("input", word.text)
20+
21+
item := {
22+
"label": "input",
23+
"kind": kind.keyword,
24+
"detail": "input document",
25+
"textEdit": {
26+
"range": location.word_range(word, position),
27+
"newText": "input",
28+
},
29+
"documentation": {
30+
"kind": "markdown",
31+
"value": _doc,
32+
},
33+
}
34+
}
35+
36+
_doc := `# input
37+
38+
'input' refers to the input document being evaluated.
39+
It is a special keyword that allows you to access the data sent to OPA at evaluation time.
40+
41+
To see more examples of how to use 'input', check out the
42+
[policy language documentation](https://www.openpolicyagent.org/docs/latest/policy-language/).
43+
44+
You can also experiment with input in the [Rego Playground](https://play.openpolicyagent.org/).
45+
`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package regal.lsp.completion.providers.input_test
2+
3+
import data.regal.lsp.completion.providers.input as provider
4+
import data.regal.lsp.completion.providers.test_utils as util
5+
6+
test_input_completion_on_typing if {
7+
policy := `package policy
8+
9+
allow if {
10+
i
11+
}`
12+
items := provider.items with input as util.input_with_location(policy, {"row": 4, "col": 3})
13+
14+
items == {{
15+
"detail": "input document",
16+
"documentation": {
17+
"kind": "markdown",
18+
"value": provider._doc,
19+
},
20+
"kind": 14,
21+
"label": "input",
22+
"textEdit": {
23+
"newText": "input",
24+
"range": {
25+
"end": {
26+
"character": 2,
27+
"line": 3,
28+
},
29+
"start": {
30+
"character": 1,
31+
"line": 3,
32+
},
33+
},
34+
},
35+
}}
36+
}

internal/lsp/completions/manager.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ func NewDefaultManager(ctx context.Context, c *cache.Cache, store storage.Store)
3636
m.RegisterProvider(&providers.PackageRefs{})
3737
m.RegisterProvider(&providers.RuleHead{})
3838
m.RegisterProvider(&providers.RuleHeadKeyword{})
39-
m.RegisterProvider(&providers.Input{})
4039

4140
m.RegisterProvider(providers.NewPolicy(ctx, store))
4241

internal/lsp/completions/providers/input.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

internal/lsp/completions/providers/input_test.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

internal/lsp/completions/providers/policy_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ allow if {
140140
labels = append(labels, item.Label)
141141
}
142142

143-
expected := []string{"example", "example.foo"}
143+
expected := []string{"input", "example", "example.foo"}
144144
if !slices.Equal(expected, labels) {
145145
t.Fatalf("expected %v, got %v", expected, labels)
146146
}

0 commit comments

Comments
 (0)