@@ -47,11 +47,13 @@ func (spec *pluginSpec) path() string {
47
47
if i := strings .Index (spec .sm , ":" ); i > 0 {
48
48
return spec .sm [:i ]
49
49
}
50
+
50
51
return ""
51
52
}
52
53
53
54
func isSync (f interface {}) bool {
54
55
t := reflect .TypeOf (f )
56
+
55
57
return t .Kind () == reflect .Func && t .NumOut () > 0
56
58
}
57
59
@@ -60,6 +62,7 @@ func (p *Plugin) handle(fn interface{}, spec *pluginSpec) {
60
62
if p .Nvim == nil {
61
63
return
62
64
}
65
+
63
66
if err := p .Nvim .RegisterHandler (spec .sm , fn ); err != nil {
64
67
panic (err )
65
68
}
@@ -82,6 +85,7 @@ func (p *Plugin) Handle(method string, fn interface{}) {
82
85
if p .Nvim == nil {
83
86
return
84
87
}
88
+
85
89
if err := p .Nvim .RegisterHandler (method , fn ); err != nil {
86
90
panic (err )
87
91
}
@@ -124,9 +128,11 @@ type FunctionOptions struct {
124
128
// {'GOPATH': $GOPATH, Cwd: getcwd()}
125
129
func (p * Plugin ) HandleFunction (options * FunctionOptions , fn interface {}) {
126
130
m := make (map [string ]string )
131
+
127
132
if options .Eval != "" {
128
- m [` eval` ] = eval (options .Eval , fn )
133
+ m [" eval" ] = eval (options .Eval , fn )
129
134
}
135
+
130
136
p .handle (fn , & pluginSpec {
131
137
sm : `0:function:` + options .Name ,
132
138
Type : `function` ,
@@ -231,12 +237,13 @@ func (p *Plugin) HandleCommand(options *CommandOptions, fn interface{}) {
231
237
m [`nargs` ] = options .NArgs
232
238
}
233
239
234
- if options .Range != "" {
235
- if options .Range == `.` {
236
- options .Range = ""
237
- }
240
+ switch {
241
+ case options .Range == `.` :
242
+ options .Range = ""
243
+ fallthrough
244
+ case options .Range != "" :
238
245
m [`range` ] = options .Range
239
- } else if options .Count != "" {
246
+ case options .Count != "" :
240
247
m [`count` ] = options .Count
241
248
}
242
249
@@ -308,20 +315,26 @@ type AutocmdOptions struct {
308
315
// documentation for information on how the expression is generated.
309
316
func (p * Plugin ) HandleAutocmd (options * AutocmdOptions , fn interface {}) {
310
317
pattern := ""
318
+
311
319
m := make (map [string ]string )
320
+
312
321
if options .Group != "" {
313
322
m [`group` ] = options .Group
314
323
}
324
+
315
325
if options .Pattern != "" {
316
326
m [`pattern` ] = options .Pattern
317
327
pattern = options .Pattern
318
328
}
329
+
319
330
if options .Nested {
320
- m [`nested` ] = "1"
331
+ m [`nested` ] = `1`
321
332
}
333
+
322
334
if options .Once {
323
- m [`once` ] = "1"
335
+ m [`once` ] = `1`
324
336
}
337
+
325
338
if options .Eval != "" {
326
339
m [`eval` ] = eval (options .Eval , fn )
327
340
}
@@ -330,6 +343,7 @@ func (p *Plugin) HandleAutocmd(options *AutocmdOptions, fn interface{}) {
330
343
ep := options .Event + ":" + pattern
331
344
i := p .eventPathCounts [ep ]
332
345
p .eventPathCounts [ep ] = i + 1
346
+
333
347
sm := fmt .Sprintf (`%d:autocmd:%s` , i , ep )
334
348
335
349
p .handle (fn , & pluginSpec {
@@ -348,6 +362,7 @@ func (p *Plugin) RegisterForTests() error {
348
362
for _ , spec := range p .pluginSpecs {
349
363
specs [spec .path ()] = append (specs [spec .path ()], spec )
350
364
}
365
+
351
366
const host = "nvim-go-test"
352
367
for path , specs := range specs {
353
368
if err := p .Nvim .Call (`remote#host#RegisterPlugin` , nil , host , path , specs ); err != nil {
@@ -363,20 +378,26 @@ func eval(eval string, f interface{}) string {
363
378
if eval != `*` {
364
379
return eval
365
380
}
381
+
366
382
ft := reflect .TypeOf (f )
367
383
if ft .Kind () != reflect .Func || ft .NumIn () < 1 {
368
384
panic (`Eval: "*" option requires function with at least one argument` )
369
385
}
386
+
370
387
argt := ft .In (ft .NumIn () - 1 )
371
388
if argt .Kind () != reflect .Ptr || argt .Elem ().Kind () != reflect .Struct {
372
389
panic (`Eval: "*" option requires function with pointer to struct as last argument` )
373
390
}
391
+
374
392
return structEval (argt .Elem ())
375
393
}
376
394
377
395
func structEval (t reflect.Type ) string {
378
- buf := []byte {'{' }
396
+ var sb strings.Builder
397
+
398
+ sb .WriteByte ('{' )
379
399
sep := ""
400
+
380
401
for i := 0 ; i < t .NumField (); i ++ {
381
402
sf := t .Field (i )
382
403
if sf .Anonymous {
@@ -389,11 +410,11 @@ func structEval(t reflect.Type) string {
389
410
if ft .Kind () == reflect .Ptr {
390
411
ft = ft .Elem ()
391
412
}
413
+
392
414
if ft .Kind () == reflect .Struct {
393
415
eval = structEval (ft )
394
416
}
395
417
}
396
-
397
418
if eval == "" {
398
419
continue
399
420
}
@@ -403,15 +424,16 @@ func structEval(t reflect.Type) string {
403
424
name = sf .Name
404
425
}
405
426
406
- buf = append ( buf , sep ... )
407
- buf = append ( buf , `'` ... )
408
- buf = append ( buf , name ... )
409
- buf = append ( buf , `':` ... )
410
- buf = append ( buf , eval ... )
411
- sep = `, `
427
+ sb . WriteString ( sep )
428
+ sb . WriteByte ( '\'' )
429
+ sb . WriteString ( name )
430
+ sb . WriteString ( "': " )
431
+ sb . WriteString ( eval )
432
+ sep = ", "
412
433
}
413
- buf = append (buf , '}' )
414
- return string (buf )
434
+ sb .WriteByte ('}' )
435
+
436
+ return sb .String ()
415
437
}
416
438
417
439
type byServiceMethod []* pluginSpec
@@ -432,18 +454,18 @@ func (p *Plugin) Manifest(host string) []byte {
432
454
path := spec .path ()
433
455
if path != prevPath {
434
456
if prevPath != "" {
435
- fmt .Fprintf (& buf , ` \\ )` )
457
+ fmt .Fprintf (& buf , " \\ )" )
436
458
}
437
- fmt .Fprintf (& buf , ` call remote#host#RegisterPlugin('%s', '%s', [\n` , host , path )
459
+ fmt .Fprintf (& buf , " call remote#host#RegisterPlugin('%s', '%s', [\n " , host , path )
438
460
prevPath = path
439
461
}
440
462
441
- sync := `0`
463
+ sync := "0"
442
464
if spec .Sync {
443
- sync = `1`
465
+ sync = "1"
444
466
}
445
467
446
- fmt .Fprintf (& buf , ` \\ {'type': '%s', 'name': '%s', 'sync': %s, 'opts': {` , spec .Type , spec .Name , sync )
468
+ fmt .Fprintf (& buf , " \\ {'type': '%s', 'name': '%s', 'sync': %s, 'opts': {" , spec .Type , spec .Name , sync )
447
469
448
470
var keys []string
449
471
for k := range spec .Opts {
@@ -453,14 +475,15 @@ func (p *Plugin) Manifest(host string) []byte {
453
475
454
476
optDelim := ""
455
477
for _ , k := range keys {
456
- fmt .Fprintf (& buf , ` %s'%s': '%s'` , optDelim , k , escape (spec .Opts [k ]))
457
- optDelim = `,`
478
+ fmt .Fprintf (& buf , " %s'%s': '%s'" , optDelim , k , escape (spec .Opts [k ]))
479
+ optDelim = ","
458
480
}
459
481
460
- fmt .Fprintf (& buf , ` }},\n` )
482
+ fmt .Fprintf (& buf , " }},\n " )
461
483
}
462
484
if prevPath != "" {
463
- fmt .Fprintf (& buf , ` \\ ])\n` )
485
+ fmt .Fprintf (& buf , " \\ ])\n " )
464
486
}
487
+
465
488
return buf .Bytes ()
466
489
}
0 commit comments