Skip to content

Commit

Permalink
internal/lsp/source: return location(s) for imported packages
Browse files Browse the repository at this point in the history
Fixes golang/go#32993

Change-Id: Iff2f7ab8e34eea1ce7c1ab99fb2e51589dce95c4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/210321
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
  • Loading branch information
danishprakash authored and stamblerre committed Mar 9, 2020
1 parent 11ec414 commit c94e1fe
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
23 changes: 14 additions & 9 deletions internal/lsp/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,21 @@ func (s *Server) definition(ctx context.Context, params *protocol.DefinitionPara
if err != nil {
return nil, err
}
decRange, err := ident.Declaration.Range()
if err != nil {
return nil, err
}
return []protocol.Location{
{
URI: protocol.URIFromSpanURI(ident.Declaration.URI()),

var locations []protocol.Location
for _, ref := range ident.Declaration.MappedRange {
decRange, err := ref.Range()
if err != nil {
return nil, err
}

locations = append(locations, protocol.Location{
URI: protocol.URIFromSpanURI(ref.URI()),
Range: decRange,
},
}, nil
})
}

return locations, nil
}

func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/source/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (i *IdentifierInfo) linkAndSymbolName() (string, string) {
// package's API). This is true if the request originated in a test package,
// and if the declaration is also found in the same test package.
if i.pkg != nil && obj.Pkg() != nil && i.pkg.ForTest() != "" {
if _, pkg, _ := FindFileInPackage(i.pkg, i.Declaration.URI()); i.pkg == pkg {
if _, pkg, _ := FindFileInPackage(i.pkg, i.Declaration.MappedRange[0].URI()); i.pkg == pkg {
return "", ""
}
}
Expand Down
39 changes: 24 additions & 15 deletions internal/lsp/source/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ type IdentifierInfo struct {
}

type Declaration struct {
mappedRange
node ast.Node
obj types.Object
MappedRange []mappedRange
node ast.Node
obj types.Object
}

// Identifier returns identifier information for a position
Expand Down Expand Up @@ -111,6 +111,7 @@ func findIdentifier(ctx context.Context, s Snapshot, pkg Package, file *ast.File
if result.mappedRange, err = posToMappedRange(view, pkg, result.ident.Pos(), result.ident.End()); err != nil {
return nil, err
}

result.Declaration.obj = pkg.GetTypesInfo().ObjectOf(result.ident)
if result.Declaration.obj == nil {
// If there was no types.Object for the declaration, there might be an implicit local variable
Expand Down Expand Up @@ -138,9 +139,13 @@ func findIdentifier(ctx context.Context, s Snapshot, pkg Package, file *ast.File
return nil, errors.Errorf("no declaration for %s", result.Name)
}
result.Declaration.node = decl
if result.Declaration.mappedRange, err = nameToMappedRange(view, pkg, decl.Pos(), result.Name); err != nil {

rng, err := nameToMappedRange(view, pkg, decl.Pos(), result.Name)
if err != nil {
return nil, err
}
result.Declaration.MappedRange = append(result.Declaration.MappedRange, rng)

return result, nil
}

Expand All @@ -154,9 +159,12 @@ func findIdentifier(ctx context.Context, s Snapshot, pkg Package, file *ast.File
}
}

if result.Declaration.mappedRange, err = objToMappedRange(view, pkg, result.Declaration.obj); err != nil {
rng, err := objToMappedRange(view, pkg, result.Declaration.obj)
if err != nil {
return nil, err
}
result.Declaration.MappedRange = append(result.Declaration.MappedRange, rng)

if result.Declaration.node, err = objToNode(s.View(), pkg, result.Declaration.obj); err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,19 +281,20 @@ func importSpec(s Snapshot, pkg Package, file *ast.File, pos token.Pos) (*Identi
if importedPkg.GetSyntax() == nil {
return nil, errors.Errorf("no syntax for for %q", importPath)
}
// Heuristic: Jump to the longest (most "interesting") file of the package.
var dest *ast.File
for _, f := range importedPkg.GetSyntax() {
if dest == nil || f.End()-f.Pos() > dest.End()-dest.Pos() {
dest = f
}
}
if dest == nil {
// Return all of the files in the package as the definition of the import spec.
dest := pkg.GetSyntax()
if len(dest) == 0 {
return nil, errors.Errorf("package %q has no files", importPath)
}
if result.Declaration.mappedRange, err = posToMappedRange(s.View(), pkg, dest.Pos(), dest.End()); err != nil {
return nil, err

for _, dst := range importedPkg.GetSyntax() {
rng, err := posToMappedRange(s.View(), pkg, dst.Pos(), dst.End())
if err != nil {
return nil, err
}
result.Declaration.MappedRange = append(result.Declaration.MappedRange, rng)
}

result.Declaration.node = imp
return result, nil
}
Expand Down
6 changes: 3 additions & 3 deletions internal/lsp/source/signature_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ FindCall:
return nil, 0, err
}
decl := &Declaration{
obj: obj,
mappedRange: rng,
node: node,
obj: obj,
node: node,
}
decl.MappedRange = append(decl.MappedRange, rng)
d, err := decl.hover(ctx)
if err != nil {
return nil, 0, err
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func (r *runner) Definition(t *testing.T, spn span.Span, d tests.Definition) {
if err != nil {
t.Fatal(err)
}
rng, err := ident.Declaration.Range()
rng, err := ident.Declaration.MappedRange[0].Range()
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c94e1fe

Please sign in to comment.