From 929fa5474d5838d4c58dca8b0e576f5add49156b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Sat, 19 Sep 2020 11:56:56 +0900 Subject: [PATCH] Simple cursor scrolling. --- helix-term/src/editor.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/helix-term/src/editor.rs b/helix-term/src/editor.rs index e1288e07..397ce8df 100644 --- a/helix-term/src/editor.rs +++ b/helix-term/src/editor.rs @@ -277,7 +277,17 @@ impl Editor { // Handle key events let mut event = reader.next().await; match event { - // TODO: handle resize events + Some(Ok(Event::Resize(width, height))) => { + self.size = (width, height); + let area = Rect::new(0, 0, width, height); + self.surface = Surface::empty(area); + self.cache = Surface::empty(area); + + // TODO: simplistic ensure cursor in view for now + self.ensure_cursor_in_view(); + + self.render(); + } Some(Ok(Event::Key(KeyEvent { code: KeyCode::Char('q'), .. @@ -310,6 +320,9 @@ impl Editor { } => helix_core::commands::insert_char(state, '\n'), _ => (), // skip } + // TODO: simplistic ensure cursor in view for now + self.ensure_cursor_in_view(); + self.render(); } Mode::Normal => { @@ -318,6 +331,9 @@ impl Editor { // TODO: handle count other than 1 command(state, 1); + // TODO: simplistic ensure cursor in view for now + self.ensure_cursor_in_view(); + self.render(); } } @@ -333,6 +349,26 @@ impl Editor { } } + fn ensure_cursor_in_view(&mut self) { + if let Some(state) = &mut self.state { + let cursor = state.selection().cursor(); + let line = state.doc().char_to_line(cursor) as u16; + let document_end = self.first_line + self.size.1.saturating_sub(1) - 1; + + let padding = 5u16; + + // TODO: side scroll + + if line > document_end.saturating_sub(padding) { + // scroll down + self.first_line += line - (document_end.saturating_sub(padding)); + } else if line < self.first_line + padding { + // scroll up + self.first_line = line.saturating_sub(padding); + } + } + } + pub async fn run(&mut self) -> Result<(), Error> { enable_raw_mode()?;