aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Hrastnik2020-10-06 23:41:09 +0000
committerJan Hrastnik2020-10-06 23:41:09 +0000
commit750610f0e7e2d27604983ffdcd8d6a447bc898b2 (patch)
tree574e857cdb97ac4bb5cd4545b81253a6e5b09ab0
parent88f93399fd4a451f529693269e4154421c8a6d06 (diff)
various fixes
-rw-r--r--helix-term/src/editor.rs4
-rw-r--r--helix-view/src/commands.rs41
-rw-r--r--helix-view/src/view.rs14
3 files changed, 25 insertions, 34 deletions
diff --git a/helix-term/src/editor.rs b/helix-term/src/editor.rs
index 266c0198..acb742b2 100644
--- a/helix-term/src/editor.rs
+++ b/helix-term/src/editor.rs
@@ -87,7 +87,7 @@ impl Editor {
// TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
let source_code = view.state.doc().to_string();
- let last_line = view.last_line(viewport);
+ let last_line = view.last_line();
let range = {
// calculate viewport byte ranges
@@ -286,7 +286,7 @@ impl Editor {
let pos = view.state.selection().cursor();
let pos = view
- .screen_coords_at_pos(&view.state.doc().slice(..), pos, area)
+ .screen_coords_at_pos(&view.state.doc().slice(..), pos)
.expect("Cursor is out of bounds.");
execute!(
diff --git a/helix-view/src/commands.rs b/helix-view/src/commands.rs
index 0b985dc6..b2223d48 100644
--- a/helix-view/src/commands.rs
+++ b/helix-view/src/commands.rs
@@ -13,6 +13,8 @@ use crate::view::View;
/// state (usually by creating and applying a transaction).
pub type Command = fn(view: &mut View, count: usize);
+const PADDING: u16 = 10;
+
pub fn move_char_left(view: &mut View, count: usize) {
// TODO: use a transaction
let selection = view
@@ -127,7 +129,7 @@ pub fn move_file_start(view: &mut View, _count: usize) {
pub fn move_file_end(view: &mut View, _count: usize) {
// TODO: use a transaction
let text = &view.state.doc;
- let last_line = text.line_to_char(text.len_lines().checked_sub(2).unwrap());
+ let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
view.state.selection = Selection::single(last_line, last_line);
view.state.mode = Mode::Normal;
@@ -136,7 +138,7 @@ 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) - 1;
+ let document_end = view.first_line + view.size.1.saturating_sub(2);
let padding = 5u16;
if (line > document_end.saturating_sub(padding)) | (line < view.first_line + padding) {
@@ -148,44 +150,37 @@ 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.first_line as usize);
- view.state.selection = Selection::single(
- text.line_to_char(view.first_line as usize),
- text.line_to_char(view.first_line 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;
-
- view.state.selection = Selection::single(
- text.line_to_char(view.first_line as usize),
- text.line_to_char(view.first_line as usize),
- );
+ view.first_line += view.size.1 + PADDING;
+ if view.first_line < view.state.doc().len_lines() as u16 {
+ 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);
-
if !check_cursor_in_view(view) {
let text = &view.state.doc;
- view.state.selection = Selection::single(
- text.line_to_char(view.first_line as usize),
- text.line_to_char(view.first_line as usize),
- );
+ let pos = text.line_to_char(view.first_line as usize);
+ view.state.selection = Selection::single(pos, pos);
}
}
pub fn half_page_down(view: &mut View, _count: usize) {
- view.first_line += view.size.1 / 2;
-
+ if view.first_line < view.state.doc().len_lines() as u16 - view.size.1 {
+ view.first_line += view.size.1 / 2;
+ }
if !check_cursor_in_view(view) {
let text = &view.state.doc;
- view.state.selection = Selection::single(
- text.line_to_char(view.first_line as usize),
- text.line_to_char(view.first_line as usize),
- );
+ let pos = text.line_to_char(view.first_line as usize);
+ view.state.selection = Selection::single(pos, pos);
}
}
// avoid select by default by having a visual mode switch that makes movements into selects
diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs
index 09cd4c65..0e6705f0 100644
--- a/helix-view/src/view.rs
+++ b/helix-view/src/view.rs
@@ -34,7 +34,7 @@ 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) - 1;
+ let document_end = self.first_line + self.size.1.saturating_sub(2);
let padding = 5u16;
@@ -51,7 +51,8 @@ impl View {
/// Calculates the last visible line on screen
#[inline]
- pub fn last_line(&self, viewport: Rect) -> usize {
+ 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.state.doc().len_lines() - 1,
@@ -61,15 +62,10 @@ impl View {
/// Translates a document position to an absolute position in the terminal.
/// Returns a (line, col) position if the position is visible on screen.
// TODO: Could return width as well for the character width at cursor.
- pub fn screen_coords_at_pos(
- &self,
- text: &RopeSlice,
- pos: usize,
- viewport: Rect,
- ) -> Option<Position> {
+ pub fn screen_coords_at_pos(&self, text: &RopeSlice, pos: usize) -> Option<Position> {
let line = text.char_to_line(pos);
- if line < self.first_line as usize || line > self.last_line(viewport) {
+ if line < self.first_line as usize || line > self.last_line() {
// Line is not visible on screen
return None;
}