Skip to content

Commit b8f3e8f

Browse files
authored
fix: improve snippets suggestion condition (#1597)
While most editors seemed to be capable of filtering out these suggestions by themselves, not all did. And we should obviously not send suggestions that aren't valid in any case. Fixes #1588 Signed-off-by: Anders Eknert <anders@styra.com>
1 parent 30c73fe commit b8f3e8f

5 files changed

Lines changed: 26 additions & 10 deletions

File tree

bundle/regal/lsp/completion/providers/snippet/snippet.rego

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ items contains item if {
1818
position := location.to_position(input.regal.context.location)
1919
line := input.regal.file.lines[position.line]
2020

21-
not endswith(trim_space(line), "=")
2221
location.in_rule_body(line)
2322

2423
word := location.word_at(line, input.regal.context.location.col)
24+
before := trim_suffix(line, word.text)
25+
26+
# match empty line, or line ending with `if` or `|` plus whitespace
27+
regex.match(`^\s+$|if\s+$|\|\s+$`, before)
2528

2629
some label, snippet in _snippets
2730

bundle/regal/lsp/completion/providers/snippet/snippet_test.rego

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ test_snippet_completion_on_typing_no_repeat if {
8282
items == set()
8383
}
8484

85+
test_no_snippet_completion_on_[typed] if {
86+
template := `allow if {
87+
%s
88+
}`
89+
90+
some typed in ["i", "in", "inp", "inpu", "input", "input.", "input.x"]
91+
92+
policy := _with_header(sprintf(template, [typed]))
93+
94+
items := provider.items with input as util.input_with_location(policy, {"row": 6, "col": 1 + count(typed)})
95+
items == set()
96+
}
97+
8598
test_snippet_completion_on_invoked if {
8699
items := provider.items with input as util.input_with_location(_with_header(`allow if `), {"row": 5, "col": 10})
87100
items == {

internal/cache/cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func BenchmarkPut(b *testing.B) {
1212
ref := ast.MustParseRef("data.foo.bar.baz")
1313
value := ast.String("qux")
1414

15-
for range b.N {
15+
for b.Loop() {
1616
cache.Put(ref, value)
1717
}
1818
}
@@ -26,7 +26,7 @@ func BenchmarkGet(b *testing.B) {
2626

2727
r := ast.MustParseRef("data.foo.bar.baz")
2828

29-
for range b.N {
29+
for b.Loop() {
3030
_ = cache.Get(r)
3131
}
3232
}

pkg/builtins/builtins_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func BenchmarkRegalLast(b *testing.B) {
1919

2020
var res *ast.Term
2121

22-
for range b.N {
22+
for b.Loop() {
2323
var err error
2424

2525
res, err = builtins.RegalLast(bctx, arr)
@@ -42,7 +42,7 @@ func BenchmarkRegalLastEmptyArr(b *testing.B) {
4242

4343
var err error
4444

45-
for range b.N {
45+
for b.Loop() {
4646
_, err = builtins.RegalLast(bctx, arr)
4747
}
4848

pkg/linter/linter_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ func BenchmarkRegalLintingItself(b *testing.B) {
743743

744744
var rep report.Report
745745

746-
for range b.N {
746+
for b.Loop() {
747747
rep, err = linter.Lint(b.Context())
748748
if err != nil {
749749
b.Fatal(err)
@@ -772,7 +772,7 @@ func BenchmarkRegalLintingItselfPrepareOnce(b *testing.B) {
772772

773773
var rep report.Report
774774

775-
for range b.N {
775+
for b.Loop() {
776776
rep, err = linter.Lint(ctx)
777777
if err != nil {
778778
b.Fatal(err)
@@ -796,7 +796,7 @@ func BenchmarkRegalNoEnabledRules(b *testing.B) {
796796

797797
var rep report.Report
798798

799-
for range b.N {
799+
for b.Loop() {
800800
rep, err = linter.Lint(b.Context())
801801
if err != nil {
802802
b.Fatal(err)
@@ -821,7 +821,7 @@ func BenchmarkRegalNoEnabledRulesPrepareOnce(b *testing.B) {
821821

822822
var rep report.Report
823823

824-
for range b.N {
824+
for b.Loop() {
825825
rep, err = linter.Lint(b.Context())
826826
if err != nil {
827827
b.Fatal(err)
@@ -866,7 +866,7 @@ func BenchmarkEachRule(b *testing.B) {
866866

867867
var rep report.Report
868868

869-
for range b.N {
869+
for b.Loop() {
870870
rep, err = linter.WithEnabledRules(ruleName).Lint(ctx)
871871
if err != nil {
872872
b.Fatal(err)

0 commit comments

Comments
 (0)