Skip to content

Commit

Permalink
Show snippet completions for complete word (#914)
Browse files Browse the repository at this point in the history
Fixes #913

This makes it more consistent with other providers.

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Jul 15, 2024
1 parent bea3d66 commit 988d558
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
7 changes: 6 additions & 1 deletion bundle/regal/lsp/completion/providers/snippet/snippet.rego
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ items contains item if {
some label, snippet in _snippets

strings.any_prefix_match(snippet.prefix, word.text)
not contains(line, snippet.prefix[0])

# regal ignore:todo-comment
# TODO: use https://github.com/open-policy-agent/opa/pull/6841 when
# released.
count(regex.find_n(snippet.prefix[0], line, 2)) < 2

not endswith(trim_space(line), "=")

item := {
Expand Down
56 changes: 55 additions & 1 deletion bundle/regal/lsp/completion/providers/snippet/snippet_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import data.regal.lsp.completion.providers.snippet as provider
import data.regal.lsp.completion.providers.utils_test as util

# regal ignore:rule-length
test_snippet_completion_on_typing if {
test_snippet_completion_on_typing_partial_prefix if {
policy := `package policy
import rego.v1
Expand Down Expand Up @@ -45,6 +45,60 @@ allow if {
}
}

# regal ignore:rule-length
test_snippet_completion_on_typing_full_prefix if {
policy := `package policy
import rego.v1
allow if {
every
}`
items := provider.items with input as util.input_with_location(policy, {"row": 6, "col": 6})
items == {
{
"detail": "every key-value iteration",
"insertTextFormat": 2,
"kind": 15,
"label": "every key-value iteration (snippet)",
"textEdit": {
"newText": "every ${1:key}, ${2:value} in ${3:collection} {\n\t$0\n}",
"range": {
"end": {"character": 6, "line": 5},
"start": {"character": 1, "line": 5},
},
},
},
{
"detail": "every value iteration",
"insertTextFormat": 2,
"kind": 15,
"label": "every value iteration (snippet)",
"textEdit": {
"newText": "every ${1:var} in ${2:collection} {\n\t$0\n}",
"range": {
"end": {"character": 6, "line": 5},
"start": {"character": 1, "line": 5},
},
},
},
}
}

# regal ignore:rule-length
test_snippet_completion_on_typing_no_repeat if {
policy := `package policy
import rego.v1
allow if {
some e in [1,2,3] some
}
`
items := provider.items with input as util.input_with_location(policy, {"row": 6, "col": 21})
items == set()
}

# regal ignore:rule-length
test_snippet_completion_on_invoked if {
policy := `package policy
Expand Down

0 comments on commit 988d558

Please sign in to comment.