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

Goto mode implementation #3

Merged
merged 9 commits into from
Oct 7, 2020
Merged

Goto mode implementation #3

merged 9 commits into from
Oct 7, 2020

Conversation

archseer
Copy link
Member

@archseer archseer commented Oct 6, 2020

No description provided.

pub fn move_file_end(view: &mut View, _count: usize) {
// TODO: use a transaction
let text = &view.state.doc;
let last_line = text.line_to_char(text.len_lines().checked_sub(2).unwrap());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use saturating_sub

pub fn check_cursor_in_view(view: &mut View) -> bool {
let cursor = view.state.selection().cursor();
let line = view.state.doc().char_to_line(cursor) as u16;
let document_end = view.first_line + view.size.1.saturating_sub(1) - 1;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saturating_sub(2)

view.first_line = view.first_line.saturating_sub(view.size.1);

view.state.selection = Selection::single(
text.line_to_char(view.first_line as usize),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calculate line_to_char in all of these methods once by assigning it to let pos

}

pub fn half_page_down(view: &mut View, _count: usize) {
view.first_line += view.size.1 / 2;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to make sure first_line < text.len_lines(), pressing half_page_down/page_down at document end will crash. Use a similar approach last_line() uses to clamp the value.

if !check_cursor_in_view(view) {
let text = &view.state.doc;
view.state.selection = Selection::single(
text.line_to_char(view.first_line as usize),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When scrolling down, you want the cursor to stick to the top edge (first_line + padding), when scrolling up you want it to be at the bottom of the screen last_line() - padding.

Make padding a const PADDING: u16 = 5 for now

@archseer archseer merged commit 6848702 into master Oct 7, 2020
@archseer archseer deleted the goto-implementation branch October 7, 2020 05:06
humblehacker added a commit to humblehacker/helix that referenced this pull request Jul 29, 2023
JeftavanderHorst pushed a commit to JeftavanderHorst/helix that referenced this pull request Mar 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants