Skip to content

Commit

Permalink
Don't call completion providers inside of comments (#831)
Browse files Browse the repository at this point in the history
It seems like this is handled client side by some editors, but Zed
triggered completion requests inside of comments. While we could have
each provider handle this, I think an early exit in the manager already
is a better solution, as there's unlikely to be any provider that wants
to do suggestions in a comment. If that changes in the future, we'll
handle it then.

Fixes #826

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Jun 13, 2024
1 parent 90b2bcc commit 53dbce6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
23 changes: 23 additions & 0 deletions internal/lsp/completions/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

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

Expand Down Expand Up @@ -47,6 +48,12 @@ func NewDefaultManager(c *cache.Cache) *Manager {
func (m *Manager) Run(params types.CompletionParams, opts *providers.Options) ([]types.CompletionItem, error) {
var completions []types.CompletionItem

if m.isInsideOfComment(params) {
// Exit early if caret position is inside a comment. Most clients won't show
// suggestions there anyway, and there's no need to ask providers for completions.
return completions, nil
}

for _, provider := range m.providers {
providerCompletions, err := provider.Run(m.c, params, opts)
if err != nil {
Expand All @@ -70,3 +77,19 @@ func (m *Manager) Run(params types.CompletionParams, opts *providers.Options) ([
func (m *Manager) RegisterProvider(provider Provider) {
m.providers = append(m.providers, provider)
}

func (m *Manager) isInsideOfComment(params types.CompletionParams) bool {
if module, ok := m.c.GetModule(params.TextDocument.URI); ok {
for _, comment := range module.Comments {
cp := rego.PositionFromLocation(comment.Location)

if cp.Line == params.Position.Line {
if cp.Character <= params.Position.Character {
return true
}
}
}
}

return false
}
41 changes: 41 additions & 0 deletions internal/lsp/completions/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package completions
import (
"testing"

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

"github.com/styrainc/regal/internal/lsp/cache"
"github.com/styrainc/regal/internal/lsp/completions/providers"
"github.com/styrainc/regal/internal/lsp/types"
Expand Down Expand Up @@ -44,3 +46,42 @@ func TestManager(t *testing.T) {
t.Fatalf("Expected label to be 'package', got: %v", comp.Label)
}
}

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

c := cache.NewCache()
fileURI := "file:///foo/bar/file.rego"

fileContents := `package p
import rego.v1 # modern rego i
`

module := ast.MustParseModule(fileContents)

c.SetFileContents(fileURI, fileContents)
c.SetModule(fileURI, module)

mgr := NewManager(c, &ManagerOptions{})
mgr.RegisterProvider(&providers.Import{})

completionParams := types.CompletionParams{
TextDocument: types.TextDocumentIdentifier{
URI: fileURI,
},
Position: types.Position{
Line: 2,
Character: 30,
},
}

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

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

0 comments on commit 53dbce6

Please sign in to comment.