Skip to content

Commit 8eb594d

Browse files
authored
nvim: add Buffer event types (#106)
* nvim: add Buffer event types * nvim: fix testBufAttach testcase
1 parent a45dcbd commit 8eb594d

File tree

2 files changed

+63
-22
lines changed

2 files changed

+63
-22
lines changed

nvim/nvim_test.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,8 @@ func testBufAttach(v *Nvim) func(*testing.T) {
113113
return func(t *testing.T) {
114114
clearBuffer(t, v, 0) // clear curret buffer text
115115

116-
type ChangedtickEvent struct {
117-
Buffer Buffer
118-
Changetick int64
119-
}
120-
type BufLinesEvent struct {
121-
Buffer Buffer
122-
Changetick int64
123-
FirstLine int64
124-
LastLine int64
125-
LineData string
126-
IsMultipart bool
127-
}
128-
129116
changedtickChan := make(chan *ChangedtickEvent)
130-
v.RegisterHandler("nvim_buf_changedtick_event", func(changedtickEvent ...interface{}) {
117+
v.RegisterHandler(EventBufChangedtick, func(changedtickEvent ...interface{}) {
131118
ev := &ChangedtickEvent{
132119
Buffer: changedtickEvent[0].(Buffer),
133120
Changetick: changedtickEvent[1].(int64),
@@ -136,18 +123,28 @@ func testBufAttach(v *Nvim) func(*testing.T) {
136123
})
137124

138125
bufLinesChan := make(chan *BufLinesEvent)
139-
v.RegisterHandler("nvim_buf_lines_event", func(bufLinesEvent ...interface{}) {
126+
v.RegisterHandler(EventBufLines, func(bufLinesEvent ...interface{}) {
140127
ev := &BufLinesEvent{
141128
Buffer: bufLinesEvent[0].(Buffer),
142129
Changetick: bufLinesEvent[1].(int64),
143130
FirstLine: bufLinesEvent[2].(int64),
144131
LastLine: bufLinesEvent[3].(int64),
145-
LineData: fmt.Sprint(bufLinesEvent[4]),
146132
IsMultipart: bufLinesEvent[5].(bool),
147133
}
134+
for _, line := range bufLinesEvent[4].([]interface{}) {
135+
ev.LineData = append(ev.LineData, line.(string))
136+
}
148137
bufLinesChan <- ev
149138
})
150139

140+
bufDetachChan := make(chan *BufDetachEvent)
141+
v.RegisterHandler(EventBufDetach, func(bufDetachEvent ...interface{}) {
142+
ev := &BufDetachEvent{
143+
Buffer: bufDetachEvent[0].(Buffer),
144+
}
145+
bufDetachChan <- ev
146+
})
147+
151148
ok, err := v.AttachBuffer(0, false, make(map[string]interface{})) // first 0 arg refers to the current buffer
152149
if err != nil {
153150
t.Fatal(err)
@@ -157,17 +154,20 @@ func testBufAttach(v *Nvim) func(*testing.T) {
157154
}
158155

159156
changedtickExpected := &ChangedtickEvent{
160-
Buffer: 1,
157+
Buffer: Buffer(1),
161158
Changetick: 3,
162159
}
163160
bufLinesEventExpected := &BufLinesEvent{
164-
Buffer: 1,
161+
Buffer: Buffer(1),
165162
Changetick: 4,
166163
FirstLine: 0,
167164
LastLine: 1,
168-
LineData: "[test]",
165+
LineData: []string{"foo", "bar", "baz", "qux", "quux", "quuz"},
169166
IsMultipart: false,
170167
}
168+
bufDetachEventExpected := &BufDetachEvent{
169+
Buffer: Buffer(1),
170+
}
171171

172172
var numEvent int64 // add and load should be atomically
173173
errc := make(chan error)
@@ -176,7 +176,7 @@ func testBufAttach(v *Nvim) func(*testing.T) {
176176
for {
177177
select {
178178
default:
179-
if atomic.LoadInt64(&numEvent) == 2 { // end buf_attach test when handle 2 event
179+
if atomic.LoadInt64(&numEvent) == 3 { // end buf_attach test when handle 2 event
180180
done <- struct{}{}
181181
return
182182
}
@@ -190,6 +190,11 @@ func testBufAttach(v *Nvim) func(*testing.T) {
190190
errc <- fmt.Errorf("bufLines = %+v, want %+v", bufLines, expected)
191191
}
192192
atomic.AddInt64(&numEvent, 1)
193+
case detach := <-bufDetachChan:
194+
if expected := bufDetachEventExpected; !reflect.DeepEqual(detach, expected) {
195+
errc <- fmt.Errorf("bufDetach = %+v, want %+v", detach, expected)
196+
}
197+
atomic.AddInt64(&numEvent, 1)
193198
}
194199
}
195200
}()
@@ -199,8 +204,12 @@ func testBufAttach(v *Nvim) func(*testing.T) {
199204
close(errc)
200205
}()
201206

202-
test := []byte("test")
203-
if err := v.SetBufferLines(0, 0, -1, true, bytes.Fields(test)); err != nil { // first 0 arg refers to the current buffer
207+
test := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz"), []byte("qux"), []byte("quux"), []byte("quuz")}
208+
if err := v.SetBufferLines(Buffer(0), 0, -1, true, test); err != nil { // first 0 arg refers to the current buffer
209+
t.Fatal(err)
210+
}
211+
212+
if detached, err := v.DetachBuffer(Buffer(0)); err != nil || !detached {
204213
t.Fatal(err)
205214
}
206215

nvim/types.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
11
package nvim
22

3+
const (
4+
// EventBufChangedtick event name of "nvim_buf_changedtick_event".
5+
EventBufChangedtick = "nvim_buf_changedtick_event"
6+
7+
// EventBufLines event name of "nvim_buf_lines_event".
8+
EventBufLines = "nvim_buf_lines_event"
9+
10+
// EventBufDetach event name of "nvim_buf_detach_event".
11+
EventBufDetach = "nvim_buf_detach_event"
12+
)
13+
14+
// ChangedtickEvent represents a EventBufChangedtick type.
15+
type ChangedtickEvent struct {
16+
Buffer Buffer `msgpack:"buffer,omitempty"`
17+
Changetick int64 `msgpack:"changetick,omitempty"`
18+
}
19+
20+
// BufLinesEvent represents a EventBufLines type.
21+
type BufLinesEvent struct {
22+
Buffer Buffer `msgpack:"buffer,omitempty"`
23+
Changetick int64 `msgpack:"changetick,omitempty"`
24+
FirstLine int64 `msgpack:"firstLine,omitempty"`
25+
LastLine int64 `msgpack:"lastLine,omitempty"`
26+
LineData []string `msgpack:",array"`
27+
IsMultipart bool `msgpack:"isMultipart,omitempty"`
28+
}
29+
30+
// BufDetachEvent represents a EventBufDetach type.
31+
type BufDetachEvent struct {
32+
Buffer Buffer `msgpack:"buffer,omitempty"`
33+
}
34+
335
// QuickfixError represents an item in a quickfix list.
436
type QuickfixError struct {
537
// Buffer number

0 commit comments

Comments
 (0)