Skip to content

Commit

Permalink
Merge pull request #30 from kitagry/fix-completion-duplicates
Browse files Browse the repository at this point in the history
Fix completion duplicates
  • Loading branch information
kitagry committed Jan 18, 2022
2 parents bcdf576 + 2e2bf1d commit 6010add
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
23 changes: 16 additions & 7 deletions langserver/internal/source/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,15 @@ func (p *Project) listCompletionItemsForTerms(location *ast.Location, target *as
result = append(result, list...)
}

for _, r := range module.Rules {
result = append(result, createRuleCompletionItem(r))
}
result = append(result, p.listCompletionItemsModuleRules(module.Rules)...)
}

if p.isLibraryTerm(target) {
if _, ok := target.Value.(ast.Ref); ok {
importRef := p.findPolicyRef(target)
policies := p.cache.FindPolicies(importRef)
for _, p := range policies {
for _, r := range p.Rules {
result = append(result, createRuleCompletionItem(r))
}
for _, po := range policies {
result = append(result, p.listCompletionItemsModuleRules(po.Rules)...)
}
}
}
Expand Down Expand Up @@ -238,6 +234,19 @@ func (p *Project) listCompletionItemsInTerm(loc *ast.Location, term *ast.Term) [
return result
}

func (p *Project) listCompletionItemsModuleRules(rules []*ast.Rule) []CompletionItem {
result := make([]CompletionItem, 0, len(rules))
exists := make(map[string]struct{}, 0)
for _, r := range rules {
item := createRuleCompletionItem(r)
if _, ok := exists[item.Label]; !ok {
result = append(result, item)
exists[item.Label] = struct{}{}
}
}
return result
}

func (p *Project) listBuiltinFunction(term *ast.Term) []CompletionItem {
if term == nil {
return nil
Expand Down
29 changes: 29 additions & 0 deletions langserver/internal/source/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ authorize = "allow" {
},
},
},
"completion other args": {
files: map[string]source.File{
"src.rego": {
RowText: `package src
func() {
me
}
mem_multiple("E") = 1000000000000000000000
mem_multiple("P") = 1000000000000000000`,
},
},
location: &ast.Location{
Row: 4,
Col: 3,
Offset: len("package src\n\nfunc() {\n me"),
Text: []byte("e"),
File: "src.rego",
},
expectItems: []source.CompletionItem{
{
Label: "mem_multiple",
Kind: source.FunctionItem,
InsertText: `mem_multiple("E")`,
},
},
},
}

for n, tt := range tests {
Expand Down

0 comments on commit 6010add

Please sign in to comment.