2019-06-11 13:09:43 -06:00
|
|
|
// 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.
|
|
|
|
|
2019-06-07 08:04:22 -06:00
|
|
|
package lsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/span"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
|
|
|
|
uri := span.NewURI(params.TextDocument.URI)
|
2019-11-15 10:43:45 -07:00
|
|
|
view, err := s.session.ViewOf(uri)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-20 14:38:43 -07:00
|
|
|
snapshot := view.Snapshot()
|
2019-12-17 16:57:54 -07:00
|
|
|
fh, err := snapshot.GetFile(ctx, uri)
|
2019-08-16 11:49:17 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-07 08:04:22 -06:00
|
|
|
// Find all references to the identifier at the position.
|
2019-12-17 16:57:54 -07:00
|
|
|
if fh.Identity().Kind != source.Go {
|
2019-12-10 09:51:34 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-17 16:57:54 -07:00
|
|
|
phs, err := snapshot.PackageHandles(ctx, fh)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
2019-12-10 09:51:34 -07:00
|
|
|
return nil, nil
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-07-02 15:41:09 -06:00
|
|
|
|
2019-06-07 08:04:22 -06:00
|
|
|
// Get the location of each reference to return as the result.
|
2019-12-09 09:36:55 -07:00
|
|
|
var (
|
|
|
|
locations []protocol.Location
|
|
|
|
seen = make(map[span.Span]bool)
|
|
|
|
lastIdent *source.IdentifierInfo
|
|
|
|
)
|
|
|
|
for _, ph := range phs {
|
2019-12-17 16:57:54 -07:00
|
|
|
ident, err := source.Identifier(ctx, snapshot, fh, params.Position, source.SpecificPackageHandle(ph.ID()))
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
2020-01-08 11:17:22 -07:00
|
|
|
return nil, err
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-12-09 09:36:55 -07:00
|
|
|
|
|
|
|
lastIdent = ident
|
|
|
|
|
|
|
|
references, err := ident.References(ctx)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
2020-01-08 11:17:22 -07:00
|
|
|
return nil, err
|
2019-12-09 09:36:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, ref := range references {
|
|
|
|
refSpan, err := ref.Span()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if seen[refSpan] {
|
|
|
|
continue // already added this location
|
|
|
|
}
|
|
|
|
seen[refSpan] = true
|
|
|
|
refRange, err := ref.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
locations = append(locations, protocol.Location{
|
|
|
|
URI: protocol.NewURI(ref.URI()),
|
|
|
|
Range: refRange,
|
|
|
|
})
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
}
|
2019-12-09 09:36:55 -07:00
|
|
|
|
2019-12-05 17:28:59 -07:00
|
|
|
// Only add the identifier's declaration if the client requests it.
|
2019-12-09 09:36:55 -07:00
|
|
|
if params.Context.IncludeDeclaration && lastIdent != nil {
|
|
|
|
rng, err := lastIdent.Declaration.Range()
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-05 17:28:59 -07:00
|
|
|
locations = append([]protocol.Location{
|
|
|
|
{
|
2019-12-09 09:36:55 -07:00
|
|
|
URI: protocol.NewURI(lastIdent.Declaration.URI()),
|
2019-12-05 17:28:59 -07:00
|
|
|
Range: rng,
|
|
|
|
},
|
|
|
|
}, locations...)
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-12-09 09:36:55 -07:00
|
|
|
|
2019-06-07 08:04:22 -06:00
|
|
|
return locations, nil
|
|
|
|
}
|