blob: 6f4e3ee2060dfe698d68b9ff4e86a770a231b50e [file] [log] [blame]
Robert Findleyb15dac22022-08-30 14:40:12 -04001// Copyright 2019 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package lsp
6
7import (
8 "context"
9
10 "golang.org/x/tools/gopls/internal/lsp/protocol"
11 "golang.org/x/tools/gopls/internal/lsp/source"
12 "golang.org/x/tools/gopls/internal/lsp/template"
13)
14
15func (s *Server) references(ctx context.Context, params *protocol.ReferenceParams) ([]protocol.Location, error) {
16 snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind)
17 defer release()
18 if !ok {
19 return nil, err
20 }
21 if snapshot.View().FileKind(fh) == source.Tmpl {
22 return template.References(ctx, snapshot, fh, params)
23 }
24 references, err := source.References(ctx, snapshot, fh, params.Position, params.Context.IncludeDeclaration)
25 if err != nil {
26 return nil, err
27 }
28 var locations []protocol.Location
29 for _, ref := range references {
30 refRange, err := ref.Range()
31 if err != nil {
32 return nil, err
33 }
34 locations = append(locations, protocol.Location{
35 URI: protocol.URIFromSpanURI(ref.URI()),
36 Range: refRange,
37 })
38 }
39 return locations, nil
40}