Skip to content

Commit

Permalink
completion: Don't show builtins on default rules (#1056)
Browse files Browse the repository at this point in the history
Related to #1010

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 authored Sep 5, 2024
1 parent b1afdf6 commit 9c6a0c9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 22 deletions.
51 changes: 29 additions & 22 deletions internal/lsp/completions/providers/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (*BuiltIns) Run(c *cache.Cache, params types.CompletionParams, _ *Options)
return []types.CompletionItem{}, nil
}

// default rules cannot contain calls
if strings.HasPrefix(strings.TrimSpace(currentLine), "default ") {
return []types.CompletionItem{}, nil
}

words := patternWhiteSpace.Split(strings.TrimSpace(currentLine), -1)
lastWord := words[len(words)-1]

Expand All @@ -47,30 +52,32 @@ func (*BuiltIns) Run(c *cache.Cache, params types.CompletionParams, _ *Options)
continue
}

if strings.HasPrefix(key, lastWord) {
items = append(items, types.CompletionItem{
Label: key,
Kind: completion.Function,
Detail: "built-in function",
Documentation: &types.MarkupContent{
Kind: "markdown",
Value: hover.CreateHoverContent(builtIn),
},
TextEdit: &types.TextEdit{
Range: types.Range{
Start: types.Position{
Line: params.Position.Line,
Character: params.Position.Character - uint(len(lastWord)),
},
End: types.Position{
Line: params.Position.Line,
Character: params.Position.Character,
},
if !strings.HasPrefix(key, lastWord) {
continue
}

items = append(items, types.CompletionItem{
Label: key,
Kind: completion.Function,
Detail: "built-in function",
Documentation: &types.MarkupContent{
Kind: "markdown",
Value: hover.CreateHoverContent(builtIn),
},
TextEdit: &types.TextEdit{
Range: types.Range{
Start: types.Position{
Line: params.Position.Line,
Character: params.Position.Character - uint(len(lastWord)),
},
End: types.Position{
Line: params.Position.Line,
Character: params.Position.Character,
},
NewText: key,
},
})
}
NewText: key,
},
})
}

return items, nil
Expand Down
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 @@ -184,6 +184,39 @@ allow if c`
}
}

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

c := cache.NewCache()

fileContents := `package foo
default allow := f`

c.SetFileContents(testCaseFileURI, fileContents)

p := &BuiltIns{}

completionParams := types.CompletionParams{
TextDocument: types.TextDocumentIdentifier{
URI: testCaseFileURI,
},
Position: types.Position{
Line: 2,
Character: 18, // is the c char that triggered the request
},
}

completions, err := p.Run(c, completionParams, nil)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if len(completions) != 0 {
t.Fatalf("Expected no completions, got: %d", len(completions))
}
}

func completionLabels(completions []types.CompletionItem) []string {
labels := make([]string, len(completions))
for i, c := range completions {
Expand Down

0 comments on commit 9c6a0c9

Please sign in to comment.