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

implementing picker preview scrolling #4189

Closed
wants to merge 18 commits into from
Closed
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ impl<T: Item + 'static> Component for FilePicker<T> {
let inner = inner.inner(&margin);
block.render(preview_area, surface);

let mut preview_scroll_offset = self.picker.preview_scroll_offset;

if let Some((path, range)) = self.current_file(cx.editor) {
let preview = self.get_preview(&path, cx.editor);
let doc = match preview.document() {
Expand All @@ -226,7 +228,8 @@ impl<T: Item + 'static> Component for FilePicker<T> {
})
.unwrap_or(0);

let offset = Position::new(first_line, 0);
preview_scroll_offset = preview_scroll_offset.min(doc.text().len_lines());
Copy link
Member

Choose a reason for hiding this comment

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

Rather than in the render loop, shouldn't we do this in move_preview_by?

Copy link
Contributor Author

@Manosmer Manosmer Oct 11, 2022

Choose a reason for hiding this comment

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

move_preview_by() is implemented in Picker and I needed the len_lines() of the current file in order to limit the scroll. I could transfer that functionality to a FilePicker::move_preview_by() function and handle the preview scroll events in FilePicker::handle_event(), instead of doing all that in Picker

let offset = Position::new(first_line + preview_scroll_offset, 0);

let highlights =
EditorView::doc_syntax_highlights(doc, offset, area.height, &cx.editor.theme);
Expand Down Expand Up @@ -258,6 +261,9 @@ impl<T: Item + 'static> Component for FilePicker<T> {
);
}
}

// Limits the preview scroll between 0 and doc's len_lines() in case it moves past the line number
self.picker.preview_scroll_offset = preview_scroll_offset;
}

fn handle_event(&mut self, event: &Event, ctx: &mut Context) -> EventResult {
Expand Down Expand Up @@ -297,6 +303,7 @@ pub struct Picker<T: Item> {
// pattern: String,
prompt: Prompt,
previous_pattern: String,
preview_scroll_offset: usize,
/// Whether to truncate the start (default true)
pub truncate_start: bool,
/// Whether to show the preview panel (default true)
Expand Down Expand Up @@ -327,6 +334,7 @@ impl<T: Item> Picker<T> {
cursor: 0,
prompt,
previous_pattern: String::new(),
preview_scroll_offset: 0,
Copy link
Member

Choose a reason for hiding this comment

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

Offset should reset on move_by

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are absolutely right, my bad

truncate_start: true,
show_preview: true,
callback_fn: Box::new(callback_fn),
Expand Down Expand Up @@ -434,6 +442,18 @@ impl<T: Item> Picker<T> {
}
}

// Move the picker file preview by a number of lines, either down (`Forward`) or up (`Backward`)
pub fn move_preview_by(&mut self, amount: usize, direction: Direction) {
self.preview_scroll_offset = match direction {
Direction::Forward => {
self.preview_scroll_offset.saturating_add(amount)
},
Direction::Backward => {
self.preview_scroll_offset.saturating_sub(amount)
},
};
}

/// Move the cursor down by exactly one page. After the last page comes the first page.
pub fn page_up(&mut self) {
self.move_by(self.completion_height as usize, Direction::Backward);
Expand Down Expand Up @@ -509,6 +529,12 @@ impl<T: Item + 'static> Component for Picker<T> {
shift!(Tab) | key!(Up) | ctrl!('p') => {
self.move_by(1, Direction::Backward);
}
shift!(Up) => {
self.move_preview_by(cx.editor.config().scroll_lines.unsigned_abs(), Direction::Backward);
}
shift!(Down) => {
self.move_preview_by(cx.editor.config().scroll_lines.unsigned_abs(), Direction::Forward);
}
key!(Tab) | key!(Down) | ctrl!('n') => {
self.move_by(1, Direction::Forward);
}
Expand Down