diff options
author | Blaž Hrastnik | 2020-09-19 02:56:56 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2020-09-19 02:58:08 +0000 |
commit | 929fa5474d5838d4c58dca8b0e576f5add49156b (patch) | |
tree | 2a85d8172f17391dce91e0b9df1ce7197be39a71 /helix-term | |
parent | b120515613ba24aff224d093b28d85049c774cad (diff) |
Simple cursor scrolling.
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/editor.rs | 38 |
1 files changed, 37 insertions, 1 deletions
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()?; |