From 6d52424303bf92a9abcfe8daa45cff145966f820 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 11 Aug 2021 13:53:38 +0900 Subject: fix: Adjust scroll offset/padding calculation to prevent wobble Fixes #324 --- helix-term/src/commands.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'helix-term/src') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e15332c8..7994c26e 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -110,10 +110,12 @@ fn align_view(doc: &Document, view: &mut View, align: Align) { .cursor(doc.text().slice(..)); let line = doc.text().char_to_line(pos); + let height = view.area.height.saturating_sub(1) as usize; // -1 for statusline + let relative = match align { - Align::Center => view.area.height as usize / 2, + Align::Center => height / 2, Align::Top => 0, - Align::Bottom => view.area.height as usize, + Align::Bottom => height, }; view.first_line = line.saturating_sub(relative); @@ -448,17 +450,21 @@ fn goto_first_nonwhitespace(cx: &mut Context) { fn goto_window(cx: &mut Context, align: Align) { let (view, doc) = current!(cx.editor); + let height = view.area.height.saturating_sub(1) as usize; // -1 for statusline + // - 1 so we have at least one gap in the middle. + // a height of 6 with padding of 3 on each side will keep shifting the view back and forth + // as we type let scrolloff = cx .editor .config .scrolloff - .min(view.area.height as usize / 2); // TODO: user pref + .min(height.saturating_sub(1) / 2); let last_line = view.last_line(doc); let line = match align { Align::Top => (view.first_line + scrolloff), - Align::Center => (view.first_line + (view.area.height as usize / 2)), + Align::Center => (view.first_line + (height / 2)), Align::Bottom => last_line.saturating_sub(scrolloff), } .min(last_line.saturating_sub(scrolloff)); @@ -894,7 +900,7 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) { .editor .config .scrolloff - .min(view.area.height as usize / 2); // TODO: user pref + .min(view.area.height as usize / 2); view.first_line = match direction { Forward => view.first_line + offset, -- cgit v1.2.3-70-g09d2