1
0
Fork 0
mirror of https://github.com/helix-editor/helix synced 2024-05-20 15:56:05 +02:00

Simple cursor scrolling.

This commit is contained in:
Blaž Hrastnik 2020-09-19 11:56:56 +09:00
parent b120515613
commit 929fa5474d

View File

@ -277,7 +277,17 @@ pub async fn event_loop(&mut self) {
// 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 @@ pub async fn event_loop(&mut self) {
} => 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 @@ pub async fn event_loop(&mut self) {
// 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 @@ pub async fn event_loop(&mut self) {
}
}
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()?;