From 5a610156430fb2d8e702a6c846f9dab9afebaffa Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Wed, 26 Jun 2024 14:36:32 +0200 Subject: [PATCH] Add snippet suggestion for metadata annotation (#871) Also in this PR is a minor fixup, where many `invoke_suggestion` functions have been replaced by `startswith` alone, as `startswith("foo", "")` is true, and doesn't need special handling. Signed-off-by: Anders Eknert --- .../providers/commonrule/commonrule.rego | 7 +----- .../completion/providers/default/default.rego | 8 ++----- .../completion/providers/import/import.rego | 6 +---- .../completion/providers/package/package.rego | 7 +----- .../providers/packagename/packagename.rego | 8 ++----- .../completion/providers/regov1/regov1.rego | 6 +---- .../completion/providers/snippet/snippet.rego | 20 ++++++++++++++++ .../providers/snippet/snippet_test.rego | 23 +++++++++++++++++++ 8 files changed, 51 insertions(+), 34 deletions(-) diff --git a/bundle/regal/lsp/completion/providers/commonrule/commonrule.rego b/bundle/regal/lsp/completion/providers/commonrule/commonrule.rego index d473ec46..88d776c2 100644 --- a/bundle/regal/lsp/completion/providers/commonrule/commonrule.rego +++ b/bundle/regal/lsp/completion/providers/commonrule/commonrule.rego @@ -17,7 +17,7 @@ items contains item if { some label in suggested_names - invoke_suggestion(line, label) + startswith(label, line) item := { "label": label, @@ -33,8 +33,3 @@ items contains item if { }, } } - -invoke_suggestion("", _) - -# regal ignore:external-reference -invoke_suggestion(line, label) if startswith(label, line) diff --git a/bundle/regal/lsp/completion/providers/default/default.rego b/bundle/regal/lsp/completion/providers/default/default.rego index d65cd1f4..897dd00a 100644 --- a/bundle/regal/lsp/completion/providers/default/default.rego +++ b/bundle/regal/lsp/completion/providers/default/default.rego @@ -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", @@ -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 @@ -42,7 +42,3 @@ items contains item if { }, } } - -invoke_suggestion("") - -invoke_suggestion(line) if startswith("default", line) diff --git a/bundle/regal/lsp/completion/providers/import/import.rego b/bundle/regal/lsp/completion/providers/import/import.rego index 15a39a56..79374eab 100644 --- a/bundle/regal/lsp/completion/providers/import/import.rego +++ b/bundle/regal/lsp/completion/providers/import/import.rego @@ -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", @@ -22,7 +22,3 @@ items contains item if { }, } } - -invoke_suggestion("") - -invoke_suggestion(line) if startswith("import", line) diff --git a/bundle/regal/lsp/completion/providers/package/package.rego b/bundle/regal/lsp/completion/providers/package/package.rego index a9700e52..d72c124e 100644 --- a/bundle/regal/lsp/completion/providers/package/package.rego +++ b/bundle/regal/lsp/completion/providers/package/package.rego @@ -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", @@ -23,8 +23,3 @@ items contains item if { }, } } - -invoke_suggestion("") - -# regal ignore:external-reference -invoke_suggestion(line) if startswith("package", line) diff --git a/bundle/regal/lsp/completion/providers/packagename/packagename.rego b/bundle/regal/lsp/completion/providers/packagename/packagename.rego index 693dba60..76b3bb2f 100644 --- a/bundle/regal/lsp/completion/providers/packagename/packagename.rego +++ b/bundle/regal/lsp/completion/providers/packagename/packagename.rego @@ -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 @@ -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 | diff --git a/bundle/regal/lsp/completion/providers/regov1/regov1.rego b/bundle/regal/lsp/completion/providers/regov1/regov1.rego index b9c2acc0..95d9d11e 100644 --- a/bundle/regal/lsp/completion/providers/regov1/regov1.rego +++ b/bundle/regal/lsp/completion/providers/regov1/regov1.rego @@ -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", @@ -27,7 +27,3 @@ items contains item if { }, } } - -invoke_suggestion(word) if { - startswith("rego.v1", word.text) -} diff --git a/bundle/regal/lsp/completion/providers/snippet/snippet.rego b/bundle/regal/lsp/completion/providers/snippet/snippet.rego index d0cd4ab0..e54435c0 100644 --- a/bundle/regal/lsp/completion/providers/snippet/snippet.rego +++ b/bundle/regal/lsp/completion/providers/snippet/snippet.rego @@ -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", diff --git a/bundle/regal/lsp/completion/providers/snippet/snippet_test.rego b/bundle/regal/lsp/completion/providers/snippet/snippet_test.rego index 7dce72d8..9c2fbbc5 100644 --- a/bundle/regal/lsp/completion/providers/snippet/snippet_test.rego +++ b/bundle/regal/lsp/completion/providers/snippet/snippet_test.rego @@ -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}, + }, + }, + }} +}