Debian Codegraph @

gopls/internal/cmd/implementation.go

raw symbols SCIP
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/protocol"
14 "golang.org/x/tools/gopls/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
42 cli, _, err := i.app.connect(ctx)
43 if err != nil {
44 return err
45 }
46 defer cli.terminate(ctx)
47
48 from := parseSpan(args[0])
49 file, err := cli.openFile(ctx, from.URI())
50 if err != nil {
51 return err
52 }
53
54 loc, err := file.spanLocation(from)
55 if err != nil {
56 return err
57 }
58
59 p := protocol.ImplementationParams{
60 TextDocumentPositionParams: protocol.LocationTextDocumentPositionParams(loc),
61 }
62 implementations, err := cli.server.Implementation(ctx, &p)
63 if err != nil {
64 return err
65 }
66
67 var spans []string
68 for _, impl := range implementations {
69 f, err := cli.openFile(ctx, impl.URI)
70 if err != nil {
71 return err
72 }
73 span, err := f.locationSpan(impl)
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 }