on master help sign in
Adjust scroll() to match kakoune: only scroll the view if cursor in bounds.
commit180521fefec8923bd4082c4a1e9beca599a44d07 authorBlaž Hrastnik <blaz@mxxn.io> dateWed 14 Apr 2021, 14:27 (6 years ago) parentdfb1ae2
2 files changed+22−14
helix-term/src/commands.rs+11−5 helix-view/src/view.rs+11−9
helix-term/src/commands.rs+11−5
@@ -450,10 +450,11 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
450450 return;
451451 }
452452
453− let scrolloff = PADDING; // min(user pref, half win width/height)
453+ let scrolloff = PADDING.min(view.area.height as usize / 2); // TODO: user pref
454454
455455 // cursor visual offset
456− let cursor_off = cursor.row - view.first_line;
456+ // TODO: only if dragging via mouse?
457+ // let cursor_off = cursor.row - view.first_line;
457458
458459 view.first_line = match direction {
459460 Forward => view.first_line + offset,
@@ -461,14 +462,19 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
461462 }
462463 .min(doc_last_line);
463464
465+ // recalculate last line
466+ let last_line = view.last_line(doc);
467+
464468 // clamp into viewport
465− let line = (view.first_line + cursor_off)
466− .max(view.first_line + scrolloff)
467− .min(last_line.saturating_sub(scrolloff));
469+ let line = cursor.row.clamp(
470+ view.first_line + scrolloff,
471+ last_line.saturating_sub(scrolloff),
472+ );
468473
469474 let text = doc.text().slice(..);
470475 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
471476
477+ // TODO: only manipulate main selection
472478 doc.set_selection(view.id, Selection::point(pos));
473479}
474480
helix-view/src/view.rs+11−9
@@ -81,26 +81,28 @@ impl View {
8181 let pos = coords_at_pos(doc.text().slice(..), cursor);
8282 let line = pos.row;
8383 let col = pos.col;
84− let last_line = self.first_line + (self.area.height as usize).saturating_sub(2);
84+ let last_line = self.last_line(doc);
85+
86+ let scrolloff = PADDING.min(self.area.height as usize / 2); // TODO: user pref
8587
8688 // TODO: not ideal
8789 const OFFSET: usize = 7; // 1 diagnostic + 5 linenr + 1 gutter
8890 let last_col = self.first_col + (self.area.width as usize - OFFSET);
8991
90− if line > last_line.saturating_sub(PADDING) {
92+ if line > last_line.saturating_sub(scrolloff) {
9193 // scroll down
92− self.first_line += line - (last_line.saturating_sub(PADDING));
93− } else if line < self.first_line + PADDING {
94+ self.first_line += line - (last_line.saturating_sub(scrolloff));
95+ } else if line < self.first_line + scrolloff {
9496 // scroll up
95− self.first_line = line.saturating_sub(PADDING);
97+ self.first_line = line.saturating_sub(scrolloff);
9698 }
9799
98− if col > last_col.saturating_sub(PADDING) {
100+ if col > last_col.saturating_sub(scrolloff) {
99101 // scroll right
100− self.first_col += col - (last_col.saturating_sub(PADDING));
101− } else if col < self.first_col + PADDING {
102+ self.first_col += col - (last_col.saturating_sub(scrolloff));
103+ } else if col < self.first_col + scrolloff {
102104 // scroll left
103− self.first_col = col.saturating_sub(PADDING);
105+ self.first_col = col.saturating_sub(scrolloff);
104106 }
105107 }
106108