Debian Codegraph @

gopls/internal/protocol/command/gen/gen.go

raw symbols SCIP
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.
4
5 // Package gen is used to generate command bindings from the gopls command
6 // interface.
7 package gen
8
9 import (
10 "bytes"
11 "fmt"
12 "go/types"
13 "log"
14 "text/template"
15
16 "golang.org/x/tools/gopls/internal/protocol/command/commandmeta"
17 "golang.org/x/tools/internal/imports"
18 "golang.org/x/tools/internal/typesinternal"
19 )
20
21 const src = `// Copyright 2024 The Go Authors. All rights reserved.
22 // Use of this source code is governed by a BSD-style
23 // license that can be found in the LICENSE file.
24
25 // Don't include this file during code generation, or it will break the build
26 // if existing interface methods have been modified.
27 //go:build !generate
28 // +build !generate
29
30 // Code generated by gen.go from gopls/internal/protocol/command. DO NOT EDIT.
31
32 package command
33
34 import (
35 {{range $k, $v := .Imports -}}
36 "{{$k}}"
37 {{end}}
38 )
39
40 // Symbolic names for gopls commands, corresponding to methods of [Interface].
41 //
42 // The string value is used in the Command field of protocol.Command.
43 // These commands may be obtained from a CodeLens or CodeAction request
44 // and executed by an ExecuteCommand request.
45 const (
46 {{- range .Commands}}
47 {{.MethodName}} Command = "{{.Name}}"
48 {{- end}}
49 )
50
51 var Commands = []Command {
52 {{- range .Commands}}
53 {{.MethodName}},
54 {{- end}}
55 }
56
57 func Dispatch(ctx context.Context, params *protocol.ExecuteCommandParams, s Interface) (any, error) {
58 switch Command(params.Command) {
59 {{- range .Commands}}
60 case {{.MethodName}}:
61 {{- if .Args -}}
62 {{- range $i, $v := .Args}}
63 var a{{$i}} {{typeString $v.Type}}
64 {{- end}}
65 if err := UnmarshalArgs(params.Arguments{{range $i, $v := .Args}}, &a{{$i}}{{end}}); err != nil {
66 return nil, err
67 }
68 {{end -}}
69 return {{if not .Result}}nil, {{end}}s.{{.MethodName}}(ctx{{range $i, $v := .Args}}, a{{$i}}{{end}}{{if .Interactive}}, &params.InteractiveParams{{end}})
70 {{- end}}
71 }
72 return nil, fmt.Errorf("unsupported command %q", params.Command)
73 }
74 {{- range .Commands}}
75
76 {{if fallible .Args}}
77 func New{{.MethodName}}Command(title string, {{range $i, $v := .Args}}{{if $i}}, {{end}}a{{$i}} {{typeString $v.Type}}{{end}}) (*protocol.Command, error) {
78 args, err := MarshalArgs({{range $i, $v := .Args}}{{if $i}}, {{end}}a{{$i}}{{end}})
79 if err != nil {
80 return nil, err
81 }
82 return &protocol.Command{
83 Title: title,
84 Command: {{.MethodName}}.String(),
85 Arguments: args,
86 }, nil
87 }
88 {{else}}
89 func New{{.MethodName}}Command(title string, {{range $i, $v := .Args}}{{if $i}}, {{end}}a{{$i}} {{typeString $v.Type}}{{end}}) *protocol.Command {
90 return &protocol.Command{
91 Title: title,
92 Command: {{.MethodName}}.String(),
93 Arguments: MustMarshalArgs({{range $i, $v := .Args}}{{if $i}}, {{end}}a{{$i}}{{end}}),
94 }
95 }
96 {{end}}
97
98 {{end}}
99 `
100
101 type data struct {
102 Imports map[string]bool
103 Commands []*commandmeta.Command
104 }
105
106 // Generate computes the new contents of ../command_gen.go from a
107 // static analysis of the command.Interface type.
108 func Generate() ([]byte, error) {
109 cmds, err := commandmeta.Load()
110 if err != nil {
111 return nil, fmt.Errorf("loading command data: %v", err)
112 }
113 const thispkg = "golang.org/x/tools/gopls/internal/protocol/command"
114 qual := func(p *types.Package) string {
115 if p.Path() == thispkg {
116 return ""
117 }
118 return p.Name()
119 }
120 tmpl, err := template.New("").Funcs(template.FuncMap{
121 "typeString": func(t types.Type) string {
122 return types.TypeString(t, qual)
123 },
124 "fallible": func(args []*commandmeta.Field) bool {
125 var fallible func(types.Type) bool
126 fallible = func(t types.Type) bool {
127 switch t := t.Underlying().(type) {
128 case *types.Basic:
129 return false
130 case *types.Slice:
131 return fallible(t.Elem())
132 case *types.Struct:
133 for field := range t.Fields() {
134 if fallible(field.Type()) {
135 return true
136 }
137 }
138 return false
139 }
140 // Assume all other types are fallible for now:
141 log.Println("Command.Args has fallible type", t)
142 return true
143 }
144 for i, arg := range args {
145 if i == len(args)-1 && typesinternal.IsPointerToNamed(arg.Type, "golang.org/x/tools/gopls/internal/protocol", "InteractiveParams") {
146 continue
147 }
148
149 if fallible(arg.Type) {
150 return true
151 }
152 }
153 return false
154 },
155 }).Parse(src)
156 if err != nil {
157 return nil, err
158 }
159 d := data{
160 Commands: cmds,
161 Imports: map[string]bool{
162 "context": true,
163 "fmt": true,
164 "golang.org/x/tools/gopls/internal/protocol": true,
165 },
166 }
167 for _, c := range d.Commands {
168 for _, arg := range c.Args {
169 pth := pkgPath(arg.Type)
170 if pth != "" && pth != thispkg {
171 d.Imports[pth] = true
172 }
173 }
174 if c.Result != nil {
175 pth := pkgPath(c.Result.Type)
176 if pth != "" && pth != thispkg {
177 d.Imports[pth] = true
178 }
179 }
180 }
181
182 var buf bytes.Buffer
183 if err := tmpl.Execute(&buf, d); err != nil {
184 return nil, fmt.Errorf("executing: %v", err)
185 }
186
187 opts := &imports.Options{
188 AllErrors: true,
189 FormatOnly: true,
190 Comments: true,
191 }
192 content, err := imports.Process("", buf.Bytes(), opts)
193 if err != nil {
194 return nil, fmt.Errorf("goimports: %v", err)
195 }
196 return content, nil
197 }
198
199 func pkgPath(t types.Type) string {
200 if tname := typesinternal.TypeNameFor(t); tname != nil {
201 if pkg := tname.Pkg(); pkg != nil {
202 return pkg.Path()
203 }
204 }
205 return ""
206 }