-
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.
More Rego completion providers (#858)
Rules converted: - `commonrule` - `package` - `rego.v1` Also: - move some common functions into utility package. - add `Name()` to provider interface to be able to easily measure performance impact of a named provider Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
39e57db
commit 82fd171
Showing
26 changed files
with
389 additions
and
580 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
46 changes: 46 additions & 0 deletions
46
bundle/regal/lsp/completion/providers/commonrule/commonrule.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,46 @@ | ||
package regal.lsp.completion.providers.commonrule | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.kind | ||
import data.regal.lsp.completion.location | ||
|
||
suggested_names := { | ||
"allow", | ||
"authorized", | ||
"deny", | ||
} | ||
|
||
items contains item if { | ||
position := location.to_position(input.regal.context.location) | ||
line := input.regal.file.lines[position.line] | ||
|
||
some label in suggested_names | ||
|
||
invoke_suggestion(line, label) | ||
|
||
item := { | ||
"label": label, | ||
"kind": kind.snippet, | ||
"detail": "common name", | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": sprintf("%q is a common rule name", [label]), | ||
}, | ||
"textEdit": { | ||
"range": { | ||
"start": { | ||
"line": position.line, | ||
"character": 0, | ||
}, | ||
"end": position, | ||
}, | ||
"newText": sprintf("%s ", [label]), | ||
}, | ||
} | ||
} | ||
|
||
invoke_suggestion("", _) | ||
|
||
# regal ignore:external-reference | ||
invoke_suggestion(line, label) if startswith(label, line) |
62 changes: 62 additions & 0 deletions
62
bundle/regal/lsp/completion/providers/commonrule/commonrule_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,62 @@ | ||
package regal.lsp.completion.providers.commonrule_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.providers.commonrule as provider | ||
import data.regal.lsp.completion.providers.utils_test as util | ||
|
||
test_common_name_completion_on_invoked if { | ||
policy := `package policy | ||
import rego.v1 | ||
` | ||
module := regal.parse_module("p.rego", policy) | ||
items := provider.items with input as util.input_module_with_location(module, policy, {"row": 5, "col": 2}) | ||
|
||
expected_item(items, "allow") | ||
expected_item(items, "deny") | ||
expected_item(items, "authorized") | ||
} | ||
|
||
test_common_name_completion_on_typed if { | ||
policy := `package policy | ||
import rego.v1 | ||
` | ||
module := regal.parse_module("p.rego", policy) | ||
new_policy := concat("", [policy, "d"]) | ||
items := provider.items with input as util.input_module_with_location(module, new_policy, {"row": 5, "col": 2}) | ||
|
||
expected_item(items, "deny") | ||
} | ||
|
||
expected_item(items, label) if { | ||
item := { | ||
"label": label, | ||
"detail": "common name", | ||
"documentation": { | ||
"kind": "markdown", | ||
"value": sprintf("%q is a common rule name", [label]), | ||
}, | ||
"kind": 15, | ||
"textEdit": { | ||
"range": { | ||
"start": { | ||
"line": 4, | ||
"character": 0, | ||
}, | ||
"end": { | ||
"line": 4, | ||
"character": 1, | ||
}, | ||
}, | ||
"newText": sprintf("%s ", [label]), | ||
}, | ||
} | ||
|
||
item in items | ||
} |
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
39 changes: 39 additions & 0 deletions
39
bundle/regal/lsp/completion/providers/package/package.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,39 @@ | ||
package regal.lsp.completion.providers["package"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.kind | ||
import data.regal.lsp.completion.location | ||
|
||
items contains item if { | ||
not strings.any_prefix_match(input.regal.file.lines, "package ") | ||
|
||
position := location.to_position(input.regal.context.location) | ||
line := input.regal.file.lines[position.line] | ||
|
||
invoke_suggestion(line) | ||
|
||
item := { | ||
"label": "package", | ||
"kind": kind.keyword, | ||
"detail": "package <package-name>", | ||
"textEdit": { | ||
"range": { | ||
"start": { | ||
"line": position.line, | ||
"character": 0, | ||
}, | ||
"end": { | ||
"line": position.line, | ||
"character": position.character, | ||
}, | ||
}, | ||
"newText": "package ", | ||
}, | ||
} | ||
} | ||
|
||
invoke_suggestion("") | ||
|
||
# regal ignore:external-reference | ||
invoke_suggestion(line) if startswith("package", line) |
60 changes: 60 additions & 0 deletions
60
bundle/regal/lsp/completion/providers/package/package_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,60 @@ | ||
package regal.lsp.completion.providers.package_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.providers["package"] as provider | ||
import data.regal.lsp.completion.providers.utils_test as util | ||
|
||
test_package_completion_on_typing if { | ||
policy := `p` | ||
items := provider.items with input as util.input_with_location(policy, {"row": 1, "col": 2}) | ||
items == {{ | ||
"detail": "package <package-name>", | ||
"kind": 14, | ||
"label": "package", | ||
"textEdit": { | ||
"newText": "package ", | ||
"range": { | ||
"end": { | ||
"character": 1, | ||
"line": 0, | ||
}, | ||
"start": { | ||
"character": 0, | ||
"line": 0, | ||
}, | ||
}, | ||
}, | ||
}} | ||
} | ||
|
||
test_package_completion_on_invoked if { | ||
policy := `` | ||
items := provider.items with input as util.input_with_location(policy, {"row": 1, "col": 1}) | ||
items == {{ | ||
"detail": "package <package-name>", | ||
"kind": 14, | ||
"label": "package", | ||
"textEdit": { | ||
"newText": "package ", | ||
"range": { | ||
"end": { | ||
"character": 0, | ||
"line": 0, | ||
}, | ||
"start": { | ||
"character": 0, | ||
"line": 0, | ||
}, | ||
}, | ||
}, | ||
}} | ||
} | ||
|
||
test_package_completion_not_suggested_if_already_present if { | ||
policy := `packae policy | ||
` | ||
items := provider.items with input as util.input_with_location(policy, {"row": 3, "col": 1}) | ||
items == set() | ||
} |
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,33 @@ | ||
package regal.lsp.completion.providers.regov1 | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.kind | ||
import data.regal.lsp.completion.location | ||
|
||
items contains item if { | ||
not strings.any_prefix_match(input.regal.file.lines, "import rego.v1") | ||
|
||
position := location.to_position(input.regal.context.location) | ||
line := input.regal.file.lines[position.line] | ||
|
||
startswith(line, "import ") | ||
|
||
word := location.ref_at(line, input.regal.context.location.col) | ||
|
||
invoke_suggestion(word) | ||
|
||
item := { | ||
"label": "rego.v1", | ||
"kind": kind.module, | ||
"detail": "use rego.v1", | ||
"textEdit": { | ||
"range": location.word_range(word, position), | ||
"newText": "rego.v1\n\n", | ||
}, | ||
} | ||
} | ||
|
||
invoke_suggestion(word) if { | ||
startswith("rego.v1", word.text) | ||
} |
Oops, something went wrong.