forked from StyraInc/regal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lsp/completions: add boolean provider (StyraInc#1059)
Signed-off-by: Charlie Egan <charlie@styra.com>
- Loading branch information
1 parent
be71a33
commit 83743b5
Showing
2 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
bundle/regal/lsp/completion/providers/booleans/booleans.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.booleans | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.kind | ||
import data.regal.lsp.completion.location | ||
|
||
parsed_current_file := data.workspace.parsed[input.regal.file.uri] | ||
|
||
items contains item if { | ||
position := location.to_position(input.regal.context.location) | ||
|
||
line := input.regal.file.lines[position.line] | ||
line != "" | ||
|
||
words := regex.split(`\s+`, line) | ||
|
||
words_on_line := count(words) | ||
|
||
previous_word := words[words_on_line - 2] | ||
|
||
previous_word in {"==", ":="} | ||
|
||
word := location.word_at(line, input.regal.context.location.col) | ||
|
||
some b in ["true", "false"] | ||
|
||
startswith(b, word.text) | ||
|
||
item := { | ||
"label": b, | ||
"kind": kind.constant, | ||
"detail": "boolean value", | ||
"textEdit": { | ||
"range": location.word_range(word, position), | ||
"newText": b, | ||
}, | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
bundle/regal/lsp/completion/providers/booleans/booleans_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,116 @@ | ||
package regal.lsp.completion.providers.booleans_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.providers.booleans as provider | ||
import data.regal.lsp.completion.providers.test_utils as utils | ||
|
||
test_suggested_in_head if { | ||
workspace := {"file:///p.rego": `package policy | ||
import rego.v1 | ||
allow := f`} | ||
|
||
regal_module := {"regal": { | ||
"file": { | ||
"name": "p.rego", | ||
"lines": split(workspace["file:///p.rego"], "\n"), | ||
}, | ||
"context": {"location": { | ||
"row": 5, | ||
"col": 10, | ||
}}, | ||
}} | ||
|
||
items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace) | ||
|
||
count(items) == 1 | ||
|
||
some item in items | ||
|
||
item.label == "false" | ||
} | ||
|
||
test_suggested_in_body if { | ||
workspace := {"file:///p.rego": `package policy | ||
import rego.v1 | ||
allow if { | ||
foo := t | ||
}`} | ||
|
||
regal_module := {"regal": { | ||
"file": { | ||
"name": "p.rego", | ||
"lines": split(workspace["file:///p.rego"], "\n"), | ||
}, | ||
"context": {"location": { | ||
"row": 6, | ||
"col": 10, | ||
}}, | ||
}} | ||
|
||
items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace) | ||
|
||
count(items) == 1 | ||
|
||
some item in items | ||
|
||
item.label == "true" | ||
} | ||
|
||
test_suggested_after_equals if { | ||
workspace := {"file:///p.rego": `package policy | ||
import rego.v1 | ||
allow if { | ||
foo == t | ||
}`} | ||
|
||
regal_module := {"regal": { | ||
"file": { | ||
"name": "p.rego", | ||
"lines": split(workspace["file:///p.rego"], "\n"), | ||
}, | ||
"context": {"location": { | ||
"row": 6, | ||
"col": 10, | ||
}}, | ||
}} | ||
|
||
items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace) | ||
|
||
count(items) == 1 | ||
|
||
some item in items | ||
|
||
item.label == "true" | ||
} | ||
|
||
test_not_suggested_at_start if { | ||
workspace := {"file:///p.rego": `package policy | ||
import rego.v1 | ||
allow if { | ||
t | ||
}`} | ||
|
||
regal_module := {"regal": { | ||
"file": { | ||
"name": "p.rego", | ||
"lines": split(workspace["file:///p.rego"], "\n"), | ||
}, | ||
"context": {"location": { | ||
"row": 6, | ||
"col": 3, | ||
}}, | ||
}} | ||
|
||
items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace) | ||
|
||
count(items) == 0 | ||
} |