-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
input.json
completion provider (#1005)
This provider suggests completions based on the nested attributes found in the `input.json` file, if such a file exists. Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
f28ac7d
commit 1fecb4c
Showing
8 changed files
with
211 additions
and
22 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
bundle/regal/lsp/completion/providers/inputdotjson/inputdotjson.rego
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,60 @@ | ||
package regal.lsp.completion.providers.inputdotjson | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.kind | ||
import data.regal.lsp.completion.location | ||
|
||
# METADATA | ||
# description: returns suggestions based on input.json structure (if found) | ||
items contains item if { | ||
input.regal.context.input_dot_json_path | ||
|
||
position := location.to_position(input.regal.context.location) | ||
line := input.regal.file.lines[position.line] | ||
word := location.ref_at(line, input.regal.context.location.col) | ||
|
||
some [suggestion, type] in _matching_input_suggestions | ||
|
||
item := { | ||
"label": suggestion, | ||
"kind": kind.variable, | ||
"detail": type, | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": sprintf("(inferred from [`input.json`](%s))", [input.regal.context.input_dot_json_path]), | ||
}, | ||
"textEdit": { | ||
"range": location.word_range(word, position), | ||
"newText": suggestion, | ||
}, | ||
} | ||
} | ||
|
||
_matching_input_suggestions contains [suggestion, type] if { | ||
position := location.to_position(input.regal.context.location) | ||
line := input.regal.file.lines[position.line] | ||
|
||
line != "" | ||
location.in_rule_body(line) | ||
|
||
word := location.ref_at(line, input.regal.context.location.col) | ||
|
||
some [suggestion, type] in _input_paths | ||
|
||
startswith(suggestion, word.text) | ||
} | ||
|
||
_input_paths contains [input_path, input_type] if { | ||
walk(input.regal.context.input_dot_json, [path, value]) | ||
|
||
count(path) > 0 | ||
|
||
# don't traverse into arrays | ||
every value in path { | ||
is_string(value) | ||
} | ||
|
||
input_type := type_name(value) | ||
input_path := concat(".", ["input", concat(".", path)]) | ||
} |
102 changes: 102 additions & 0 deletions
102
bundle/regal/lsp/completion/providers/inputdotjson/inputdotjson_test.rego
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,102 @@ | ||
package regal.lsp.completion.providers.inputdotjson_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.providers.inputdotjson as provider | ||
|
||
# regal ignore:rule-length | ||
test_matching_input_suggestions if { | ||
items := provider.items with input as input_obj | ||
items == { | ||
{ | ||
"detail": "object", | ||
"kind": 6, | ||
"label": "input.request", | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "(inferred from [`input.json`](/foo/bar/input.json))", | ||
}, | ||
"textEdit": { | ||
"newText": "input.request", | ||
"range": { | ||
"end": {"character": 13, "line": 5}, | ||
"start": {"character": 6, "line": 5}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"detail": "string", | ||
"kind": 6, | ||
"label": "input.request.method", | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "(inferred from [`input.json`](/foo/bar/input.json))", | ||
}, | ||
"textEdit": { | ||
"newText": "input.request.method", | ||
"range": { | ||
"end": {"character": 13, "line": 5}, | ||
"start": {"character": 6, "line": 5}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"detail": "string", | ||
"kind": 6, | ||
"label": "input.request.url", | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": "(inferred from [`input.json`](/foo/bar/input.json))", | ||
}, | ||
"textEdit": { | ||
"newText": "input.request.url", | ||
"range": { | ||
"end": {"character": 13, "line": 5}, | ||
"start": {"character": 6, "line": 5}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
test_not_matching_input_suggestions if { | ||
input_obj_new_loc := object.union(input_obj, {"regal": {"context": {"location": { | ||
"row": 1, | ||
"col": 1, | ||
}}}}) | ||
items := provider.items with input as input_obj_new_loc | ||
items == set() | ||
} | ||
|
||
input_obj := {"regal": { | ||
"context": { | ||
"location": { | ||
"row": 6, | ||
"col": 12, | ||
}, | ||
"input_dot_json": { | ||
"user": { | ||
"name": { | ||
"first": "John", | ||
"last": "Doe", | ||
}, | ||
"email": "[email protected]", | ||
"roles": [{"name": "admin"}, {"name": "user"}], | ||
}, | ||
"request": { | ||
"method": "GET", | ||
"url": "https://example.com", | ||
}, | ||
}, | ||
"input_dot_json_path": "/foo/bar/input.json", | ||
}, | ||
"file": {"lines": [ | ||
"package p", | ||
"", | ||
"import rego.v1", | ||
"", | ||
"allow if {", | ||
" f(input.r", | ||
"}", | ||
]}, | ||
}} |
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
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
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
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
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
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