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

Add Page Up and Page Down #1114

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions rust/core-lib/src/edit_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub(crate) enum ViewEvent {
SelectionForReplace,
SelectionIntoLines,
CollapseSelections,
PageUpDown(Movement),
}

/// Events that modify the buffer
Expand Down Expand Up @@ -264,6 +265,8 @@ impl From<EditNotification> for EventDomain {
PlayRecording { recording_name } => SpecialEvent::PlayRecording(recording_name).into(),
ClearRecording { recording_name } => SpecialEvent::ClearRecording(recording_name).into(),
CollapseSelections => ViewEvent::CollapseSelections.into(),
PageUp => ViewEvent::PageUpDown(Movement::UpPage).into(),
PageDown => ViewEvent::PageUpDown(Movement::DownPage).into(),
}
}
}
2 changes: 2 additions & 0 deletions rust/core-lib/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ pub enum EditNotification {
recording_name: String,
},
CollapseSelections,
PageUp,
PageDown,
}

/// The edit related requests.
Expand Down
20 changes: 20 additions & 0 deletions rust/core-lib/src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ impl View {
Replace { chars, preserve_case } => self.do_set_replace(chars, preserve_case),
SelectionForReplace => self.do_selection_for_replace(text),
SelectionIntoLines => self.do_split_selection_into_lines(text),
PageUpDown(movement) => self.page_up_down(movement, text),
}
}

Expand Down Expand Up @@ -322,6 +323,25 @@ impl View {
self.scroll_to = Some(end);
}

pub fn page_up_down(&mut self, movement: Movement, text: &Rope) {
let nm = self.first_line as usize;
let op = self.scroll_height() as usize;

let newPos = self.offset_of_line(text, nm + 2 * op);

let end = newPos;
let line = self.line_of_offset(text, end);
if line < self.first_line {
self.first_line = line;
} else if self.first_line + self.height <= line {
self.first_line = line - (self.height - 1);
}

let offset = self.sel_regions().last().unwrap().end;

self.scroll_to = Some(end);
}

/// Toggles a caret at the given offset.
pub fn toggle_sel(&mut self, text: &Rope, offset: usize) {
// We could probably reduce the cloning of selections by being clever.
Expand Down