diff options
author | Blaž Hrastnik | 2020-10-06 08:32:30 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2020-10-07 05:03:38 +0000 |
commit | 7f07e6676801be72e5a58b5612893c7d16f94a64 (patch) | |
tree | 18cc823d2a5dfd0b09b5802f6eb11c978080df36 /helix-view/src | |
parent | 5392b4826856e5e50653587ed5a13aef86805401 (diff) |
Cleanup: track first_line as usize.
Diffstat (limited to 'helix-view/src')
-rw-r--r-- | helix-view/src/commands.rs | 28 | ||||
-rw-r--r-- | helix-view/src/view.rs | 10 |
2 files changed, 19 insertions, 19 deletions
diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs index 9ebdb4c4..19853c37 100644 --- a/helix-view/src/commands.rs +++ b/helix-view/src/commands.rs @@ -13,7 +13,7 @@ use crate::view::View; /// state (usually by creating and applying a transaction). pub type Command = fn(view: &mut View, count: usize); -const PADDING: u16 = 5; +const PADDING: usize = 5; pub fn move_char_left(view: &mut View, count: usize) { // TODO: use a transaction @@ -137,8 +137,8 @@ pub fn move_file_end(view: &mut View, _count: usize) { pub fn check_cursor_in_view(view: &mut View) -> bool { let cursor = view.state.selection().cursor(); - let line = view.state.doc().char_to_line(cursor) as u16; - let document_end = view.first_line + view.size.1.saturating_sub(1); + let line = view.state.doc().char_to_line(cursor); + let document_end = view.first_line + view.size.1.saturating_sub(1) as usize; if (line > document_end.saturating_sub(PADDING)) | (line < view.first_line + PADDING) { return false; @@ -147,28 +147,28 @@ pub fn check_cursor_in_view(view: &mut View) -> bool { } pub fn page_up(view: &mut View, _count: usize) { - let text = &view.state.doc; - view.first_line = view.first_line.saturating_sub(view.size.1); - let pos = text.line_to_char(view.last_line() as usize); + view.first_line = view.first_line.saturating_sub(view.size.1 as usize); if !check_cursor_in_view(view) { - let text = &view.state.doc; + let text = view.state.doc(); let pos = text.line_to_char(view.last_line().saturating_sub(PADDING as usize)); view.state.selection = Selection::single(pos, pos); } } pub fn page_down(view: &mut View, _count: usize) { - let text = &view.state.doc; - view.first_line += view.size.1 + PADDING; - if view.first_line < view.state.doc().len_lines() as u16 { + view.first_line += view.size.1 as usize + PADDING; + + if view.first_line < view.state.doc().len_lines() { + let text = view.state.doc(); let pos = text.line_to_char(view.first_line as usize); view.state.selection = Selection::single(pos, pos); } } pub fn half_page_up(view: &mut View, _count: usize) { - view.first_line = view.first_line.saturating_sub(view.size.1 / 2); + view.first_line = view.first_line.saturating_sub(view.size.1 as usize / 2); + if !check_cursor_in_view(view) { let text = &view.state.doc; let pos = text.line_to_char(view.last_line() - PADDING as usize); @@ -178,11 +178,11 @@ pub fn half_page_up(view: &mut View, _count: usize) { pub fn half_page_down(view: &mut View, _count: usize) { let lines = view.state.doc().len_lines(); - if view.first_line < lines.saturating_sub(view.size.1 as usize) as u16 { - view.first_line += view.size.1 / 2; + if view.first_line < lines.saturating_sub(view.size.1 as usize) { + view.first_line += view.size.1 as usize / 2; } if !check_cursor_in_view(view) { - let text = &view.state.doc; + let text = view.state.doc(); let pos = text.line_to_char(view.first_line as usize); view.state.selection = Selection::single(pos, pos); } diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index 6ef5ee6f..887c45a2 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -11,7 +11,7 @@ use tui::layout::Rect; pub struct View { pub state: State, - pub first_line: u16, + pub first_line: usize, pub size: (u16, u16), pub theme: Theme, // TODO: share one instance } @@ -33,10 +33,10 @@ impl View { pub fn ensure_cursor_in_view(&mut self) { let cursor = self.state.selection().cursor(); - let line = self.state.doc().char_to_line(cursor) as u16; - let document_end = self.first_line + self.size.1.saturating_sub(1); + let line = self.state.doc().char_to_line(cursor); + let document_end = self.first_line + (self.size.1 as usize).saturating_sub(1); - let padding = 5u16; + let padding = 5usize; // TODO: side scroll @@ -54,7 +54,7 @@ impl View { pub fn last_line(&self) -> usize { let viewport = Rect::new(6, 0, self.size.0, self.size.1 - 1); // - 1 for statusline std::cmp::min( - (self.first_line + viewport.height) as usize, + self.first_line + (viewport.height as usize), self.state.doc().len_lines() - 1, ) } |