1
0
mirror of https://github.com/golang/go synced 2024-09-30 20:18:33 -06:00
go/internal/lsp/implementation.go
Muir Manders a27fdba277 internal/lsp: check all package variants in find-implementations
We previously only searched for implementations of the object we found
in the "widest" package variant. We instead need to search all
variants because each variant is type checked separately, and
implementations can be located in packages associated with different
variants.

For example, say you have:

-- foo/foo.go --
package foo
type Foo int
type Fooer interface { Foo() Foo }

-- foo/foo_test.go --
package foo
func TestFoo(t *testing.T) {}

-- bar/bar.go --
package bar
import "foo"
type impl struct {}
func (impl) Foo() foo.Foo { return 0 }

When you run find-implementations on the Fooer interface, we
previously would start from the (widest) foo.test's Fooer named
type. Unfortunately bar imports foo, not foo.test, so bar.impl
does not implement foo.test.Fooer. The specific reason is that
bar.impl.Foo returns foo.Foo, whereas foo.test.Fooer.Foo returns
foo.test.Foo, which are distinct *types.Named objects.

Starting our search instead from foo.Fooer resolves this issue.
However, we also need to search from foo.test.Fooer so we match any
implementations in foo_test.go.

Change-Id: I0b0039c98925410751c8f643c8ebd185340e409f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/210459
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-12-11 21:44:05 +00:00

70 lines
1.6 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lsp
import (
"context"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/lsp/telemetry"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/telemetry/log"
)
func (s *Server) implementation(ctx context.Context, params *protocol.ImplementationParams) ([]protocol.Location, error) {
uri := span.NewURI(params.TextDocument.URI)
view, err := s.session.ViewOf(uri)
if err != nil {
return nil, err
}
snapshot := view.Snapshot()
f, err := view.GetFile(ctx, uri)
if err != nil {
return nil, err
}
phs, err := snapshot.PackageHandles(ctx, snapshot.Handle(ctx, f))
if err != nil {
return nil, err
}
var (
allLocs []protocol.Location
seen = make(map[protocol.Location]bool)
)
for _, ph := range phs {
ctx := telemetry.Package.With(ctx, ph.ID())
ident, err := source.Identifier(ctx, snapshot, f, params.Position, source.SpecificPackageHandle(ph.ID()))
if err != nil {
if err == source.ErrNoIdentFound {
return nil, err
}
log.Error(ctx, "failed to find Identifer", err)
continue
}
locs, err := ident.Implementation(ctx)
if err != nil {
if err == source.ErrNotAMethod {
return nil, err
}
log.Error(ctx, "failed to find Implemenation", err)
continue
}
for _, loc := range locs {
if seen[loc] {
continue
}
seen[loc] = true
allLocs = append(allLocs, loc)
}
}
return allLocs, nil
}