on master help sign in
Simple cursor scrolling.
929fa54Blaž Hrastnik
helix-term/src/editor.rs+37−1
@@ -277,7 +277,17 @@ impl Editor {
277 // Handle key events
277 // Handle key events
278 let mut event = reader.next().await;
278 let mut event = reader.next().await;
279 match event {
279 match event {
280− // TODO: handle resize events
280+ Some(Ok(Event::Resize(width, height))) => {
281+ self.size = (width, height);
282+ let area = Rect::new(0, 0, width, height);
283+ self.surface = Surface::empty(area);
284+ self.cache = Surface::empty(area);
285+
286+ // TODO: simplistic ensure cursor in view for now
287+ self.ensure_cursor_in_view();
288+
289+ self.render();
290+ }
281 Some(Ok(Event::Key(KeyEvent {
291 Some(Ok(Event::Key(KeyEvent {
282 code: KeyCode::Char('q'),
292 code: KeyCode::Char('q'),
283 ..
293 ..
@@ -310,6 +320,9 @@ impl Editor {
310 } => helix_core::commands::insert_char(state, '\n'),
320 } => helix_core::commands::insert_char(state, '\n'),
311 _ => (), // skip
321 _ => (), // skip
312 }
322 }
323+ // TODO: simplistic ensure cursor in view for now
324+ self.ensure_cursor_in_view();
325+
313 self.render();
326 self.render();
314 }
327 }
315 Mode::Normal => {
328 Mode::Normal => {
@@ -318,6 +331,9 @@ impl Editor {
318 // TODO: handle count other than 1
331 // TODO: handle count other than 1
319 command(state, 1);
332 command(state, 1);
320
333
334+ // TODO: simplistic ensure cursor in view for now
335+ self.ensure_cursor_in_view();
336+
321 self.render();
337 self.render();
322 }
338 }
323 }
339 }
@@ -333,6 +349,26 @@ impl Editor {
333 }
349 }
334 }
350 }
335
351
352+ fn ensure_cursor_in_view(&mut self) {
353+ if let Some(state) = &mut self.state {
354+ let cursor = state.selection().cursor();
355+ let line = state.doc().char_to_line(cursor) as u16;
356+ let document_end = self.first_line + self.size.1.saturating_sub(1) - 1;
357+
358+ let padding = 5u16;
359+
360+ // TODO: side scroll
361+
362+ if line > document_end.saturating_sub(padding) {
363+ // scroll down
364+ self.first_line += line - (document_end.saturating_sub(padding));
365+ } else if line < self.first_line + padding {
366+ // scroll up
367+ self.first_line = line.saturating_sub(padding);
368+ }
369+ }
370+ }
371+
336 pub async fn run(&mut self) -> Result<(), Error> {
372 pub async fn run(&mut self) -> Result<(), Error> {
337 enable_raw_mode()?;
373 enable_raw_mode()?;
338
374