1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use failure::Error;
use std::fmt;
use std::sync::mpsc;
use neovim_lib::neovim::Neovim;
use neovim_lib::neovim_api::Buffer;
use neovim_lib::neovim_api::NeovimApi;
use folds::FoldList;
use lines::Lines;
use neovim_ext::BufferExt;
pub enum Event {
LinesEvent {
buf: Buffer,
changedtick: u64,
firstline: i64,
lastline: i64,
linedata: Vec<String>,
more: bool,
},
ChangedTickEvent { buf: Buffer, changedtick: u64 },
DetachEvent { buf: Buffer },
RefreshFolds,
Quit,
}
impl Event {
pub fn event_loop(
receiver: &mpsc::Receiver<Event>,
mut nvim: Neovim,
) -> Result<(), Error> {
use self::Event::*;
let curbuf = nvim.get_current_buf()?;
curbuf.attach(&mut nvim, true, vec![])?;
let mut foldlist = FoldList::new();
let mut lines = Lines::new(Vec::new());
loop {
match receiver.recv() {
Ok(LinesEvent {
firstline,
lastline,
linedata,
..
}) => {
if lastline == -1 {
lines = Lines::new(linedata);
foldlist.recreate_all(&lines)?;
foldlist.resend_all(&mut nvim)?;
} else if lastline >= 0 && firstline >= 0 {
lines.update(firstline as usize, lastline as usize, linedata);
foldlist.recreate_all(&lines)?;
} else {
error!(
"LinesEvent only works with nonnegative numbers, except for
lastline = -1!"
);
}
}
Ok(RefreshFolds) => {
foldlist.resend_all(&mut nvim)?;
}
Ok(Quit) => {
break;
}
Ok(o) => {
warn!("receiver recieved {:?}", o);
}
Err(e) => {
warn!("receiver received error: {:?}", e);
}
}
}
info!("quitting");
Ok(())
}
}
impl fmt::Debug for Event {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Event::*;
match *self {
LinesEvent {
changedtick,
firstline,
lastline,
ref linedata,
..
} => write!(
f,
"Update{{ changedtick: {}, firstline: {}, \
lastline: {}, #linedata: {} }}",
changedtick,
firstline,
lastline,
linedata.len()
),
ChangedTickEvent { changedtick, .. } => {
write!(f, "ChangedTick{{ changedtick: {} }}", changedtick,)
}
DetachEvent { .. } => write!(f, "UpdatesEnd"),
RefreshFolds => write!(f, "RefreshFolds"),
Quit => write!(f, "Quit"),
}
}
}