| 1 |
|
| 2 |
|
| 3 |
|
| 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/goasm" |
| 13 |
"golang.org/x/tools/gopls/internal/golang" |
| 14 |
"golang.org/x/tools/gopls/internal/label" |
| 15 |
"golang.org/x/tools/gopls/internal/protocol" |
| 16 |
"golang.org/x/tools/gopls/internal/telemetry" |
| 17 |
"golang.org/x/tools/gopls/internal/template" |
| 18 |
"golang.org/x/tools/internal/event" |
| 19 |
) |
| 20 |
|
| 21 |
func (s *server) Definition(ctx context.Context, params *protocol.DefinitionParams) (_ []protocol.Location, rerr error) { |
| 22 |
recordLatency := telemetry.StartLatencyTimer("definition") |
| 23 |
defer func() { |
| 24 |
recordLatency(ctx, rerr) |
| 25 |
}() |
| 26 |
|
| 27 |
ctx, done := event.Start(ctx, "server.Definition", label.URI.Of(params.TextDocument.URI)) |
| 28 |
defer done() |
| 29 |
|
| 30 |
|
| 31 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 32 |
if err != nil { |
| 33 |
return nil, err |
| 34 |
} |
| 35 |
defer release() |
| 36 |
switch kind := snapshot.FileKind(fh); kind { |
| 37 |
case file.Tmpl: |
| 38 |
return template.Definition(snapshot, fh, params.Range) |
| 39 |
case file.Go: |
| 40 |
return golang.Definition(ctx, snapshot, fh, params.Range) |
| 41 |
case file.Asm: |
| 42 |
return goasm.Definition(ctx, snapshot, fh, params.Range) |
| 43 |
default: |
| 44 |
return nil, fmt.Errorf("can't find definitions for file type %s", kind) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func (s *server) TypeDefinition(ctx context.Context, params *protocol.TypeDefinitionParams) ([]protocol.Location, error) { |
| 49 |
ctx, done := event.Start(ctx, "server.TypeDefinition", label.URI.Of(params.TextDocument.URI)) |
| 50 |
defer done() |
| 51 |
|
| 52 |
|
| 53 |
fh, snapshot, release, err := s.session.FileOf(ctx, params.TextDocument.URI) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
|
| 58 |
defer release() |
| 59 |
switch kind := snapshot.FileKind(fh); kind { |
| 60 |
case file.Go: |
| 61 |
return golang.TypeDefinition(ctx, snapshot, fh, params.Range) |
| 62 |
default: |
| 63 |
return nil, fmt.Errorf("can't find type definitions for file type %s", kind) |
| 64 |
} |
| 65 |
} |