Skip to content

Commit

Permalink
nvim: add Buffer event types (#106)
Browse files Browse the repository at this point in the history
* nvim: add Buffer event types

* nvim: fix testBufAttach testcase
  • Loading branch information
zchee authored Mar 8, 2021
1 parent a45dcbd commit 8eb594d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
53 changes: 31 additions & 22 deletions nvim/nvim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,8 @@ func testBufAttach(v *Nvim) func(*testing.T) {
return func(t *testing.T) {
clearBuffer(t, v, 0) // clear curret buffer text

type ChangedtickEvent struct {
Buffer Buffer
Changetick int64
}
type BufLinesEvent struct {
Buffer Buffer
Changetick int64
FirstLine int64
LastLine int64
LineData string
IsMultipart bool
}

changedtickChan := make(chan *ChangedtickEvent)
v.RegisterHandler("nvim_buf_changedtick_event", func(changedtickEvent ...interface{}) {
v.RegisterHandler(EventBufChangedtick, func(changedtickEvent ...interface{}) {
ev := &ChangedtickEvent{
Buffer: changedtickEvent[0].(Buffer),
Changetick: changedtickEvent[1].(int64),
Expand All @@ -136,18 +123,28 @@ func testBufAttach(v *Nvim) func(*testing.T) {
})

bufLinesChan := make(chan *BufLinesEvent)
v.RegisterHandler("nvim_buf_lines_event", func(bufLinesEvent ...interface{}) {
v.RegisterHandler(EventBufLines, func(bufLinesEvent ...interface{}) {
ev := &BufLinesEvent{
Buffer: bufLinesEvent[0].(Buffer),
Changetick: bufLinesEvent[1].(int64),
FirstLine: bufLinesEvent[2].(int64),
LastLine: bufLinesEvent[3].(int64),
LineData: fmt.Sprint(bufLinesEvent[4]),
IsMultipart: bufLinesEvent[5].(bool),
}
for _, line := range bufLinesEvent[4].([]interface{}) {
ev.LineData = append(ev.LineData, line.(string))
}
bufLinesChan <- ev
})

bufDetachChan := make(chan *BufDetachEvent)
v.RegisterHandler(EventBufDetach, func(bufDetachEvent ...interface{}) {
ev := &BufDetachEvent{
Buffer: bufDetachEvent[0].(Buffer),
}
bufDetachChan <- ev
})

ok, err := v.AttachBuffer(0, false, make(map[string]interface{})) // first 0 arg refers to the current buffer
if err != nil {
t.Fatal(err)
Expand All @@ -157,17 +154,20 @@ func testBufAttach(v *Nvim) func(*testing.T) {
}

changedtickExpected := &ChangedtickEvent{
Buffer: 1,
Buffer: Buffer(1),
Changetick: 3,
}
bufLinesEventExpected := &BufLinesEvent{
Buffer: 1,
Buffer: Buffer(1),
Changetick: 4,
FirstLine: 0,
LastLine: 1,
LineData: "[test]",
LineData: []string{"foo", "bar", "baz", "qux", "quux", "quuz"},
IsMultipart: false,
}
bufDetachEventExpected := &BufDetachEvent{
Buffer: Buffer(1),
}

var numEvent int64 // add and load should be atomically
errc := make(chan error)
Expand All @@ -176,7 +176,7 @@ func testBufAttach(v *Nvim) func(*testing.T) {
for {
select {
default:
if atomic.LoadInt64(&numEvent) == 2 { // end buf_attach test when handle 2 event
if atomic.LoadInt64(&numEvent) == 3 { // end buf_attach test when handle 2 event
done <- struct{}{}
return
}
Expand All @@ -190,6 +190,11 @@ func testBufAttach(v *Nvim) func(*testing.T) {
errc <- fmt.Errorf("bufLines = %+v, want %+v", bufLines, expected)
}
atomic.AddInt64(&numEvent, 1)
case detach := <-bufDetachChan:
if expected := bufDetachEventExpected; !reflect.DeepEqual(detach, expected) {
errc <- fmt.Errorf("bufDetach = %+v, want %+v", detach, expected)
}
atomic.AddInt64(&numEvent, 1)
}
}
}()
Expand All @@ -199,8 +204,12 @@ func testBufAttach(v *Nvim) func(*testing.T) {
close(errc)
}()

test := []byte("test")
if err := v.SetBufferLines(0, 0, -1, true, bytes.Fields(test)); err != nil { // first 0 arg refers to the current buffer
test := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz"), []byte("qux"), []byte("quux"), []byte("quuz")}
if err := v.SetBufferLines(Buffer(0), 0, -1, true, test); err != nil { // first 0 arg refers to the current buffer
t.Fatal(err)
}

if detached, err := v.DetachBuffer(Buffer(0)); err != nil || !detached {
t.Fatal(err)
}

Expand Down
32 changes: 32 additions & 0 deletions nvim/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
package nvim

const (
// EventBufChangedtick event name of "nvim_buf_changedtick_event".
EventBufChangedtick = "nvim_buf_changedtick_event"

// EventBufLines event name of "nvim_buf_lines_event".
EventBufLines = "nvim_buf_lines_event"

// EventBufDetach event name of "nvim_buf_detach_event".
EventBufDetach = "nvim_buf_detach_event"
)

// ChangedtickEvent represents a EventBufChangedtick type.
type ChangedtickEvent struct {
Buffer Buffer `msgpack:"buffer,omitempty"`
Changetick int64 `msgpack:"changetick,omitempty"`
}

// BufLinesEvent represents a EventBufLines type.
type BufLinesEvent struct {
Buffer Buffer `msgpack:"buffer,omitempty"`
Changetick int64 `msgpack:"changetick,omitempty"`
FirstLine int64 `msgpack:"firstLine,omitempty"`
LastLine int64 `msgpack:"lastLine,omitempty"`
LineData []string `msgpack:",array"`
IsMultipart bool `msgpack:"isMultipart,omitempty"`
}

// BufDetachEvent represents a EventBufDetach type.
type BufDetachEvent struct {
Buffer Buffer `msgpack:"buffer,omitempty"`
}

// QuickfixError represents an item in a quickfix list.
type QuickfixError struct {
// Buffer number
Expand Down

0 comments on commit 8eb594d

Please sign in to comment.