on master help sign in
various fixes
750610fJan Hrastnik
helix-term/src/editor.rs+2−2
@@ -87,7 +87,7 @@ impl Editor {
87 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
87 // TODO: inefficient, should feed chunks.iter() to tree_sitter.parse_with(|offset, pos|)
88 let source_code = view.state.doc().to_string();
88 let source_code = view.state.doc().to_string();
89
89
90− let last_line = view.last_line(viewport);
90+ let last_line = view.last_line();
91
91
92 let range = {
92 let range = {
93 // calculate viewport byte ranges
93 // calculate viewport byte ranges
@@ -286,7 +286,7 @@ impl Editor {
286 let pos = view.state.selection().cursor();
286 let pos = view.state.selection().cursor();
287
287
288 let pos = view
288 let pos = view
289− .screen_coords_at_pos(&view.state.doc().slice(..), pos, area)
289+ .screen_coords_at_pos(&view.state.doc().slice(..), pos)
290 .expect("Cursor is out of bounds.");
290 .expect("Cursor is out of bounds.");
291
291
292 execute!(
292 execute!(
helix-view/src/commands.rs+18−23
@@ -13,6 +13,8 @@ use crate::view::View;
13/// state (usually by creating and applying a transaction).
13/// state (usually by creating and applying a transaction).
14pub type Command = fn(view: &mut View, count: usize);
14pub type Command = fn(view: &mut View, count: usize);
15
15
16+const PADDING: u16 = 10;
17+
16pub fn move_char_left(view: &mut View, count: usize) {
18pub fn move_char_left(view: &mut View, count: usize) {
17 // TODO: use a transaction
19 // TODO: use a transaction
18 let selection = view
20 let selection = view
@@ -127,7 +129,7 @@ pub fn move_file_start(view: &mut View, _count: usize) {
127pub fn move_file_end(view: &mut View, _count: usize) {
129pub fn move_file_end(view: &mut View, _count: usize) {
128 // TODO: use a transaction
130 // TODO: use a transaction
129 let text = &view.state.doc;
131 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));
131 view.state.selection = Selection::single(last_line, last_line);
133 view.state.selection = Selection::single(last_line, last_line);
132
134
133 view.state.mode = Mode::Normal;
135 view.state.mode = Mode::Normal;
@@ -136,7 +138,7 @@ pub fn move_file_end(view: &mut View, _count: usize) {
136pub fn check_cursor_in_view(view: &mut View) -> bool {
138pub fn check_cursor_in_view(view: &mut View) -> bool {
137 let cursor = view.state.selection().cursor();
139 let cursor = view.state.selection().cursor();
138 let line = view.state.doc().char_to_line(cursor) as u16;
140 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);
140 let padding = 5u16;
142 let padding = 5u16;
141
143
142 if (line > document_end.saturating_sub(padding)) | (line < view.first_line + padding) {
144 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 {
148pub fn page_up(view: &mut View, _count: usize) {
150pub fn page_up(view: &mut View, _count: usize) {
149 let text = &view.state.doc;
151 let text = &view.state.doc;
150 view.first_line = view.first_line.saturating_sub(view.size.1);
152 view.first_line = view.first_line.saturating_sub(view.size.1);
153+ let pos = text.line_to_char(view.first_line as usize);
151
154
152− view.state.selection = Selection::single(
155+ view.state.selection = Selection::single(pos, pos);
153− text.line_to_char(view.first_line as usize),
154− text.line_to_char(view.first_line as usize),
155− );
156}
156}
157
157
158pub fn page_down(view: &mut View, _count: usize) {
158pub fn page_down(view: &mut View, _count: usize) {
159 let text = &view.state.doc;
159 let text = &view.state.doc;
160− view.first_line += view.size.1;
160+ view.first_line += view.size.1 + PADDING;
161−
161+ if view.first_line < view.state.doc().len_lines() as u16 {
162+ let pos = text.line_to_char(view.first_line as usize);
162− view.state.selection = Selection::single(
163+ view.state.selection = Selection::single(pos, pos);
163− text.line_to_char(view.first_line as usize),
164+ }
164− text.line_to_char(view.first_line as usize),
165− );
166}
165}
167
166
168pub fn half_page_up(view: &mut View, _count: usize) {
167pub fn half_page_up(view: &mut View, _count: usize) {
169 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
168 view.first_line = view.first_line.saturating_sub(view.size.1 / 2);
170−
171 if !check_cursor_in_view(view) {
169 if !check_cursor_in_view(view) {
172 let text = &view.state.doc;
170 let text = &view.state.doc;
171+ let pos = text.line_to_char(view.first_line as usize);
173− view.state.selection = Selection::single(
172+ view.state.selection = Selection::single(pos, pos);
174− text.line_to_char(view.first_line as usize),
175− text.line_to_char(view.first_line as usize),
176− );
177 }
173 }
178}
174}
179
175
180pub fn half_page_down(view: &mut View, _count: usize) {
176pub fn half_page_down(view: &mut View, _count: usize) {
177+ if view.first_line < view.state.doc().len_lines() as u16 - view.size.1 {
181− view.first_line += view.size.1 / 2;
178+ view.first_line += view.size.1 / 2;
182−
179+ }
183 if !check_cursor_in_view(view) {
180 if !check_cursor_in_view(view) {
184 let text = &view.state.doc;
181 let text = &view.state.doc;
182+ let pos = text.line_to_char(view.first_line as usize);
185− view.state.selection = Selection::single(
183+ view.state.selection = Selection::single(pos, pos);
186− text.line_to_char(view.first_line as usize),
187− text.line_to_char(view.first_line as usize),
188− );
189 }
184 }
190}
185}
191// avoid select by default by having a visual mode switch that makes movements into selects
186// 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 {
34 pub fn ensure_cursor_in_view(&mut self) {
34 pub fn ensure_cursor_in_view(&mut self) {
35 let cursor = self.state.selection().cursor();
35 let cursor = self.state.selection().cursor();
36 let line = self.state.doc().char_to_line(cursor) as u16;
36 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);
38
38
39 let padding = 5u16;
39 let padding = 5u16;
40
40
@@ -51,7 +51,8 @@ impl View {
51
51
52 /// Calculates the last visible line on screen
52 /// Calculates the last visible line on screen
53 #[inline]
53 #[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
55 std::cmp::min(
56 std::cmp::min(
56 (self.first_line + viewport.height) as usize,
57 (self.first_line + viewport.height) as usize,
57 self.state.doc().len_lines() - 1,
58 self.state.doc().len_lines() - 1,
@@ -61,15 +62,10 @@ impl View {
61 /// Translates a document position to an absolute position in the terminal.
62 /// Translates a document position to an absolute position in the terminal.
62 /// Returns a (line, col) position if the position is visible on screen.
63 /// Returns a (line, col) position if the position is visible on screen.
63 // TODO: Could return width as well for the character width at cursor.
64 // TODO: Could return width as well for the character width at cursor.
64− pub fn screen_coords_at_pos(
65+ pub fn screen_coords_at_pos(&self, text: &RopeSlice, pos: usize) -> Option<Position> {
65− &self,
66− text: &RopeSlice,
67− pos: usize,
68− viewport: Rect,
69− ) -> Option<Position> {
70 let line = text.char_to_line(pos);
66 let line = text.char_to_line(pos);
71
67
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() {
73 // Line is not visible on screen
69 // Line is not visible on screen
74 return None;
70 return None;
75 }
71 }