Skip to content

Commit

Permalink
move nvim-scroll logic into scroll(), dont respect scrolloff
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDickie committed Aug 21, 2023
1 parent 949cfb9 commit 5a48de0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 66 deletions.
99 changes: 34 additions & 65 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,24 +1505,27 @@ fn switch_to_lowercase(cx: &mut Context) {
});
}

pub fn scroll_page_and_cursor(cx: &mut Context, offset: usize, direction: Direction) {
pub fn scroll(cx: &mut Context, offset: usize, direction: Direction, sync_cursor: bool) {
use Direction::*;
let config = cx.editor.config();
let cursor_pos = cx.editor.cursor().0.unwrap();
let (view, doc) = current!(cx.editor);

let range = doc.selection(view.id).primary();
let text = doc.text().slice(..);

let cursor = range.cursor(text);
let height = view.inner_height();
let scrolloff = config.scrolloff.min(height.saturating_sub(1) / 2);

let scrolloff = config.scrolloff.min(height.saturating_sub(1) / 2);
let offset = match direction {
Direction::Forward => offset as isize,
Direction::Backward => -(offset as isize),
Forward => offset as isize,
Backward => -(offset as isize),
};

let doc_text = doc.text().slice(..);
let viewport = view.inner_area(doc);
let text_fmt = doc.text_format(viewport.width, None);
let mut annotations = view.text_annotations(doc, None);

(view.offset.anchor, view.offset.vertical_offset) = char_idx_at_visual_offset(
doc_text,
view.offset.anchor,
Expand All @@ -1532,64 +1535,30 @@ pub fn scroll_page_and_cursor(cx: &mut Context, offset: usize, direction: Direct
&annotations,
);

let movement = match cx.editor.mode {
Mode::Select => Movement::Extend,
_ => Movement::Move,
};

let mut offset = offset.abs() as usize;
if cursor_pos.row <= scrolloff {
offset += scrolloff - cursor_pos.row + 1;
}

// TODO: When inline diagnostics gets merged- 1. move_vertically_visual removes
// line annotations/diagnostics so the cursor may jump further than the view.
// 2. If the cursor lands on a complete line of virtual text, the cursor will
// jump a different distance than the view.
let selection = doc.selection(view.id).clone().transform(|range| {
if sync_cursor {
let movement = match cx.editor.mode {
Mode::Select => Movement::Extend,
_ => Movement::Move,
};
// TODO: When inline diagnostics gets merged- 1. move_vertically_visual removes
// line annotations/diagnostics so the cursor may jump further than the view.
// 2. If the cursor lands on a complete line of virtual text, the cursor will
// jump a different distance than the view.
let selection = doc.selection(view.id).clone().transform(|range| {
move_vertically_visual(
doc_text,
range,
direction,
offset,
offset.abs() as usize,
movement,
&text_fmt,
&mut annotations,
)
});
doc.set_selection(view.id, selection);
}

pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
use Direction::*;
let config = cx.editor.config();
let (view, doc) = current!(cx.editor);

let range = doc.selection(view.id).primary();
let text = doc.text().slice(..);

let cursor = range.cursor(text);
let height = view.inner_height();

let scrolloff = config.scrolloff.min(height.saturating_sub(1) / 2);
let offset = match direction {
Forward => offset as isize,
Backward => -(offset as isize),
};

let doc_text = doc.text().slice(..);
let viewport = view.inner_area(doc);
let text_fmt = doc.text_format(viewport.width, None);
let annotations = view.text_annotations(doc, None);
(view.offset.anchor, view.offset.vertical_offset) = char_idx_at_visual_offset(
doc_text,
view.offset.anchor,
view.offset.vertical_offset as isize + offset,
0,
&text_fmt,
&annotations,
);

doc.set_selection(view.id, selection);
return;
}

let mut head;
match direction {
Forward => {
Expand Down Expand Up @@ -1640,49 +1609,49 @@ pub fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
fn page_up(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height();
scroll(cx, offset, Direction::Backward);
scroll(cx, offset, Direction::Backward, false);
}

fn page_down(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height();
scroll(cx, offset, Direction::Forward);
scroll(cx, offset, Direction::Forward, false);
}

fn half_page_up(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height() / 2;
scroll(cx, offset, Direction::Backward);
scroll(cx, offset, Direction::Backward, false);
}

fn half_page_down(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height() / 2;
scroll(cx, offset, Direction::Forward);
scroll(cx, offset, Direction::Forward, false);
}

fn page_cursor_up(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height();
scroll_page_and_cursor(cx, offset, Direction::Backward);
scroll(cx, offset, Direction::Backward, true);
}

fn page_cursor_down(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height();
scroll_page_and_cursor(cx, offset, Direction::Forward);
scroll(cx, offset, Direction::Forward, true);
}

fn page_cursor_half_up(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height() / 2;
scroll_page_and_cursor(cx, offset, Direction::Backward);
scroll(cx, offset, Direction::Backward, true);
}

fn page_cursor_half_down(cx: &mut Context) {
let view = view!(cx.editor);
let offset = view.inner_height() / 2;
scroll_page_and_cursor(cx, offset, Direction::Forward);
scroll(cx, offset, Direction::Forward, true);
}

#[allow(deprecated)]
Expand Down Expand Up @@ -5027,11 +4996,11 @@ fn align_view_middle(cx: &mut Context) {
}

fn scroll_up(cx: &mut Context) {
scroll(cx, cx.count(), Direction::Backward);
scroll(cx, cx.count(), Direction::Backward, false);
}

fn scroll_down(cx: &mut Context) {
scroll(cx, cx.count(), Direction::Forward);
scroll(cx, cx.count(), Direction::Forward, false);
}

fn goto_ts_object_impl(cx: &mut Context, object: &'static str, direction: Direction) {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ impl EditorView {
}

let offset = config.scroll_lines.unsigned_abs();
commands::scroll(cxt, offset, direction);
commands::scroll(cxt, offset, direction, false);

cxt.editor.tree.focus = current_view;
cxt.editor.ensure_cursor_in_view(current_view);
Expand Down

0 comments on commit 5a48de0

Please sign in to comment.