Skip to content
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
10 changes: 8 additions & 2 deletions bundle/regal/ast/keywords_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ test_keywords_some_no_body if {
_keyword_on_row(
kwds,
6,
{"name": "some", "location": {"row": 6, "col": 2, "end": {"col": 6, "row": 6}, "text": "some"}},
{
"name": "some",
"location": {"row": 6, "col": 2, "end": {"col": 6, "row": 6}, "text": "some"},
},
)

_keyword_on_row(
kwds,
6,
{"name": "in", "location": {"row": 6, "col": 9, "end": {"col": 11, "row": 6}, "text": "in"}},
{
"name": "in",
"location": {"row": 6, "col": 9, "end": {"col": 11, "row": 6}, "text": "in"},
},
)
}

Expand Down
22 changes: 20 additions & 2 deletions bundle/regal/lsp/codeaction/codeaction_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,16 @@ test_code_actions_empty_only_means_all if {
_diagnostics["opa-fmt"] := {
"code": "opa-fmt",
"message": "Use opa fmt to format this file",
"range": {"start": {"line": 0, "character": 0}, "end": {"line": 0, "character": 1}},
"range": {
"start": {
"line": 0,
"character": 0,
},
"end": {
"line": 0,
"character": 1,
},
},
}

# Silly object.union only to appease the type checker, who for some reason thinks that
Expand All @@ -236,7 +245,16 @@ _diagnostics["use-assignment-operator"] := object.union(
{
"code": "use-assignment-operator",
"message": "Use := instead of = for assignment",
"range": {"start": {"line": 2, "character": 0}, "end": {"line": 2, "character": 1}},
"range": {
"start": {
"line": 2,
"character": 0,
},
"end": {
"line": 2,
"character": 1,
},
},
"codeDescription": {"href": "https://www.openpolicyagent.org/projects/regal/rules/style/use-assignment-operator"},
},
{},
Expand Down
10 changes: 8 additions & 2 deletions bundle/regal/lsp/completion/providers/default/default_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ import rego.v1
"textEdit": {
"newText": "default ",
"range": {
"start": {"character": 0, "line": 4},
"end": {"character": 1, "line": 4},
"start": {
"character": 0,
"line": 4,
},
"end": {
"character": 1,
"line": 4,
},
},
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ test_package_name_completion_on_typing if {
"textEdit": {
"newText": "foo.bar.baz\n\n",
"range": {
"end": {"character": 9, "line": 0},
"start": {"character": 8, "line": 0},
"end": {
"character": 9,
"line": 0,
},
"start": {
"character": 8,
"line": 0,
},
},
},
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ test_keyword_completion_after_rule_name_no_prefix[label] if {
expected := object.union(completion, {"textEdit": {
"newText": $"{label} ",
"range": {
"start": {"line": 2, "character": 5},
"end": {"line": 2, "character": 5},
"start": {
"line": 2,
"character": 5,
},
"end": {
"line": 2,
"character": 5,
},
},
}})

Expand Down
30 changes: 24 additions & 6 deletions bundle/regal/lsp/documenthighlight/documenthighlight.rego
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ items contains item if {

item := {
"range": {
"start": {"line": input.params.position.line, "character": 2},
"end": {"line": input.params.position.line, "character": 10},
"start": {
"line": input.params.position.line,
"character": 2,
},
"end": {
"line": input.params.position.line,
"character": 10,
},
},
"kind": 1,
}
Expand All @@ -96,8 +102,14 @@ items contains item if {

item := {
"range": {
"start": {"line": i, "character": 2},
"end": {"line": i, "character": 2 + count(word)},
"start": {
"line": i,
"character": 2,
},
"end": {
"line": i,
"character": 2 + count(word),
},
},
"kind": 1,
}
Expand All @@ -110,8 +122,14 @@ items contains item if {
word := _attribute_from_text(input.regal.file.lines[line])
item := {
"range": {
"start": {"line": line, "character": 2},
"end": {"line": line, "character": 2 + count(word)},
"start": {
"line": line,
"character": 2,
},
"end": {
"line": line,
"character": 2 + count(word),
},
},
"kind": 1,
}
Expand Down
25 changes: 15 additions & 10 deletions bundle/regal/lsp/documentlink/documentlink.rego
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@ items contains item if {
module := data.workspace.parsed[input.params.textDocument.uri]

some encoded in module.comments
comment := object.union(encoded, {"text": base64.decode(encoded.text)})
contains(comment.text, "regal ignore:")

loc := util.to_location_no_text(comment.location)
rules := regex.split(`,\s*`, trim_space(regex.replace(comment.text, `^.*regal ignore:\s*(\S+)`, "$1")))
text := base64.decode(encoded.text)
contains(text, "regal ignore:")

some rule in rules

pos := indexof(comment.text, rule)
loc := util.to_location_no_text(encoded.location)
row := loc.row - 1
col := loc.col + pos

some rule in regex.split(`,\s*`, trim_space(regex.replace(text, `^.*regal ignore:\s*(\S+)`, "$1")))

col := loc.col + indexof(text, rule)

item := {
"target": $"https://www.openpolicyagent.org/projects/regal/rules/{_category_for[rule]}/{rule}",
"range": {
"start": {"line": row, "character": col},
"end": {"line": row, "character": col + count(rule)},
"start": {
"line": row,
"character": col,
},
"end": {
"line": row,
"character": col + count(rule),
},
},
"tooltip": $"See documentation for {rule}",
}
Expand Down
40 changes: 32 additions & 8 deletions bundle/regal/lsp/linkededitingrange/linkededitingrange_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,25 @@ foo(bar, baz) := baz if {
expected_ranges := {
# function arg 'bar' in function head
{
"start": {"line": 2, "character": 4},
"end": {"line": 2, "character": 7},
"start": {
"line": 2,
"character": 4,
},
"end": {
"line": 2,
"character": 7,
},
},
# function arg 'bar' reference in function body
{
"start": {"line": 3, "character": 4},
"end": {"line": 3, "character": 7},
"start": {
"line": 3,
"character": 4,
},
"end": {
"line": 3,
"character": 7,
},
},
}

Expand Down Expand Up @@ -61,13 +73,25 @@ foo(bar, baz) := baz if {
expected_ranges := {
# function arg 'bar' in function head
{
"start": {"line": 2, "character": 4},
"end": {"line": 2, "character": 7},
"start": {
"line": 2,
"character": 4,
},
"end": {
"line": 2,
"character": 7,
},
},
# function arg 'bar' reference in function body
{
"start": {"line": 3, "character": 4},
"end": {"line": 3, "character": 7},
"start": {
"line": 3,
"character": 4,
},
"end": {
"line": 3,
"character": 7,
},
},
}

Expand Down
5 changes: 4 additions & 1 deletion bundle/regal/lsp/testlocations/testlocations_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ test_foo if true
"location": {
"col": 1,
"row": 3,
"end": {"col": 9, "row": 3},
"end": {
"col": 9,
"row": 3,
},
"file": "file:///foo/foo.rego",
"text": "test_foo if true",
},
Expand Down
30 changes: 24 additions & 6 deletions bundle/regal/lsp/util/range/range_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,44 @@ test_parse if range.parse("1:5:2:10") == {

test_range_contains_position_on_same_line if {
rng := {
"start": {"line": 3, "character": 6},
"end": {"line": 3, "character": 10},
"start": {
"line": 3,
"character": 6,
},
"end": {
"line": 3,
"character": 10,
},
}

range.contains_position(rng, {"line": 3, "character": 7})
}

test_range_contains_position_multiple_lines if {
rng := {
"start": {"line": 3, "character": 6},
"end": {"line": 5, "character": 20},
"start": {
"line": 3,
"character": 6,
},
"end": {
"line": 5,
"character": 20,
},
}

range.contains_position(rng, {"line": 4, "character": 0})
}

test_range_contains_position_last_line if {
rng := {
"start": {"line": 3, "character": 6},
"end": {"line": 5, "character": 20},
"start": {
"line": 3,
"character": 6,
},
"end": {
"line": 5,
"character": 20,
},
}

range.contains_position(rng, {"line": 5, "character": 15})
Expand Down
15 changes: 2 additions & 13 deletions bundle/regal/result/result.rego
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ _related_resources(annotations, category, title) := arr if {

arr := [{
"description": "documentation",
"ref": $"{config.docs.base_url}/{category}/{title}",
"ref": $"https://www.openpolicyagent.org/projects/regal/rules/{category}/{title}",
}]
}

_fail_annotated(metadata, details) := violation if {
_fail_annotated(metadata, details) := without_custom_and_scope if {
is_object(metadata)

with_location := object.union(metadata, details)
Expand All @@ -130,12 +130,6 @@ _fail_annotated(metadata, details) := violation if {
})

without_custom_and_scope := object.remove(with_category, ["custom", "scope", "schemas"])
related_resources := _resource_urls(without_custom_and_scope.related_resources, category)

violation := json.patch(
without_custom_and_scope,
[{"op": "replace", "path": "/related_resources", "value": related_resources}],
)
}

_fail_annotated_custom(metadata, details) := violation if {
Expand All @@ -151,11 +145,6 @@ _fail_annotated_custom(metadata, details) := violation if {
violation := object.remove(with_category, ["custom", "scope", "schemas"])
}

_resource_urls(related_resources, category) := [obj |
some item in related_resources
obj := object.union(item, {"ref": config.docs.resolve_url(item.ref, category)})
]

# Note that the `text` attribute always returns the entire line and *not*
# based on the location range. This is intentional, as the context is often
# needed when this is printed out in the console. LSP diagnostics however use
Expand Down
3 changes: 1 addition & 2 deletions bundle/regal/result/result_test.rego
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package regal.result_test

import data.regal.config
import data.regal.result

test_no_related_resources_in_result_fail_on_custom_rule_unless_provided if {
Expand Down Expand Up @@ -75,7 +74,7 @@ test_related_resources_generated_by_result_fail_for_builtin_rule if {
"level": "error",
"related_resources": [{
"description": "documentation",
"ref": config.docs.resolve_url("$baseUrl/$category/name", "category"),
"ref": "https://www.openpolicyagent.org/projects/regal/rules/category/name",
}],
"title": "name",
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# METADATA
# description: Annotation without metadata
# related_resources:
# - description: documentation
# ref: https://www.openpolicyagent.org/projects/regal/rules/bugs/annotation-without-metadata
package regal.rules.bugs["annotation-without-metadata"]

import data.regal.ast
Expand Down
Loading
Loading