-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completions: package name suggested from any path component (#869)
And fix bug that would sometimes suggest "" as the package name. Oh, and the provider is now written in Rego :) Fixes #846 Fixes #856 Signed-off-by: Anders Eknert <[email protected]>
- Loading branch information
1 parent
3b5b83e
commit 2fa61d7
Showing
11 changed files
with
231 additions
and
233 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
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
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
51 changes: 51 additions & 0 deletions
51
bundle/regal/lsp/completion/providers/packagename/packagename.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,51 @@ | ||
package regal.lsp.completion.providers.packagename | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.kind | ||
import data.regal.lsp.completion.location | ||
|
||
items contains item if { | ||
position := location.to_position(input.regal.context.location) | ||
line := input.regal.file.lines[position.line] | ||
|
||
invoke_suggestion(line, position) | ||
|
||
ps := input.regal.context.path_separator | ||
|
||
abs_dir := base(input.regal.file.name) | ||
rel_dir := trim_prefix(abs_dir, input.regal.context.workspace_root) | ||
fix_dir := replace(replace(trim_prefix(rel_dir, ps), ".", "_"), ps, ".") | ||
|
||
word := location.ref_at(line, input.regal.context.location.col) | ||
|
||
some suggestion in suggestions(fix_dir, word) | ||
|
||
item := { | ||
"label": suggestion, | ||
"kind": kind.folder, | ||
"detail": "suggested package name based on directory structure", | ||
"textEdit": { | ||
"range": location.word_range(word, position), | ||
"newText": sprintf("%s\n\n", [suggestion]), | ||
}, | ||
} | ||
} | ||
|
||
invoke_suggestion(line, position) if { | ||
startswith(line, "package ") | ||
position.character > 7 | ||
} | ||
|
||
base(path) := substring(path, 0, regal.last(indexof_n(path, "/"))) | ||
|
||
suggestions(dir, word) := [path | | ||
parts := split(dir, ".") | ||
len_p := count(parts) | ||
some n in numbers.range(0, len_p) | ||
|
||
path := concat(".", array.slice(parts, n, len_p)) | ||
path != "" | ||
|
||
startswith(path, word.text) | ||
] |
154 changes: 154 additions & 0 deletions
154
bundle/regal/lsp/completion/providers/packagename/packagename_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,154 @@ | ||
package regal.lsp.completion.providers.packagename_test | ||
|
||
import rego.v1 | ||
|
||
import data.regal.lsp.completion.providers.packagename as provider | ||
|
||
test_package_name_completion_on_typing if { | ||
policy := `package f` | ||
provider_input := {"regal": { | ||
"file": { | ||
"name": "/Users/joe/policy/foo/bar/baz/p.rego", | ||
"lines": split(policy, "\n"), | ||
}, | ||
"context": { | ||
"path_separator": "/", | ||
"workspace_root": "/Users/joe/policy", | ||
"location": { | ||
"row": 1, | ||
"col": 10, | ||
}, | ||
}, | ||
}} | ||
items := provider.items with input as provider_input | ||
items == {{ | ||
"detail": "suggested package name based on directory structure", | ||
"kind": 19, | ||
"label": "foo.bar.baz", | ||
"textEdit": { | ||
"newText": "foo.bar.baz\n\n", | ||
"range": { | ||
"end": {"character": 9, "line": 0}, | ||
"start": {"character": 8, "line": 0}, | ||
}, | ||
}, | ||
}} | ||
} | ||
|
||
# regal ignore:rule-length | ||
test_package_name_completion_on_typing_multiple_suggestions if { | ||
policy := `package b` | ||
provider_input := {"regal": { | ||
"file": { | ||
"name": "/Users/joe/policy/foo/bar/baz/p.rego", | ||
"lines": split(policy, "\n"), | ||
}, | ||
"context": { | ||
"path_separator": "/", | ||
"workspace_root": "/Users/joe/policy", | ||
"location": { | ||
"row": 1, | ||
"col": 10, | ||
}, | ||
}, | ||
}} | ||
items := provider.items with input as provider_input | ||
items == { | ||
{ | ||
"detail": "suggested package name based on directory structure", | ||
"kind": 19, | ||
"label": "bar.baz", | ||
"textEdit": { | ||
"newText": "bar.baz\n\n", | ||
"range": { | ||
"end": {"character": 9, "line": 0}, | ||
"start": {"character": 8, "line": 0}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"detail": "suggested package name based on directory structure", | ||
"kind": 19, | ||
"label": "baz", | ||
"textEdit": { | ||
"newText": "baz\n\n", | ||
"range": { | ||
"end": {"character": 9, "line": 0}, | ||
"start": {"character": 8, "line": 0}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
# regal ignore:rule-length | ||
test_package_name_completion_on_typing_multiple_suggestions_when_invoked if { | ||
policy := `package ` | ||
provider_input := {"regal": { | ||
"file": { | ||
"name": "/Users/joe/policy/foo/bar/baz/p.rego", | ||
"lines": split(policy, "\n"), | ||
}, | ||
"context": { | ||
"path_separator": "/", | ||
"workspace_root": "/Users/joe/policy", | ||
"location": { | ||
"row": 1, | ||
"col": 9, | ||
}, | ||
}, | ||
}} | ||
items := provider.items with input as provider_input | ||
items == { | ||
{ | ||
"detail": "suggested package name based on directory structure", | ||
"kind": 19, | ||
"label": "foo.bar.baz", | ||
"textEdit": { | ||
"newText": "foo.bar.baz\n\n", | ||
"range": { | ||
"end": {"character": 8, "line": 0}, | ||
"start": {"character": 8, "line": 0}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"detail": "suggested package name based on directory structure", | ||
"kind": 19, | ||
"label": "bar.baz", | ||
"textEdit": { | ||
"newText": "bar.baz\n\n", | ||
"range": { | ||
"end": {"character": 8, "line": 0}, | ||
"start": {"character": 8, "line": 0}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"detail": "suggested package name based on directory structure", | ||
"kind": 19, | ||
"label": "baz", | ||
"textEdit": { | ||
"newText": "baz\n\n", | ||
"range": { | ||
"end": {"character": 8, "line": 0}, | ||
"start": {"character": 8, "line": 0}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
test_build_suggestions if { | ||
provider.suggestions("foo.bar.baz", {"text": "foo"}) == ["foo.bar.baz"] | ||
provider.suggestions("foo.bar.baz", {"text": "bar"}) == ["bar.baz"] | ||
provider.suggestions("foo.bar.baz", {"text": "ba"}) == ["bar.baz", "baz"] | ||
} | ||
|
||
test_build_suggestions_invoked if { | ||
provider.suggestions("foo.bar.baz", {"text": ""}) == [ | ||
"foo.bar.baz", | ||
"bar.baz", | ||
"baz", | ||
] | ||
} |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.