Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 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 cmd |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "flag" |
| 10 | "fmt" |
| 11 | "sort" |
| 12 | |
| 13 | "golang.org/x/tools/gopls/internal/lsp/protocol" |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 14 | "golang.org/x/tools/internal/tool" |
| 15 | ) |
| 16 | |
| 17 | // implementation implements the implementation verb for gopls |
| 18 | type implementation struct { |
| 19 | app *Application |
| 20 | } |
| 21 | |
| 22 | func (i *implementation) Name() string { return "implementation" } |
| 23 | func (i *implementation) Parent() string { return i.app.Name() } |
| 24 | func (i *implementation) Usage() string { return "<position>" } |
| 25 | func (i *implementation) ShortHelp() string { return "display selected identifier's implementation" } |
| 26 | func (i *implementation) DetailedHelp(f *flag.FlagSet) { |
| 27 | fmt.Fprint(f.Output(), ` |
| 28 | Example: |
| 29 | |
| 30 | $ # 1-indexed location (:line:column or :#offset) of the target identifier |
| 31 | $ gopls implementation helper/helper.go:8:6 |
| 32 | $ gopls implementation helper/helper.go:#53 |
| 33 | `) |
| 34 | printFlagDefaults(f) |
| 35 | } |
| 36 | |
| 37 | func (i *implementation) Run(ctx context.Context, args ...string) error { |
| 38 | if len(args) != 1 { |
| 39 | return tool.CommandLineErrorf("implementation expects 1 argument (position)") |
| 40 | } |
| 41 | |
Rob Findley | 1c5ccad | 2023-04-18 13:48:38 -0400 | [diff] [blame] | 42 | conn, err := i.app.connect(ctx, nil) |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | defer conn.terminate(ctx) |
| 47 | |
Alan Donovan | 3292b36 | 2023-11-14 01:08:40 -0500 | [diff] [blame] | 48 | from := parseSpan(args[0]) |
Alan Donovan | 54c806f | 2023-04-07 15:04:09 -0400 | [diff] [blame] | 49 | file, err := conn.openFile(ctx, from.URI()) |
| 50 | if err != nil { |
| 51 | return err |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Alan Donovan | 3292b36 | 2023-11-14 01:08:40 -0500 | [diff] [blame] | 54 | loc, err := file.spanLocation(from) |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | |
| 59 | p := protocol.ImplementationParams{ |
Alan Donovan | d093a13 | 2023-01-26 16:14:35 -0500 | [diff] [blame] | 60 | TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc), |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 61 | } |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 62 | implementations, err := conn.Implementation(ctx, &p) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | |
| 67 | var spans []string |
| 68 | for _, impl := range implementations { |
Alan Donovan | 979df5b | 2023-11-15 13:16:52 -0500 | [diff] [blame] | 69 | f, err := conn.openFile(ctx, impl.URI) |
Alan Donovan | 54c806f | 2023-04-07 15:04:09 -0400 | [diff] [blame] | 70 | if err != nil { |
| 71 | return err |
| 72 | } |
Alan Donovan | 3292b36 | 2023-11-14 01:08:40 -0500 | [diff] [blame] | 73 | span, err := f.locationSpan(impl) |
Robert Findley | b15dac2 | 2022-08-30 14:40:12 -0400 | [diff] [blame] | 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | spans = append(spans, fmt.Sprint(span)) |
| 78 | } |
| 79 | sort.Strings(spans) |
| 80 | |
| 81 | for _, s := range spans { |
| 82 | fmt.Println(s) |
| 83 | } |
| 84 | |
| 85 | return nil |
| 86 | } |