Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snippet suggestion for metadata annotation #871

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ items contains item if {

some label in suggested_names

invoke_suggestion(line, label)
startswith(label, line)

item := {
"label": label,
Expand All @@ -33,8 +33,3 @@ items contains item if {
},
}
}

invoke_suggestion("", _)

# regal ignore:external-reference
invoke_suggestion(line, label) if startswith(label, line)
8 changes: 2 additions & 6 deletions bundle/regal/lsp/completion/providers/default/default.rego
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ items contains item if {
position := location.to_position(input.regal.context.location)
line := input.regal.file.lines[position.line]

invoke_suggestion(line)
startswith("default", line)

item := {
"label": "default",
Expand All @@ -28,7 +28,7 @@ items contains item if {
position := location.to_position(input.regal.context.location)
line := input.regal.file.lines[position.line]

invoke_suggestion(line)
startswith("default", line)

some name in ast.rule_and_function_names

Expand All @@ -42,7 +42,3 @@ items contains item if {
},
}
}

invoke_suggestion("")

invoke_suggestion(line) if startswith("default", line)
6 changes: 1 addition & 5 deletions bundle/regal/lsp/completion/providers/import/import.rego
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ items contains item if {
line := input.regal.file.lines[position.line]
word := location.word_at(line, input.regal.context.location.col)

invoke_suggestion(line)
startswith("import", line)

item := {
"label": "import",
Expand All @@ -22,7 +22,3 @@ items contains item if {
},
}
}

invoke_suggestion("")

invoke_suggestion(line) if startswith("import", line)
7 changes: 1 addition & 6 deletions bundle/regal/lsp/completion/providers/package/package.rego
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ items contains item if {
position := location.to_position(input.regal.context.location)
line := input.regal.file.lines[position.line]

invoke_suggestion(line)
startswith("package", line)

item := {
"label": "package",
Expand All @@ -23,8 +23,3 @@ items contains item if {
},
}
}

invoke_suggestion("")

# regal ignore:external-reference
invoke_suggestion(line) if startswith("package", line)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ items contains item if {
position := location.to_position(input.regal.context.location)
line := input.regal.file.lines[position.line]

invoke_suggestion(line, position)
startswith(line, "package ")
position.character > 7

ps := input.regal.context.path_separator

Expand All @@ -32,11 +33,6 @@ items contains item if {
}
}

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 |
Expand Down
6 changes: 1 addition & 5 deletions bundle/regal/lsp/completion/providers/regov1/regov1.rego
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ items contains item if {

word := location.ref_at(line, input.regal.context.location.col)

invoke_suggestion(word)
startswith("rego.v1", word.text)

item := {
"label": "rego.v1",
Expand All @@ -27,7 +27,3 @@ items contains item if {
},
}
}

invoke_suggestion(word) if {
startswith("rego.v1", word.text)
}
20 changes: 20 additions & 0 deletions bundle/regal/lsp/completion/providers/snippet/snippet.rego
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ items contains item if {
}
}

items contains item if {
position := location.to_position(input.regal.context.location)
line := input.regal.file.lines[position.line]

startswith("metadata", line)

word := location.word_at(line, input.regal.context.location.col)

item := {
"label": "metadata annotation (snippet)",
"kind": kind.snippet,
"detail": "metadata annotation",
"textEdit": {
"range": location.word_range(word, position),
"newText": "# METADATA\n# title: ${1:title}\n# description: ${2:description}",
},
"insertTextFormat": 2, # snippet
}
}

_snippets := {
"some value iteration": {
"body": "some ${1:var} in ${2:collection}\n$0",
Expand Down
23 changes: 23 additions & 0 deletions bundle/regal/lsp/completion/providers/snippet/snippet_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,26 @@ allow if `
},
}
}

test_metadata_snippet_completion if {
policy := `package policy

import rego.v1


`
items := provider.items with input as util.input_with_location(policy, {"row": 5, "col": 1})
items == {{
"detail": "metadata annotation",
"insertTextFormat": 2,
"kind": 15,
"label": "metadata annotation (snippet)",
"textEdit": {
"newText": "# METADATA\n# title: ${1:title}\n# description: ${2:description}",
"range": {
"end": {"character": 0, "line": 4},
"start": {"character": 0, "line": 4},
},
},
}}
}