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

lsp/completions: add boolean provider #1059

Merged
merged 1 commit into from
Sep 5, 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
39 changes: 39 additions & 0 deletions bundle/regal/lsp/completion/providers/booleans/booleans.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package regal.lsp.completion.providers.booleans

import rego.v1

import data.regal.lsp.completion.kind
import data.regal.lsp.completion.location

parsed_current_file := data.workspace.parsed[input.regal.file.uri]

items contains item if {
position := location.to_position(input.regal.context.location)

line := input.regal.file.lines[position.line]
line != ""

words := regex.split(`\s+`, line)

words_on_line := count(words)

previous_word := words[words_on_line - 2]

previous_word in {"==", ":="}

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

some b in ["true", "false"]

startswith(b, word.text)
charlieegan3 marked this conversation as resolved.
Show resolved Hide resolved

item := {
"label": b,
"kind": kind.constant,
"detail": "boolean value",
"textEdit": {
"range": location.word_range(word, position),
"newText": b,
},
}
}
116 changes: 116 additions & 0 deletions bundle/regal/lsp/completion/providers/booleans/booleans_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package regal.lsp.completion.providers.booleans_test

import rego.v1

import data.regal.lsp.completion.providers.booleans as provider
import data.regal.lsp.completion.providers.test_utils as utils

test_suggested_in_head if {
workspace := {"file:///p.rego": `package policy

import rego.v1

allow := f`}

regal_module := {"regal": {
"file": {
"name": "p.rego",
"lines": split(workspace["file:///p.rego"], "\n"),
},
"context": {"location": {
"row": 5,
"col": 10,
}},
}}

items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace)

count(items) == 1

some item in items

item.label == "false"
}

test_suggested_in_body if {
workspace := {"file:///p.rego": `package policy

import rego.v1

allow if {
foo := t
}`}

regal_module := {"regal": {
"file": {
"name": "p.rego",
"lines": split(workspace["file:///p.rego"], "\n"),
},
"context": {"location": {
"row": 6,
"col": 10,
}},
}}

items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace)

count(items) == 1

some item in items

item.label == "true"
}

test_suggested_after_equals if {
workspace := {"file:///p.rego": `package policy

import rego.v1

allow if {
foo == t
}`}

regal_module := {"regal": {
"file": {
"name": "p.rego",
"lines": split(workspace["file:///p.rego"], "\n"),
},
"context": {"location": {
"row": 6,
"col": 10,
}},
}}

items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace)

count(items) == 1

some item in items

item.label == "true"
}

test_not_suggested_at_start if {
workspace := {"file:///p.rego": `package policy

import rego.v1

allow if {
t
}`}

regal_module := {"regal": {
"file": {
"name": "p.rego",
"lines": split(workspace["file:///p.rego"], "\n"),
},
"context": {"location": {
"row": 6,
"col": 3,
}},
}}

items := provider.items with input as regal_module with data.workspace.parsed as utils.parsed_modules(workspace)

count(items) == 0
}