golang.org/x/tools/gopls@v0.15.3/internal/server/definition.go (about) 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 server 6 7 import ( 8 "context" 9 "fmt" 10 11 "golang.org/x/tools/gopls/internal/file" 12 "golang.org/x/tools/gopls/internal/golang" 13 "golang.org/x/tools/gopls/internal/protocol" 14 "golang.org/x/tools/gopls/internal/telemetry" 15 "golang.org/x/tools/gopls/internal/template" 16 "golang.org/x/tools/internal/event" 17 "golang.org/x/tools/internal/event/tag" 18 ) 19 20 func (s *server) Definition(ctx context.Context, params *protocol.DefinitionParams) (_ []protocol.Location, rerr error) { 21 recordLatency := telemetry.StartLatencyTimer("definition") 22 defer func() { 23 recordLatency(ctx, rerr) 24 }() 25 26 ctx, done := event.Start(ctx, "lsp.Server.definition", tag.URI.Of(params.TextDocument.URI)) 27 defer done() 28 29 // TODO(rfindley): definition requests should be multiplexed across all views. 30 fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) 31 if err != nil { 32 return nil, err 33 } 34 defer release() 35 switch kind := snapshot.FileKind(fh); kind { 36 case file.Tmpl: 37 return template.Definition(snapshot, fh, params.Position) 38 case file.Go: 39 return golang.Definition(ctx, snapshot, fh, params.Position) 40 default: 41 return nil, fmt.Errorf("can't find definitions for file type %s", kind) 42 } 43 } 44 45 func (s *server) TypeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) { 46 ctx, done := event.Start(ctx, "lsp.Server.typeDefinition", tag.URI.Of(params.TextDocument.URI)) 47 defer done() 48 49 // TODO(rfindley): type definition requests should be multiplexed across all views. 50 fh, snapshot, release, err := s.fileOf(ctx, params.TextDocument.URI) 51 if err != nil { 52 return nil, err 53 } 54 defer release() 55 switch kind := snapshot.FileKind(fh); kind { 56 case file.Go: 57 return golang.TypeDefinition(ctx, snapshot, fh, params.Position) 58 default: 59 return nil, fmt.Errorf("can't find type definitions for file type %s", kind) 60 } 61 }