Skip to content

Commit f265126

Browse files
committed
Implement preview scrolling for Picker
1 parent 68c7e87 commit f265126

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

helix-term/src/ui/picker.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ pub struct Picker<T: 'static + Send + Sync, D: 'static> {
269269
/// An event handler for syntax highlighting the currently previewed file.
270270
preview_highlight_handler: Sender<Arc<Path>>,
271271
dynamic_query_handler: Option<Sender<DynamicQueryChange>>,
272+
scroll: ScrollState,
273+
}
274+
275+
struct ScrollState {
276+
cursor_id: u32,
277+
position: usize,
278+
}
279+
280+
impl ScrollState {
281+
fn get_position(&mut self, cursor: u32) -> usize {
282+
if cursor == self.cursor_id {
283+
self.position
284+
} else {
285+
self.cursor_id = cursor;
286+
self.position = 0;
287+
self.position
288+
}
289+
}
272290
}
273291

274292
impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
@@ -394,6 +412,10 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
394412
file_fn: None,
395413
preview_highlight_handler: PreviewHighlightHandler::<T, D>::default().spawn(),
396414
dynamic_query_handler: None,
415+
scroll: ScrollState {
416+
cursor_id: 0,
417+
position: 0,
418+
},
397419
}
398420
}
399421

@@ -897,6 +919,9 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
897919
let inner = inner.inner(margin);
898920
BLOCK.render(area, surface);
899921

922+
let current_cursor = self.cursor;
923+
let scroll_position = self.scroll.get_position(current_cursor);
924+
900925
if let Some((preview, range)) = self.get_preview(cx.editor) {
901926
let doc = match preview.document() {
902927
Some(doc)
@@ -958,6 +983,11 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
958983
}
959984
}
960985

986+
let text = doc.text().slice(..);
987+
988+
let view_start = text.line_to_char(scroll_position);
989+
offset.anchor = view_start;
990+
961991
let loader = cx.editor.syn_loader.load();
962992

963993
let syntax_highlighter =
@@ -1143,6 +1173,18 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I,
11431173
ctrl!('t') => {
11441174
self.toggle_preview();
11451175
}
1176+
ctrl!('j') => {
1177+
if let Some((preview, _)) = self.get_preview(ctx.editor) {
1178+
if let Some(doc) = preview.document() {
1179+
let max_lines = doc.text().len_lines();
1180+
self.scroll.position =
1181+
(self.scroll.position.saturating_add(1)).min(max_lines);
1182+
}
1183+
}
1184+
}
1185+
ctrl!('k') => {
1186+
self.scroll.position = self.scroll.position.saturating_sub(1);
1187+
}
11461188
_ => {
11471189
self.prompt_handle_event(event, ctx);
11481190
}

0 commit comments

Comments
 (0)