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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
|
2020-02-12 14:36:46 -07:00
|
|
|
uri := params.TextDocument.URI.SpanURI()
|
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()
|
2020-01-10 15:37:29 -07:00
|
|
|
fh, err := snapshot.GetFile(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 22:06:31 -07:00
|
|
|
|
|
|
|
references, err := source.References(ctx, view.Snapshot(), fh, params.Position, params.Context.IncludeDeclaration)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
2019-12-17 22:06:31 -07:00
|
|
|
return nil, err
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-07-02 15:41:09 -06:00
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
var locations []protocol.Location
|
|
|
|
for _, ref := range references {
|
|
|
|
refRange, err := ref.Range()
|
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
|
|
|
}
|
|
|
|
|
2019-12-17 22:06:31 -07:00
|
|
|
locations = append(locations, protocol.Location{
|
2020-02-12 14:36:46 -07:00
|
|
|
URI: protocol.URIFromSpanURI(ref.URI()),
|
2019-12-17 22:06:31 -07:00
|
|
|
Range: refRange,
|
|
|
|
})
|
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
|
|
|
|
}
|