1 // Copyright 2021 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.
12 // ID returns the command name for use in the LSP.
13 func ID(name string) string {
14 return "gopls." + name
19 func (c Command) ID() string {
23 // MarshalArgs encodes the given arguments to json.RawMessages. This function
24 // is used to construct arguments to a protocol.Command.
28 // jsonArgs, err := EncodeArgs(1, "hello", true, StructuredArg{42, 12.6})
30 func MarshalArgs(args ...interface{}) ([]json.RawMessage, error) {
31 var out []json.RawMessage
32 for _, arg := range args {
33 argJSON, err := json.Marshal(arg)
37 out = append(out, argJSON)
42 // UnmarshalArgs decodes the given json.RawMessages to the variables provided
43 // by args. Each element of args should be a pointer.
51 // structured StructuredArg
53 // err := UnmarshalArgs(args, &num, &str, &bul, &structured)
55 func UnmarshalArgs(jsonArgs []json.RawMessage, args ...interface{}) error {
56 if len(args) != len(jsonArgs) {
57 return fmt.Errorf("DecodeArgs: expected %d input arguments, got %d JSON arguments", len(args), len(jsonArgs))
59 for i, arg := range args {
60 if err := json.Unmarshal(jsonArgs[i], arg); err != nil {