Skip to content

Commit a2b9126

Browse files
author
Charlie Egan
committed
lsp/completions: Handle hyphenated package names
Fixes #1391 Signed-off-by: Charlie Egan <charlie@styra.com>
1 parent 37f126e commit a2b9126

2 files changed

Lines changed: 88 additions & 1 deletion

File tree

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,39 @@ _base(path) := substring(path, 0, regal.last(indexof_n(path, "/")))
4242
_suggestions(dir, text) := [path |
4343
parts := split(dir, ".")
4444
len_p := count(parts)
45+
4546
some n in numbers.range(0, len_p)
4647

47-
path := concat(".", array.slice(parts, n, len_p))
48+
formatted_parts := [p |
49+
some index, part in array.slice(parts, n, len_p)
50+
p := _format_part(part, contains(part, "-"))
51+
]
52+
53+
path := concat("", [p |
54+
some index, part in formatted_parts
55+
p := _delimit_part(part, array.slice(formatted_parts, index + 1, index + 2))
56+
])
57+
4858
path != ""
4959

60+
# it's not valid Rego to have a hypenated first part
61+
not startswith(path, `[""`)
62+
5063
startswith(path, text)
5164
]
65+
66+
_format_part(part, false) := part
67+
_format_part(part, true) := sprintf(`["%s"]`, [part])
68+
69+
_delimit_part(part, next_part) := delimited_part if {
70+
next_part != []
71+
not startswith(next_part[0], "[")
72+
delimited_part := sprintf("%s.", [part])
73+
}
74+
75+
_delimit_part(part, next_part) := part if {
76+
next_part != []
77+
startswith(next_part[0], "[")
78+
}
79+
80+
_delimit_part(part, next_part) := part if next_part == []

bundle/regal/lsp/completion/providers/packagename/packagename_test.rego

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,64 @@ test_package_name_completion_on_typing_multiple_suggestions_when_invoked if {
135135
}
136136
}
137137

138+
test_package_name_quoted if {
139+
policy := `package f`
140+
provider_input := {"regal": {
141+
"file": {
142+
"name": "/Users/joe/foo/bar/baz-are/foo/baz-are/foo/p.rego",
143+
"lines": split(policy, "\n"),
144+
},
145+
"context": {
146+
"workspace_root": "/Users/joe/policy",
147+
"location": {
148+
"row": 1,
149+
"col": 10,
150+
},
151+
},
152+
"environment": {"path_separator": "/"},
153+
}}
154+
155+
items := provider.items with input as provider_input
156+
items == {
157+
{
158+
"detail": "suggested package name based on directory structure",
159+
"kind": 19,
160+
"label": "foo.bar[\"baz-are\"].foo[\"baz-are\"].foo",
161+
"textEdit": {
162+
"newText": "foo.bar[\"baz-are\"].foo[\"baz-are\"].foo\n\n",
163+
"range": {
164+
"end": {"character": 9, "line": 0},
165+
"start": {"character": 8, "line": 0},
166+
},
167+
},
168+
},
169+
{
170+
"detail": "suggested package name based on directory structure",
171+
"kind": 19,
172+
"label": "foo",
173+
"textEdit": {
174+
"newText": "foo\n\n",
175+
"range": {
176+
"end": {"character": 9, "line": 0},
177+
"start": {"character": 8, "line": 0},
178+
},
179+
},
180+
},
181+
{
182+
"detail": "suggested package name based on directory structure",
183+
"kind": 19,
184+
"label": "foo[\"baz-are\"].foo",
185+
"textEdit": {
186+
"newText": "foo[\"baz-are\"].foo\n\n",
187+
"range": {
188+
"end": {"character": 9, "line": 0},
189+
"start": {"character": 8, "line": 0},
190+
},
191+
},
192+
},
193+
}
194+
}
195+
138196
test_build_suggestions if {
139197
provider._suggestions("foo.bar.baz", "foo") == ["foo.bar.baz"]
140198
provider._suggestions("foo.bar.baz", "bar") == ["bar.baz"]

0 commit comments

Comments
 (0)