Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scroll cursor and page together (neovim-like scrolling) #8015

Merged
merged 13 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions helix-core/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ pub fn move_vertically_visual(
if !text_fmt.soft_wrap {
return move_vertically(slice, range, dir, count, behaviour, text_fmt, annotations);
}
annotations.clear_line_annotations();
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
let pos = range.cursor(slice);

// Compute the current position's 2d coordinates.
Expand Down Expand Up @@ -112,7 +111,6 @@ pub fn move_vertically(
text_fmt: &TextFormat,
annotations: &mut TextAnnotations,
) -> Range {
annotations.clear_line_annotations();
let pos = range.cursor(slice);
let line_idx = slice.char_to_line(pos);
let line_start = slice.line_to_char(line_idx);
Expand Down
80 changes: 80 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ impl MappableCommand {
page_down, "Move page down",
half_page_up, "Move half page up",
half_page_down, "Move half page down",
page_cursor_up, "Move page and cursor up",
page_cursor_down, "Move page and cursor down",
page_cursor_half_up, "Move page and cursor half up",
page_cursor_half_down, "Move page and cursor half down",
select_all, "Select whole document",
select_regex, "Select all regex matches inside selections",
split_selection, "Split selections on regex matches",
Expand Down Expand Up @@ -595,6 +599,7 @@ fn move_impl(cx: &mut Context, move_fn: MoveFn, dir: Direction, behaviour: Movem
let text = doc.text().slice(..);
let text_fmt = doc.text_format(view.inner_area(doc).width, None);
let mut annotations = view.text_annotations(doc, None);
annotations.clear_line_annotations();

let selection = doc.selection(view.id).clone().transform(|range| {
move_fn(
Expand Down Expand Up @@ -1501,6 +1506,57 @@ fn switch_to_lowercase(cx: &mut Context) {
});
}

pub fn scroll_page_and_cursor(cx: &mut Context, offset: usize, direction: Direction) {
let config = cx.editor.config();
let cursor_pos = cx.editor.cursor().0.unwrap();
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
let (view, doc) = current!(cx.editor);

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

let offset = match direction {
Direction::Forward => offset as isize,
Direction::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,
view.offset.vertical_offset as isize + offset,
0,
&text_fmt,
&annotations,
);

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

let mut offset = offset.abs() as usize;
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
if cursor_pos.row <= scrolloff {
offset += scrolloff - cursor_pos.row + 1;
}

let selection = doc.selection(view.id).clone().transform(|range| {
move_vertically_visual(
pascalkuthe marked this conversation as resolved.
Show resolved Hide resolved
doc_text,
range,
direction,
offset,
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();
Expand Down Expand Up @@ -1602,6 +1658,30 @@ fn half_page_down(cx: &mut Context) {
scroll(cx, offset, Direction::Forward);
}

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);
}

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);
}

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);
}

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);
}

#[allow(deprecated)]
// currently uses the deprecated `visual_coords_at_pos`/`pos_at_visual_coords` functions
// as this function ignores softwrapping (and virtual text) and instead only cares
Expand Down
12 changes: 6 additions & 6 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"esc" => normal_mode,
"C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down,
"C-u" => half_page_up,
"C-d" => half_page_down,
"C-u" => page_cursor_half_up,
"C-d" => page_cursor_half_down,

"C-w" => { "Window"
"C-w" | "w" => rotate_view,
Expand Down Expand Up @@ -287,8 +287,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"j" | "down" => scroll_down,
"C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down,
"C-u" | "backspace" => half_page_up,
"C-d" | "space" => half_page_down,
"C-u" | "backspace" => page_cursor_half_up,
"C-d" | "space" => page_cursor_half_down,

"/" => search,
"?" => rsearch,
Expand All @@ -304,8 +304,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"j" | "down" => scroll_down,
"C-b" | "pageup" => page_up,
"C-f" | "pagedown" => page_down,
"C-u" | "backspace" => half_page_up,
"C-d" | "space" => half_page_down,
"C-u" | "backspace" => page_cursor_half_up,
"C-d" | "space" => page_cursor_half_down,

"/" => search,
"?" => rsearch,
Expand Down
Loading