on master help sign in
various fixes
commit750610f0e7e2d27604983ffdcd8d6a447bc898b2 authorJan Hrastnik <jan.hrastnik2@gmail.com> dateWed 7 Oct 2020, 01:41 (6 years ago) parent88f9339
3 files changed+25−34
helix-term/src/editor.rs+2−2 helix-view/src/commands.rs+18−23 helix-view/src/view.rs+5−9
helix-term/src/editor.rs+2−2
@@ -87,7 +87,7 @@ impl Editor {
8787 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
8888 let source_code = view.state.doc().to_string();
8989
90− let last_line = view.last_line(viewport);
90+ let last_line = view.last_line();
9191
9292 let range = {
9393 // calculate viewport byte ranges
@@ -286,7 +286,7 @@ impl Editor {
286286 let pos = view.state.selection().cursor();
287287
288288 let pos = view
289− .screen_coords_at_pos(&view.state.doc().slice(..), pos, area)
289+ .screen_coords_at_pos(&view.state.doc().slice(..), pos)
290290 .expect("Cursor is out of bounds.");
291291
292292 execute!(
helix-view/src/commands.rs+18−23
@@ -13,6 +13,8 @@ use crate::view::View;
1313/// state (usually by creating and applying a transaction).
1414pub type Command = fn(view: &mut View, count: usize);
1515
16+const PADDING: u16 = 10;
17+
1618pub fn move_char_left(view: &mut View, count: usize) {
1719 // TODO: use a transaction
1820 let selection = view
@@ -127,7 +129,7 @@ pub fn move_file_start(view: &mut View, _count: usize) {
127129pub fn move_file_end(view: &mut View, _count: usize) {
128130 // TODO: use a transaction
129131 let text = &view.state.doc;
130− let last_line = text.line_to_char(text.len_lines().checked_sub(2).unwrap());
132+ let last_line = text.line_to_char(text.len_lines().saturating_sub(2));
131133 view.state.selection = Selection::single(last_line, last_line);
132134
133135 view.state.mode = Mode::Normal;
@@ -136,7 +138,7 @@ pub fn move_file_end(view: &mut View, _count: usize) {
136138pub fn check_cursor_in_view(view: &mut View) -> bool {
137139 let cursor = view.state.selection().cursor();
138140 let line = view.state.doc().char_to_line(cursor) as u16;
139− let document_end = view.first_line + view.size.1.saturating_sub(1) - 1;
141+ let document_end = view.first_line + view.size.1.saturating_sub(2);
140142 let padding = 5u16;
141143
142144 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 {
148150pub fn page_up(view: &mut View, _count: usize) {
149151 let text = &view.state.doc;
150152 view.first_line = view.first_line.saturating_sub(view.size.1);
153+ let pos = text.line_to_char(view.first_line as usize);
151154
152− view.state.selection = Selection::single(
153− text.line_to_char(view.first_line as usize),
154− text.line_to_char(view.first_line as usize),
155− );
155+ view.state.selection = Selection::single(pos, pos);
156156}
157157
158158pub fn page_down(view: &mut View, _count: usize) {
159159 let text = &view.state.doc;
160− view.first_line += view.size.1;
161−
162− view.state.selection = Selection::single(
163− text.line_to_char(view.first_line as usize),
164− text.line_to_char(view.first_line as usize),
165− );
160+ view.first_line += view.size.1 + PADDING;
161+ if view.first_line < view.state.doc().len_lines() as u16 {
162+ let pos = text.line_to_char(view.first_line as usize);
163+ view.state.selection = Selection::single(pos, pos);
164+ }
166165}
167166
168167pub fn half_page_up(view: &mut View, _count: usize) {
169168 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
170−
171169 if !check_cursor_in_view(view) {
172170 let text = &view.state.doc;
173− view.state.selection = Selection::single(
174− text.line_to_char(view.first_line as usize),
175− text.line_to_char(view.first_line as usize),
176− );
171+ let pos = text.line_to_char(view.first_line as usize);
172+ view.state.selection = Selection::single(pos, pos);
177173 }
178174}
179175
180176pub fn half_page_down(view: &mut View, _count: usize) {
181− view.first_line += view.size.1 / 2;
182−
177+ if view.first_line < view.state.doc().len_lines() as u16 - view.size.1 {
178+ view.first_line += view.size.1 / 2;
179+ }
183180 if !check_cursor_in_view(view) {
184181 let text = &view.state.doc;
185− view.state.selection = Selection::single(
186− text.line_to_char(view.first_line as usize),
187− text.line_to_char(view.first_line as usize),
188− );
182+ let pos = text.line_to_char(view.first_line as usize);
183+ view.state.selection = Selection::single(pos, pos);
189184 }
190185}
191186// avoid select by default by having a visual mode switch that makes movements into selects
helix-view/src/view.rs+5−9
@@ -34,7 +34,7 @@ impl View {
3434 pub fn ensure_cursor_in_view(&mut self) {
3535 let cursor = self.state.selection().cursor();
3636 let line = self.state.doc().char_to_line(cursor) as u16;
37− let document_end = self.first_line + self.size.1.saturating_sub(1) - 1;
37+ let document_end = self.first_line + self.size.1.saturating_sub(2);
3838
3939 let padding = 5u16;
4040
@@ -51,7 +51,8 @@ impl View {
5151
5252 /// Calculates the last visible line on screen
5353 #[inline]
54− pub fn last_line(&self, viewport: Rect) -> usize {
54+ pub fn last_line(&self) -> usize {
55+ let viewport = Rect::new(6, 0, self.size.0, self.size.1 - 1); // - 1 for statusline
5556 std::cmp::min(
5657 (self.first_line + viewport.height) as usize,
5758 self.state.doc().len_lines() - 1,
@@ -61,15 +62,10 @@ impl View {
6162 /// Translates a document position to an absolute position in the terminal.
6263 /// Returns a (line, col) position if the position is visible on screen.
6364 // TODO: Could return width as well for the character width at cursor.
64− pub fn screen_coords_at_pos(
65− &self,
66− text: &RopeSlice,
67− pos: usize,
68− viewport: Rect,
69− ) -> Option<Position> {
65+ pub fn screen_coords_at_pos(&self, text: &RopeSlice, pos: usize) -> Option<Position> {
7066 let line = text.char_to_line(pos);
7167
72− if line < self.first_line as usize || line > self.last_line(viewport) {
68+ if line < self.first_line as usize || line > self.last_line() {
7369 // Line is not visible on screen
7470 return None;
7571 }