Skip to content

Commit

Permalink
Clear the store when reloading config (#855)
Browse files Browse the repository at this point in the history
This also ensures that config is loaded before doing the initial load of
the workspace. This ensure that loaded ignored files do not need to then
be immediately dropped.

Fixes #832
Fixes #843

Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Jun 19, 2024
1 parent d3dad61 commit 39e57db
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 117 deletions.
7 changes: 7 additions & 0 deletions bundle/regal/lsp/completion/providers/rulerefs/rulerefs.rego
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ items := [item |
line != ""
location.in_rule_body(line)

# \W is used here to match ( in the case of func() := ..., as well as the space in the case of rule := ...
first_word := regex.split(`\W+`, trim_space(line))[0]

last_word := regal.last(regex.split(`\s+`, trim_space(line)))

prefix := defermine_ref_prefix(last_word)
Expand All @@ -122,6 +125,9 @@ items := [item |

startswith(ref, prefix)

# this is to avoid suggesting a recursive rule, e.g. rule := rule, or func() := func()
ref != first_word

item := {
"label": ref,
"kind": kind.variable,
Expand All @@ -136,5 +142,6 @@ items := [item |
},
"newText": ref,
},
"_regal": {"provider": "rulerefs"},
}
]
68 changes: 62 additions & 6 deletions bundle/regal/lsp/completion/providers/rulerefs/rulerefs_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import rego.v1
workspace := {
"current_file.rego": `package foo
import rego.v1
import data.imported_pkg
import data.imported_pkg_2
local_rule := true
local_rule if true
`,
"imported_file.rego": `package imported_pkg
Expand Down Expand Up @@ -47,7 +49,7 @@ another_local_rule := `])
"lines": split(current_file_contents, "\n"),
},
"context": {"location": {
"row": 8,
"row": 10,
"col": 21,
}},
}}
Expand Down Expand Up @@ -78,7 +80,7 @@ another_local_rule := imp`])
"lines": split(current_file_contents, "\n"),
},
"context": {"location": {
"row": 8,
"row": 10,
"col": 21,
}},
}}
Expand All @@ -100,15 +102,69 @@ test_rule_refs_not_in_rule if {
a`])

lines := split(current_file_contents, "\n")

regal_module := {"regal": {
"file": {
"name": "current_file.rego",
"uri": "current_file.rego", # would be file:// prefixed in server
"lines": split(current_file_contents, "\n"),
"lines": lines,
},
"context": {"location": {
"row": 8,
"col": 21,
"row": count(lines),
"col": 1,
}},
}}

items := rulerefs.items with input as regal_module with data.workspace.parsed as parsed_modules

count(items) == 0
}

test_rule_refs_no_recursion if {
current_file_contents := concat("", [workspace["current_file.rego"], `
local_rule if local`])

lines := split(current_file_contents, "\n")

regal_module := {"regal": {
"file": {
"name": "current_file.rego",
"uri": "current_file.rego", # would be file:// prefixed in server
"lines": lines,
},
"context": {"location": {
"row": count(lines),
"col": 19,
}},
}}

items := rulerefs.items with input as regal_module with data.workspace.parsed as parsed_modules

count(items) == 0
}

test_rule_refs_no_recursion_func if {
current_file_contents := concat("", [workspace["current_file.rego"], `
local_fun("") := foo {
true
}
local_func("foo") := local_f`])

lines := split(current_file_contents, "\n")

regal_module := {"regal": {
"file": {
"name": "current_file.rego",
"uri": "current_file.rego", # would be file:// prefixed in server
"lines": lines,
},
"context": {"location": {
"row": count(lines),
"col": 26,
}},
}}

Expand Down
Loading

0 comments on commit 39e57db

Please sign in to comment.