Skip to content

Commit

Permalink
lsp/completions: add boolean provider (StyraInc#1059)
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 authored and srenatus committed Oct 1, 2024
1 parent be71a33 commit 83743b5
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
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)

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
}

0 comments on commit 83743b5

Please sign in to comment.