@@ -37,6 +37,7 @@ pub struct Editor {
37 first_line: u16,
37 first_line: u16,
38 size: (u16, u16),
38 size: (u16, u16),
39 surface: Surface,
39 surface: Surface,
40 theme: Theme,
41 theme: Theme,
@@ -55,6 +56,7 @@ impl Editor {
55 first_line: 0,
56 first_line: 0,
57 surface: Surface::empty(area),
58 surface: Surface::empty(area),
59+ cache: Surface::empty(area),
59 // TODO; move to state
61 // TODO; move to state
@@ -82,8 +84,8 @@ impl Editor {
82 match &mut self.state {
84 match &mut self.state {
83 Some(state) => {
85 Some(state) => {
84 let area = Rect::new(0, 0, self.size.0, self.size.1);
86 let area = Rect::new(0, 0, self.size.0, self.size.1);
85− let mut surface = Surface::empty(area);
86 let mut stdout = stdout();
87 let mut stdout = stdout();
88+ self.surface.reset(); // reset is faster than allocating new empty surface
88 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
90 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
89 let source_code = state.doc().to_string();
91 let source_code = state.doc().to_string();
@@ -146,7 +148,12 @@ impl Editor {
146 // which should really be the majority case
148 // which should really be the majority case
147 let grapheme = std::borrow::Cow::from(grapheme);
149 let grapheme = std::borrow::Cow::from(grapheme);
148 let width = grapheme_width(&grapheme) as u16;
150 let width = grapheme_width(&grapheme) as u16;
149− surface.set_string(offset + visual_x, line, grapheme, style);
151+ self.surface.set_string(
151 visual_x += width;
158 visual_x += width;
@@ -178,7 +185,7 @@ impl Editor {
178 // (x2 - x1 + 1) as u16,
185 // (x2 - x1 + 1) as u16,
179 // (y2 - y1 + 1) as u16,
186 // (y2 - y1 + 1) as u16,
181− // surface.set_style(area, select);
188+ // self.surface.set_style(area, select);
183 // // TODO: don't highlight next char in append mode
190 // // TODO: don't highlight next char in append mode
@@ -199,9 +206,9 @@ impl Editor {
200 self.terminal
207 self.terminal
201 .backend_mut()
208 .backend_mut()
202− .draw(self.surface.diff(&surface).into_iter());
209+ .draw(self.cache.diff(&self.surface).into_iter());
203 // swap the buffer
210 // swap the buffer
204− self.surface = surface;
211+ std::mem::swap(&mut self.surface, &mut self.cache);
206 // set cursor shape
213 // set cursor shape
207 match state.mode() {
214 match state.mode() {