Skip to content

Commit

Permalink
Move utils to refs
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 committed May 23, 2024
1 parent 7d1d3a0 commit cbc1372
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 107 deletions.
2 changes: 0 additions & 2 deletions internal/ast/ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func TestRefToString(t *testing.T) {
}

for _, tc := range cases {
tc := tc

t.Run(tc.title, func(t *testing.T) {
t.Parallel()

Expand Down
4 changes: 0 additions & 4 deletions internal/ast/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ func TestGetRuleDetail(t *testing.T) {
}

for _, tc := range cases {
tc := tc

t.Run(tc.input, func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -83,8 +81,6 @@ func TestSimplifyType(t *testing.T) {
}

for _, tc := range cases {
tc := tc

t.Run(tc.input, func(t *testing.T) {
t.Parallel()

Expand Down
11 changes: 5 additions & 6 deletions internal/lsp/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/open-policy-agent/opa/ast"

"github.com/styrainc/regal/internal/lsp/completions/utils"
"github.com/styrainc/regal/internal/lsp/types"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ type Cache struct {
builtinPositionsMu sync.Mutex

// fileRefs is a map of file URI to completion items that might be suggested from that file
fileRefs map[string]map[string]utils.Ref
fileRefs map[string]map[string]types.Ref
fileRefMu sync.Mutex
}

Expand All @@ -54,7 +53,7 @@ func NewCache() *Cache {

builtinPositionsFile: make(map[string]map[uint][]types.BuiltinPosition),

fileRefs: make(map[string]map[string]utils.Ref),
fileRefs: make(map[string]map[string]types.Ref),
}
}

Expand Down Expand Up @@ -210,21 +209,21 @@ func (c *Cache) GetAllBuiltInPositions() map[string]map[uint][]types.BuiltinPosi
return c.builtinPositionsFile
}

func (c *Cache) SetFileRefs(uri string, items map[string]utils.Ref) {
func (c *Cache) SetFileRefs(uri string, items map[string]types.Ref) {
c.fileRefMu.Lock()
defer c.fileRefMu.Unlock()

c.fileRefs[uri] = items
}

func (c *Cache) GetFileRefs(uri string) map[string]utils.Ref {
func (c *Cache) GetFileRefs(uri string) map[string]types.Ref {
c.fileRefMu.Lock()
defer c.fileRefMu.Unlock()

return c.fileRefs[uri]
}

func (c *Cache) GetAllFileRefs() map[string]map[string]utils.Ref {
func (c *Cache) GetAllFileRefs() map[string]map[string]types.Ref {
c.fileRefMu.Lock()
defer c.fileRefMu.Unlock()

Expand Down
5 changes: 2 additions & 3 deletions internal/lsp/completions/providers/packagerefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"

"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/completions/utils"
"github.com/styrainc/regal/internal/lsp/types"
"github.com/styrainc/regal/internal/lsp/types/symbols"
)
Expand Down Expand Up @@ -34,15 +33,15 @@ func (*PackageRefs) Run(c *cache.Cache, params types.CompletionParams, _ *Option
}

thisFileReferences := c.GetFileRefs(fileURI)
otherFilePackages := make(map[string]utils.Ref)
otherFilePackages := make(map[string]types.Ref)

for file, refs := range c.GetAllFileRefs() {
if file == fileURI {
continue
}

for key, ref := range refs {
if ref.Kind != utils.Package {
if ref.Kind != types.Package {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/completions/providers/packagerefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/completions/utils"
"github.com/styrainc/regal/internal/lsp/completions/refs"
"github.com/styrainc/regal/internal/lsp/types"
"github.com/styrainc/regal/internal/parse"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ import
mod := parse.MustParseModule(contents)
c.SetModule(uri, mod)

c.SetFileRefs(uri, utils.RefsForModule(mod))
c.SetFileRefs(uri, refs.ForModule(mod))
}

p := &PackageRefs{}
Expand Down
22 changes: 14 additions & 8 deletions internal/lsp/completions/providers/rulerefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import (
"strings"

"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/completions/utils"
"github.com/styrainc/regal/internal/lsp/types"
"github.com/styrainc/regal/internal/lsp/types/symbols"
)

type RuleFromImportedPackageRefs struct{}

func (*RuleFromImportedPackageRefs) Run(c *cache.Cache, params types.CompletionParams, _ *Options) ([]types.CompletionItem, error) {
func (*RuleFromImportedPackageRefs) Run(
c *cache.Cache,
params types.CompletionParams,
_ *Options,
) ([]types.CompletionItem, error) {
fileURI := params.TextDocument.URI

fileContents, ok := c.GetFileContents(fileURI)
Expand Down Expand Up @@ -42,7 +45,7 @@ func (*RuleFromImportedPackageRefs) Run(c *cache.Cache, params types.CompletionP
return nil, nil
}

refsFromImports := make(map[string]utils.Ref)
refsFromImports := make(map[string]types.Ref)

for file, refs := range c.GetAllFileRefs() {
if file == fileURI {
Expand All @@ -51,17 +54,20 @@ func (*RuleFromImportedPackageRefs) Run(c *cache.Cache, params types.CompletionP

for key, ref := range refs {
// we are not interested in packages here, only the rules
if ref.Kind == utils.Package {
if ref.Kind == types.Package {
continue
}

isFromImportedPackage := false

for _, i := range mod.Imports {
if strings.HasPrefix(key, i.Path.String()) {
isFromImportedPackage = true

break
}
}

if !isFromImportedPackage {
continue
}
Expand All @@ -71,15 +77,16 @@ func (*RuleFromImportedPackageRefs) Run(c *cache.Cache, params types.CompletionP
}

items := make([]types.CompletionItem, 0)
for _, ref := range refsFromImports {

for _, ref := range refsFromImports {
symbol := symbols.Variable
detail := "Rule"

switch {
case ref.Kind == utils.ConstantRule:
case ref.Kind == types.ConstantRule:
symbol = symbols.Constant
detail = "Constant Rule"
case ref.Kind == utils.Function:
case ref.Kind == types.Function:
symbol = symbols.Function
detail = "Function"
}
Expand Down Expand Up @@ -114,7 +121,6 @@ func (*RuleFromImportedPackageRefs) Run(c *cache.Cache, params types.CompletionP
}

func labelToPackageAndRule(label string) string {

parts := strings.Split(label, ".")
partCount := len(parts)

Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/completions/providers/rulerefs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/completions/utils"
"github.com/styrainc/regal/internal/lsp/completions/refs"
"github.com/styrainc/regal/internal/lsp/types"
"github.com/styrainc/regal/internal/parse"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ deny := false

c.SetModule(uri, mod)

c.SetFileRefs(uri, utils.RefsForModule(mod))
c.SetFileRefs(uri, refs.ForModule(mod))
}

c.SetFileContents("file:///example.rego", currentlyEditingFileContents+"\n\nallow if ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package refs

import (
"fmt"
Expand All @@ -10,42 +10,29 @@ import (
"github.com/open-policy-agent/opa/ast"

rast "github.com/styrainc/regal/internal/ast"
"github.com/styrainc/regal/internal/lsp/types"
)

type RefKind int

const (
Package RefKind = iota + 1
Rule
ConstantRule
Function
)

type Ref struct {
Kind RefKind
Label string
Detail string
Description string
}

// RefsForModule returns a map of refs and details about them to be used in completions.
// ForModule returns a map of refs and details about them to be used in completions.
// Currently, only the package is returned. In future, this will also return information
// about rules and functions.
func RefsForModule(module *ast.Module) map[string]Ref {
func ForModule(module *ast.Module) map[string]types.Ref {
modKey := module.Package.Path.String()

// first, create a reference for the package using the metadata
// if present
packagePrettyName := strings.TrimPrefix(rast.RefToString(module.Package.Path), "data.")
packageDescription := defaultDescription(packagePrettyName)

packageAnnotation, ok := findAnnotationForPackage(module)
if ok {
packageDescription = createDocModuleRef(packageAnnotation)
}
items := map[string]Ref{

items := map[string]types.Ref{
modKey: {
Label: modKey,
Kind: Package,
Kind: types.Package,
Detail: "Package",
Description: packageDescription,
},
Expand All @@ -68,9 +55,11 @@ func RefsForModule(module *ast.Module) map[string]Ref {
ruleKey := fmt.Sprintf("%s.%s", modKey, g)

isConstant := true

for _, r := range rs {
if !rast.IsConstant(r) {
isConstant = false

break
}
}
Expand All @@ -80,21 +69,23 @@ func RefsForModule(module *ast.Module) map[string]Ref {
isFunc = true
}

kind := Rule
kind := types.Rule

switch {
case isConstant:
kind = ConstantRule
kind = types.ConstantRule
case isFunc:
kind = Function
kind = types.Function
}

ruleDescription := defaultDescription(g)

ruleAnnotation, ok := findAnnotationForRuleGroup(module, rs)
if ok {
ruleDescription = createDocModuleRef(ruleAnnotation)
}

items[ruleKey] = Ref{
items[ruleKey] = types.Ref{
Kind: kind,
Label: ruleKey,
Detail: rast.GetRuleDetail(rs[0]),
Expand Down Expand Up @@ -145,7 +136,6 @@ func findAnnotationForRuleGroup(m *ast.Module, rs []*ast.Rule) (*ast.Annotations
}

func createDocModuleRef(selectedAnnotation *ast.Annotations) string {

var sb strings.Builder

if selectedAnnotation.Title != "" {
Expand Down
Loading

0 comments on commit cbc1372

Please sign in to comment.