Skip to content

Commit

Permalink
lsp: find definition should honor ignored files (#732)
Browse files Browse the repository at this point in the history
Fixes #731

Signed-off-by: Anders Eknert <anders@styra.com>
  • Loading branch information
anderseknert authored May 22, 2024
1 parent 9d7530a commit d1525ce
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/sourcegraph/jsonrpc2 v0.2.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,6 @@ go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
Expand Down
4 changes: 3 additions & 1 deletion internal/lsp/opa/oracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"errors"

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

"github.com/styrainc/regal/internal/compile"
)

// Error defines the structure of errors returned by the oracle.
Expand Down Expand Up @@ -138,7 +140,7 @@ func walkToFirstOccurrence(node ast.Node, needle ast.Var) (match *ast.Term) {
}

func compileUpto(stage string, modules map[string]*ast.Module, bs []byte, filename string) (*ast.Compiler, *ast.Module, error) {
compiler := ast.NewCompiler()
compiler := compile.NewCompilerWithRegalBuiltins()

if stage != "" {
compiler = compiler.WithStageAfter(stage, ast.CompilerStageDefinition{
Expand Down
44 changes: 40 additions & 4 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//nolint:nilnil
//nolint:nilerr,nilnil
package lsp

import (
Expand Down Expand Up @@ -30,6 +30,7 @@ import (
"github.com/styrainc/regal/internal/lsp/types"
"github.com/styrainc/regal/internal/lsp/uri"
rparse "github.com/styrainc/regal/internal/parse"
"github.com/styrainc/regal/internal/util"
"github.com/styrainc/regal/pkg/config"
"github.com/styrainc/regal/pkg/fixer/fixes"
"github.com/styrainc/regal/pkg/linter"
Expand Down Expand Up @@ -773,18 +774,30 @@ func (l *LanguageServer) handleTextDocumentDefinition(
return nil, fmt.Errorf("failed to get file contents for uri %q", params.TextDocument.URI)
}

modules, err := l.getFilteredModules()
if err != nil {
return nil, fmt.Errorf("failed to filter ignored paths: %w", err)
}

orc := oracle.New()
query := oracle.DefinitionQuery{
Filename: uri.ToPath(l.clientIdentifier, params.TextDocument.URI),
Pos: positionToOffset(contents, params.Position),
Modules: l.cache.GetAllModules(),
Modules: modules,
Buffer: []byte(contents),
}

definition, err := orc.FindDefinition(query)
if err != nil {
// fail silently — the user could have clicked anywhere. return "null" as per the spec
return nil, nil //nolint:nilerr
if errors.Is(err, oracle.ErrNoDefinitionFound) || errors.Is(err, oracle.ErrNoMatchFound) {
// fail silently — the user could have clicked anywhere. return "null" as per the spec
return nil, nil
}

l.logError(fmt.Errorf("failed to find definition: %w", err))

// return "null" as per the spec
return nil, nil
}

loc := types.Location{
Expand Down Expand Up @@ -1304,6 +1317,29 @@ func (l *LanguageServer) sendFileDiagnostics(ctx context.Context, uri string) er
return nil
}

func (l *LanguageServer) getFilteredModules() (map[string]*ast.Module, error) {
ignore := make([]string, 0)

if l.loadedConfig != nil && l.loadedConfig.Ignore.Files != nil {
ignore = l.loadedConfig.Ignore.Files
}

allModules := l.cache.GetAllModules()
paths := util.Keys(allModules)

filtered, err := config.FilterIgnoredPaths(paths, ignore, false, l.clientRootURI)
if err != nil {
return nil, fmt.Errorf("failed to filter ignored paths: %w", err)
}

modules := make(map[string]*ast.Module, len(filtered))
for _, path := range filtered {
modules[path] = allModules[path]
}

return modules, nil
}

func positionToOffset(text string, p types.Position) int {
bytesRead := 0
lines := strings.Split(text, "\n")
Expand Down

0 comments on commit d1525ce

Please sign in to comment.