Skip to content

Commit 767661b

Browse files
zcheejustinmk
authored andcommitted
Fix SetBufferVirtualText chunks arg type (#51)
* nvim: fix SetBufferVirtualText chunks arg type and fix godoc * nvim: go generate * nvim: add clearBuffer test function * nvim: add SetBufferVirtualText testcase * nvim: add []VirtualTextChunk to Array C type
1 parent eaaca6f commit 767661b

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

nvim/apidef.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func ClearBufferHighlight(buffer Buffer, srcID int, startLine int, endLine int)
203203
name(nvim_buf_clear_highlight)
204204
}
205205

206-
// Set the virtual text (annotation) for a buffer line.
206+
// SetBufferVirtualText sets the virtual text (annotation) for a buffer line.
207207
//
208208
// By default (and currently the only option) the text will be placed after
209209
// the buffer text. Virtual text will never cause reflow, rather virtual
@@ -222,7 +222,7 @@ func ClearBufferHighlight(buffer Buffer, srcID int, startLine int, endLine int)
222222
// The `opts` is optional parameters. Currently not used.
223223
//
224224
// The returns the nsID that was used.
225-
func SetBufferVirtualText(buffer Buffer, nsID int, line int, chunks []interface{}, opts map[string]interface{}) int {
225+
func SetBufferVirtualText(buffer Buffer, nsID int, line int, chunks []VirtualTextChunk, opts map[string]interface{}) int {
226226
name(nvim_buf_set_virtual_text)
227227
}
228228

nvim/apiimp.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nvim/apitool.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,14 @@ var nvimTypes = map[string]string{
308308
"[][]byte": "ArrayOf(String)",
309309
"[]string": "ArrayOf(String)",
310310

311-
"Mode": "Dictionary",
312-
"*HLAttrs": "Dictionary",
313-
"*Channel": "Dictionary",
314-
"[]*Channel": "Array",
315-
"[]*Mapping": "ArrayOf(Dictionary)",
316-
"[]*Process": "Array",
317-
"[]*UI": "Array",
311+
"Mode": "Dictionary",
312+
"*HLAttrs": "Dictionary",
313+
"*Channel": "Dictionary",
314+
"[]*Channel": "Array",
315+
"[]*Mapping": "ArrayOf(Dictionary)",
316+
"[]*Process": "Array",
317+
"[]*UI": "Array",
318+
"[]VirtualTextChunk": "Array",
318319
}
319320

320321
func convertToNvimTypes(f *Function) *Function {

nvim/nvim_test.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,7 @@ func TestAPI(t *testing.T) {
383383
})
384384

385385
t.Run("buf_attach", func(t *testing.T) {
386-
buffer, err := v.CurrentBuffer()
387-
if err != nil {
388-
t.Fatal(err)
389-
}
390-
391-
// clear curret buffer text
392-
if err := v.SetBufferLines(buffer, 0, -1, true, bytes.Fields(nil)); err != nil {
393-
t.Fatal(err)
394-
}
386+
clearBuffer(t, v, 0) // clear curret buffer text
395387

396388
type ChangedtickEvent struct {
397389
Buffer Buffer
@@ -428,7 +420,7 @@ func TestAPI(t *testing.T) {
428420
bufLinesChan <- ev
429421
})
430422

431-
ok, err := v.AttachBuffer(buffer, false, make(map[string]interface{}))
423+
ok, err := v.AttachBuffer(0, false, make(map[string]interface{})) // first 0 arg refers to the current buffer
432424
if err != nil {
433425
t.Fatal(err)
434426
}
@@ -480,7 +472,7 @@ func TestAPI(t *testing.T) {
480472
}()
481473

482474
test := []byte("test")
483-
if err := v.SetBufferLines(buffer, 0, -1, true, bytes.Fields(test)); err != nil {
475+
if err := v.SetBufferLines(0, 0, -1, true, bytes.Fields(test)); err != nil { // first 0 arg refers to the current buffer
484476
t.Fatal(err)
485477
}
486478

@@ -490,6 +482,39 @@ func TestAPI(t *testing.T) {
490482
}
491483
}
492484
})
485+
486+
t.Run("virtual_text", func(t *testing.T) {
487+
clearBuffer(t, v, 0) // clear curret buffer text
488+
489+
nsID, err := v.CreateNamespace("test_virtual_text")
490+
if err != nil {
491+
t.Fatal(err)
492+
}
493+
494+
lines := []byte("ping")
495+
if err := v.SetBufferLines(0, 0, -1, true, bytes.Fields(lines)); err != nil {
496+
t.Fatal(err)
497+
}
498+
499+
chunks := []VirtualTextChunk{
500+
{
501+
Text: "pong",
502+
HLGroup: "String",
503+
},
504+
}
505+
nsID2, err := v.SetBufferVirtualText(0, nsID, 0, chunks, make(map[string]interface{}))
506+
if err != nil {
507+
t.Fatal(err)
508+
}
509+
510+
if got := nsID2; got != nsID {
511+
t.Fatalf("namespaceID: got %d, want %d", got, nsID)
512+
}
513+
514+
if err := v.ClearBufferNamespace(0, nsID, 0, -1); err != nil {
515+
t.Fatal(err)
516+
}
517+
})
493518
}
494519

495520
func TestDial(t *testing.T) {
@@ -563,3 +588,10 @@ func TestEmbedded(t *testing.T) {
563588
t.Fatal("timeout waiting for serve to exit")
564589
}
565590
}
591+
592+
// clearBuffer clear the buffer text.
593+
func clearBuffer(t *testing.T, v *Nvim, buffer Buffer) {
594+
if err := v.SetBufferLines(buffer, 0, -1, true, bytes.Fields(nil)); err != nil {
595+
t.Fatal(err)
596+
}
597+
}

nvim/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,9 @@ type Command struct {
229229
CompleteArg string `msgpack:"complete_arg,omitempty"`
230230
Definition string `msgpack:"definition"`
231231
}
232+
233+
// VirtualTextChunk represents a virtual text chunk.
234+
type VirtualTextChunk struct {
235+
Text string `msgpack:",array"`
236+
HLGroup string `msgpack:",array"`
237+
}

0 commit comments

Comments
 (0)