11//go:build ignore
22// +build ignore
33
4- // Command apitool generates api.go from api_def.go. The command also has
4+ // Command api_tool generates api.go from api_def.go. The command also has
55// an option to compare api_def.go to Nvim's current API meta data.
66package main
77
@@ -122,13 +122,16 @@ func parseFields(fset *token.FileSet, fl *ast.FieldList) []*Field {
122122}
123123
124124// parseAPIDef parses the file api_def.go.
125- func parseAPIDef () ([]* Function , error ) {
125+ func parseAPIDef () ([]* Function , [] * Function , error ) {
126126 fset := token .NewFileSet ()
127127 file , err := parser .ParseFile (fset , "api_def.go" , nil , parser .ParseComments )
128128 if err != nil {
129- return nil , err
129+ return nil , nil , err
130130 }
131+
131132 var functions []* Function
133+ var deprecateds []* Function
134+
132135 for _ , decl := range file .Decls {
133136 fdecl , ok := decl .(* ast.FuncDecl )
134137 if ! ok {
@@ -148,10 +151,13 @@ func parseAPIDef() ([]*Function, error) {
148151 Doc : string (doc ),
149152 Parameters : parseFields (fset , fdecl .Type .Params ),
150153 }
154+
151155 fields := parseFields (fset , fdecl .Type .Results )
152156 if len (fields ) > 1 {
153- return nil , fmt .Errorf ("%s: more than one result for %s" , fset .Position (fdecl .Pos ()), m .Name )
154- } else if len (fields ) == 1 {
157+ return nil , nil , fmt .Errorf ("%s: more than one result for %s" , fset .Position (fdecl .Pos ()), m .Name )
158+ }
159+
160+ if len (fields ) == 1 {
155161 m .ReturnName = fields [0 ].Name
156162 m .ReturnType = fields [0 ].Type
157163 }
@@ -179,65 +185,22 @@ func parseAPIDef() ([]*Function, error) {
179185 }
180186 }
181187 }
188+
182189 if m .Name == "" {
183- return nil , fmt .Errorf ("%s: service method not specified for %s" , fset .Position (fdecl .Pos ()), m .Name )
190+ return nil , nil , fmt .Errorf ("%s: service method not specified for %s" , fset .Position (fdecl .Pos ()), m .Name )
191+ }
192+
193+ if m .DeprecatedSince > 0 {
194+ deprecateds = append (deprecateds , m )
195+ continue
184196 }
185197 functions = append (functions , m )
186198 }
187- return functions , nil
188- }
189-
190- var implementationTemplate = template .Must (template .New ("" ).Funcs (template.FuncMap {
191- "lower" : strings .ToLower ,
192- }).Parse (`// Code generated by running "go generate" in github.com/neovim/go-client/nvim. DO NOT EDIT.
193-
194- package nvim
195-
196- import (
197- "fmt"
198-
199- "github.com/neovim/go-client/msgpack"
200- "github.com/neovim/go-client/msgpack/rpc"
201- )
202-
203- const (
204- {{- range $name, $type := .ErrorTypes}}
205- {{- lower $name}}Error = {{$type.ID}}
206- {{ end -}}
207- )
208-
209- func withExtensions() rpc.Option {
210- return rpc.WithExtensions(msgpack.ExtensionMap{
211- {{- range $name, $type := .Types}}
212- {{$type.ID}}: func(p []byte) (interface{}, error) {
213- x, err := decodeExt(p)
214- return {{$name}}(x), err
215- },
216- {{end -}}
217- })
218- }
219-
220- {{range $name, $type := .Types}}
221- {{$type.Doc}}
222- type {{$name}} int
223-
224- // MarshalMsgPack implements msgpack.Marshaler.
225- func (x {{$name}}) MarshalMsgPack(enc *msgpack.Encoder) error {
226- return enc.PackExtension({{$type.ID}}, encodeExt(int(x)))
227- }
228199
229- // UnmarshalMsgPack implements msgpack.Unmarshaler.
230- func (x *{{$name}}) UnmarshalMsgPack(dec *msgpack.Decoder) error {
231- n, err := unmarshalExt(dec, {{$type.ID}}, x)
232- *x = {{$name}}(n)
233- return err
200+ return functions , deprecateds , nil
234201}
235202
236- // String returns a string representation of the {{$name}}.
237- func (x {{$name}}) String() string {
238- return fmt.Sprintf("{{$name}}:%d", int(x))
239- }
240- {{end}}
203+ const genTemplate = `
241204
242205{{range .Functions}}
243206{{if eq "interface{}" .ReturnType}}
@@ -295,11 +258,137 @@ func (b *Batch) {{.GoName}}({{range .Parameters}}{{.Name}} {{.Type}},{{end}}) {
295258}
296259{{end}}
297260{{end}}
298- ` ))
261+ `
262+
263+ var implementationTemplate = template .Must (template .New ("implementation" ).Funcs (template.FuncMap {
264+ "lower" : strings .ToLower ,
265+ }).Parse (`// Code generated by running "go generate" in github.com/neovim/go-client/nvim. DO NOT EDIT.
266+
267+ package nvim
268+
269+ import (
270+ "fmt"
271+
272+ "github.com/neovim/go-client/msgpack"
273+ "github.com/neovim/go-client/msgpack/rpc"
274+ )
275+
276+ const (
277+ {{- range $name, $type := .ErrorTypes}}
278+ {{- lower $name}}Error = {{$type.ID}}
279+ {{ end -}}
280+ )
281+
282+ func withExtensions() rpc.Option {
283+ return rpc.WithExtensions(msgpack.ExtensionMap{
284+ {{- range $name, $type := .Types}}
285+ {{$type.ID}}: func(p []byte) (interface{}, error) {
286+ x, err := decodeExt(p)
287+ return {{$name}}(x), err
288+ },
289+ {{end -}}
290+ })
291+ }
292+
293+ {{range $name, $type := .Types}}
294+ {{$type.Doc}}
295+ type {{$name}} int
296+
297+ // MarshalMsgPack implements msgpack.Marshaler.
298+ func (x {{$name}}) MarshalMsgPack(enc *msgpack.Encoder) error {
299+ return enc.PackExtension({{$type.ID}}, encodeExt(int(x)))
300+ }
301+
302+ // UnmarshalMsgPack implements msgpack.Unmarshaler.
303+ func (x *{{$name}}) UnmarshalMsgPack(dec *msgpack.Decoder) error {
304+ n, err := unmarshalExt(dec, {{$type.ID}}, x)
305+ *x = {{$name}}(n)
306+ return err
307+ }
308+
309+ // String returns a string representation of the {{$name}}.
310+ func (x {{$name}}) String() string {
311+ return fmt.Sprintf("{{$name}}:%d", int(x))
312+ }
313+ {{end}}
314+ ` + genTemplate ))
315+
316+ var deprecatedTemplate = template .Must (template .New ("deprecated" ).Funcs (template.FuncMap {
317+ "lower" : strings .ToLower ,
318+ }).Parse (`// Code generated by running "go generate" in github.com/neovim/go-client/nvim. DO NOT EDIT.
299319
300- func printImplementation (functions []* Function , outFile string ) error {
320+ package nvim
321+
322+ // EmbedOptions specifies options for starting an embedded instance of Nvim.
323+ //
324+ // Deprecated: Use ChildProcessOption instead.
325+ type EmbedOptions struct {
326+ // Logf log function for rpc.WithLogf.
327+ Logf func(string, ...interface{})
328+
329+ // Dir specifies the working directory of the command. The working
330+ // directory in the current process is used if Dir is "".
331+ Dir string
332+
333+ // Path is the path of the command to run. If Path = "", then
334+ // StartEmbeddedNvim searches for "nvim" on $PATH.
335+ Path string
336+
337+ // Args specifies the command line arguments. Do not include the program
338+ // name (the first argument) or the --embed option.
339+ Args []string
340+
341+ // Env specifies the environment of the Nvim process. The current process
342+ // environment is used if Env is nil.
343+ Env []string
344+ }
345+
346+ // NewEmbedded starts an embedded instance of Nvim using the specified options.
347+ //
348+ // The application must call Serve() to handle RPC requests and responses.
349+ //
350+ // Deprecated: Use NewChildProcess instead.
351+ func NewEmbedded(options *EmbedOptions) (*Nvim, error) {
352+ if options == nil {
353+ options = &EmbedOptions{}
354+ }
355+ path := options.Path
356+ if path == "" {
357+ path = "nvim"
358+ }
359+
360+ return NewChildProcess(
361+ ChildProcessArgs(append([]string{"--embed"}, options.Args...)...),
362+ ChildProcessCommand(path),
363+ ChildProcessEnv(options.Env),
364+ ChildProcessDir(options.Dir),
365+ ChildProcessServe(false))
366+ }
367+
368+ // ExecuteLua executes a Lua block.
369+ //
370+ // Deprecated: Use ExecLua instead.
371+ func (v *Nvim) ExecuteLua(code string, result interface{}, args ...interface{}) error {
372+ if args == nil {
373+ args = []interface{}{}
374+ }
375+ return v.call("nvim_execute_lua", result, code, args)
376+ }
377+
378+ // ExecuteLua executes a Lua block.
379+ //
380+ // Deprecated: Use ExecLua instead.
381+ func (b *Batch) ExecuteLua(code string, result interface{}, args ...interface{}) {
382+ if args == nil {
383+ args = []interface{}{}
384+ }
385+ b.call("nvim_execute_lua", result, code, args)
386+ }
387+ ` + genTemplate ))
388+
389+ func printImplementation (functions []* Function , tmpl * template.Template , outFile string ) error {
301390 var buf bytes.Buffer
302- if err := implementationTemplate .Execute (& buf , & APIInfo {
391+ if err := tmpl .Execute (& buf , & APIInfo {
303392 Functions : functions ,
304393 Types : extensionTypes ,
305394 ErrorTypes : errorTypes ,
@@ -517,6 +606,7 @@ func main() {
517606 log .SetFlags (log .Lshortfile )
518607
519608 generateFlag := flag .String ("generate" , "" , "Generate implementation from api_def.go and write to `file`" )
609+ deprecatedFlag := flag .String ("deprecated" , "" , "Generate deprecated implementation from api_def.go and write to `file`" )
520610 compareFlag := flag .Bool ("compare" , false , "Compare api_def.go to the output of nvim --api-info" )
521611 dumpFlag := flag .Bool ("dump" , false , "Print nvim --api-info as JSON" )
522612 flag .Parse ()
@@ -528,16 +618,25 @@ func main() {
528618 return
529619 }
530620
531- functions , err := parseAPIDef ()
621+ functions , deprecateds , err := parseAPIDef ()
532622 if err != nil {
533623 log .Fatal (err )
534624 }
535625
536626 switch {
537627 case * compareFlag :
628+ functions = append (functions , deprecateds ... )
538629 err = compareFunctions (functions )
539630 default :
540- err = printImplementation (functions , * generateFlag )
631+ if * generateFlag != "" {
632+ if * deprecatedFlag == "" {
633+ functions = append (functions , deprecateds ... )
634+ }
635+ err = printImplementation (functions , implementationTemplate , * generateFlag )
636+ }
637+ if * deprecatedFlag != "" {
638+ err = printImplementation (deprecateds , deprecatedTemplate , * deprecatedFlag )
639+ }
541640 }
542641 if err != nil {
543642 log .Fatal (err )
0 commit comments