.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / internal / lsp / definition.go
1 // 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
5 package lsp
6
7 import (
8         "context"
9
10         "golang.org/x/tools/internal/lsp/protocol"
11         "golang.org/x/tools/internal/lsp/source"
12 )
13
14 func (s *Server) definition(ctx context.Context, params *protocol.DefinitionParams) ([]protocol.Location, error) {
15         snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
16         defer release()
17         if !ok {
18                 return nil, err
19         }
20         ident, err := source.Identifier(ctx, snapshot, fh, params.Position)
21         if err != nil {
22                 return nil, err
23         }
24         if !snapshot.View().Options().ImportShortcut.ShowDefinition() {
25                 return nil, nil
26         }
27         var locations []protocol.Location
28         for _, ref := range ident.Declaration.MappedRange {
29                 decRange, err := ref.Range()
30                 if err != nil {
31                         return nil, err
32                 }
33
34                 locations = append(locations, protocol.Location{
35                         URI:   protocol.URIFromSpanURI(ref.URI()),
36                         Range: decRange,
37                 })
38         }
39
40         return locations, nil
41 }
42
43 func (s *Server) typeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) {
44         snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.Go)
45         defer release()
46         if !ok {
47                 return nil, err
48         }
49         ident, err := source.Identifier(ctx, snapshot, fh, params.Position)
50         if err != nil {
51                 return nil, err
52         }
53         identRange, err := ident.Type.Range()
54         if err != nil {
55                 return nil, err
56         }
57         return []protocol.Location{
58                 {
59                         URI:   protocol.URIFromSpanURI(ident.Type.URI()),
60                         Range: identRange,
61                 },
62         }, nil
63 }