Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
charlieegan3 committed Sep 5, 2024
1 parent 9c6a0c9 commit 290e712
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
29 changes: 28 additions & 1 deletion internal/lsp/completions/providers/builtins.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package providers

import (
"fmt"
"strings"

"github.com/open-policy-agent/opa/ast"
"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/hover"
"github.com/styrainc/regal/internal/lsp/rego"
Expand Down Expand Up @@ -56,6 +58,8 @@ func (*BuiltIns) Run(c *cache.Cache, params types.CompletionParams, _ *Options)
continue
}

insertTextFormat := uint(2) // snippet

items = append(items, types.CompletionItem{
Label: key,
Kind: completion.Function,
Expand All @@ -64,6 +68,7 @@ func (*BuiltIns) Run(c *cache.Cache, params types.CompletionParams, _ *Options)
Kind: "markdown",
Value: hover.CreateHoverContent(builtIn),
},
InsertTextFormat: &insertTextFormat,
TextEdit: &types.TextEdit{
Range: types.Range{
Start: types.Position{
Expand All @@ -75,10 +80,32 @@ func (*BuiltIns) Run(c *cache.Cache, params types.CompletionParams, _ *Options)
Character: params.Position.Character,
},
},
NewText: key,
NewText: newTextForBuiltIn(builtIn),
},
})
}

return items, nil
}

func newTextForBuiltIn(bi *ast.Builtin) string {
args := make([]string, len(bi.Decl.Args()))
for i, arg := range bi.Decl.NamedFuncArgs().Args {
args[i] = strings.Split(arg.String(), ":")[0]
}

if len(args) == 0 {
return bi.Name + "($0)"
}

argString := ""
for i, arg := range args {

Check failure on line 102 in internal/lsp/completions/providers/builtins.go

View workflow job for this annotation

GitHub Actions / Matrix (ubuntu-latest, linux, true)

ranges should only be cuddled with assignments used in the iteration (wsl)
if i > 0 {
argString += ", "
}

argString += fmt.Sprintf("${%d:%s}", i+1, arg)
}

return fmt.Sprintf("%s(%s)", bi.Name, argString)
}
33 changes: 33 additions & 0 deletions internal/lsp/completions/providers/builtins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,42 @@ import (
"testing"

"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/rego"
"github.com/styrainc/regal/internal/lsp/types"
)

func TestNewTextForBuiltIn(t *testing.T) {

Check failure on line 13 in internal/lsp/completions/providers/builtins_test.go

View workflow job for this annotation

GitHub Actions / Matrix (ubuntu-latest, linux, true)

Function TestNewTextForBuiltIn missing the call to method parallel (paralleltest)
testCases := map[string]struct {
BuiltIn string
Output string
}{
"print": {
BuiltIn: "print",
Output: "print($0)",
},
"strings.count": {
BuiltIn: "strings.count",
Output: "strings.count(${1:search}, ${2:substring})",
},
}

bis := rego.GetBuiltins()

for name, tc := range testCases {

Check failure on line 30 in internal/lsp/completions/providers/builtins_test.go

View workflow job for this annotation

GitHub Actions / Matrix (ubuntu-latest, linux, true)

Range statement for test TestNewTextForBuiltIn missing the call to method parallel in test Run (paralleltest)
t.Run(name, func(t *testing.T) {
bi, ok := bis[tc.BuiltIn]
if !ok {
t.Fatalf("BuiltIn %s not found", tc.BuiltIn)
}

output := newTextForBuiltIn(bi)
if output != tc.Output {
t.Fatalf("Expected\n%s\ngot\n%s", tc.Output, output)
}
})
}
}

func TestBuiltIns_if(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 6 additions & 6 deletions internal/lsp/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ type CompletionItem struct {
Label string `json:"label"`
LabelDetails *CompletionItemLabelDetails `json:"labelDetails,omitempty"`
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#completionItemKind
Kind completion.ItemKind `json:"kind"`
Detail string `json:"detail"`
Documentation *MarkupContent `json:"documentation,omitempty"`
Preselect bool `json:"preselect"`
TextEdit *TextEdit `json:"textEdit,omitempty"`
InserTextFormat *uint `json:"insertTextFormat,omitempty"`
Kind completion.ItemKind `json:"kind"`
Detail string `json:"detail"`
Documentation *MarkupContent `json:"documentation,omitempty"`
Preselect bool `json:"preselect"`
TextEdit *TextEdit `json:"textEdit,omitempty"`
InsertTextFormat *uint `json:"insertTextFormat,omitempty"`

// Mandatory is used to indicate that the completion item is mandatory and should be offered
// as an exclusive completion. This is not part of the LSP spec, but used in regal providers
Expand Down

0 comments on commit 290e712

Please sign in to comment.