Debian Codegraph @

gopls/internal/cmd/execute.go

raw symbols SCIP
1 // Copyright 2023 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 "encoding/json"
10 "flag"
11 "fmt"
12 "log"
13 "slices"
14
15 "golang.org/x/tools/gopls/internal/protocol"
16 "golang.org/x/tools/gopls/internal/protocol/command"
17 "golang.org/x/tools/internal/tool"
18 )
19
20 // execute implements the LSP ExecuteCommand verb for gopls.
21 type execute struct {
22 EditFlags
23 app *Application
24 }
25
26 func (e *execute) Name() string { return "execute" }
27 func (e *execute) Parent() string { return e.app.Name() }
28 func (e *execute) Usage() string { return "[flags] command argument..." }
29 func (e *execute) ShortHelp() string { return "Execute a gopls custom LSP command" }
30 func (e *execute) DetailedHelp(f *flag.FlagSet) {
31 fmt.Fprint(f.Output(), `
32 The execute command sends an LSP ExecuteCommand request to gopls,
33 with a set of optional JSON argument values.
34 Some commands return a result, also JSON.
35
36 Gopls' command set is defined by the command.Interface type; see
37 https://pkg.go.dev/golang.org/x/tools/gopls/internal/protocol/command#Interface.
38 It is not a stable interface: commands may change or disappear without notice.
39
40 Examples:
41
42 $ gopls execute gopls.add_import '{"ImportPath": "fmt", "URI": "file:///hello.go"}'
43 $ gopls execute gopls.run_tests '{"URI": "file:///a_test.go", "Tests": ["Test"]}'
44 $ gopls execute gopls.list_known_packages '{"URI": "file:///hello.go"}'
45
46 execute-flags:
47 `)
48 printFlagDefaults(f)
49 }
50
51 func (e *execute) Run(ctx context.Context, args ...string) error {
52 if len(args) == 0 {
53 return tool.CommandLineErrorf("execute requires a command name")
54 }
55 cmd := args[0]
56 if !slices.Contains(command.Commands, command.Command(cmd)) {
57 return tool.CommandLineErrorf("unrecognized command: %s", cmd)
58 }
59
60 // A command may have multiple arguments, though the only one
61 // that currently does so is the "legacy" gopls.test,
62 // so we don't show an example of it.
63 var jsonArgs []json.RawMessage
64 for i, arg := range args[1:] {
65 var dummy any
66 if err := json.Unmarshal([]byte(arg), &dummy); err != nil {
67 return fmt.Errorf("argument %d is not valid JSON: %v", i+1, err)
68 }
69 jsonArgs = append(jsonArgs, json.RawMessage(arg))
70 }
71
72 e.app.editFlags = &e.EditFlags // in case command performs an edit
73
74 cli, _, err := e.app.connect(ctx)
75 if err != nil {
76 return err
77 }
78 defer cli.terminate(ctx)
79
80 res, err := executeCommand(ctx, cli.server, &protocol.Command{
81 Command: cmd,
82 Arguments: jsonArgs,
83 })
84 if err != nil {
85 return err
86 }
87 if res != nil {
88 data, err := json.MarshalIndent(res, "", "\t")
89 if err != nil {
90 log.Fatal(err)
91 }
92 fmt.Printf("%s\n", data)
93 }
94 return nil
95 }
96
97 // executeCommand executes a protocol.Command, displaying progress
98 // messages and awaiting completion of asynchronous commands.
99 func executeCommand(ctx context.Context, server protocol.Server, cmd *protocol.Command) (any, error) {
100 return server.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
101 Command: cmd.Command,
102 Arguments: cmd.Arguments,
103 })
104 }