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"
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/telemetry/tag"
|
2019-06-07 08:04:22 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
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-09-27 11:17:59 -06:00
|
|
|
f, err := view.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-11-20 14:38:43 -07:00
|
|
|
ident, err := source.Identifier(ctx, snapshot, f, params.Position)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-06 21:58:07 -06:00
|
|
|
references, err := ident.References(ctx)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
2019-07-14 21:08:10 -06:00
|
|
|
log.Error(ctx, "no references", err, tag.Of("Identifier", ident.Name))
|
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.
|
|
|
|
locations := make([]protocol.Location, 0, len(references))
|
2019-06-26 16:05:29 -06:00
|
|
|
seen := make(map[span.Span]bool)
|
2019-06-07 08:04:22 -06:00
|
|
|
for _, ref := range references {
|
2019-08-26 22:26:45 -06:00
|
|
|
refSpan, err := ref.Span()
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-26 16:05:29 -06:00
|
|
|
if seen[refSpan] {
|
|
|
|
continue // already added this location
|
|
|
|
}
|
|
|
|
seen[refSpan] = true
|
2019-08-26 22:26:45 -06:00
|
|
|
refRange, err := ref.Range()
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
locations = append(locations, protocol.Location{
|
|
|
|
URI: protocol.NewURI(ref.URI()),
|
|
|
|
Range: refRange,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// The declaration of this identifier may not be in the
|
|
|
|
// scope that we search for references, so make sure
|
|
|
|
// it is added to the beginning of the list if IncludeDeclaration
|
|
|
|
// was specified.
|
|
|
|
if params.Context.IncludeDeclaration {
|
2019-09-04 15:23:20 -06:00
|
|
|
decSpan, err := ident.Declaration.Span()
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-04 15:23:20 -06:00
|
|
|
if !seen[decSpan] {
|
|
|
|
rng, err := ident.Declaration.Range()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
locations = append([]protocol.Location{
|
|
|
|
{
|
|
|
|
URI: protocol.NewURI(ident.Declaration.URI()),
|
|
|
|
Range: rng,
|
|
|
|
},
|
|
|
|
}, locations...)
|
|
|
|
}
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
|
|
|
return locations, nil
|
|
|
|
}
|