Skip to content

Commit 5836ee0

Browse files
committed
Implement basic preview scrolling for FilePicker
May be the starting point for more thorough imp- lementation if mainteners approve the further work
1 parent 68c7e87 commit 5836ee0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

helix-term/src/ui/picker.rs

Lines changed: 36 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)
@@ -956,6 +981,11 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
956981
} else {
957982
offset.anchor = start;
958983
}
984+
} else {
985+
let text = doc.text().slice(..);
986+
987+
let view_start = text.line_to_char(scroll_position);
988+
offset.anchor = view_start;
959989
}
960990

961991
let loader = cx.editor.syn_loader.load();
@@ -1143,6 +1173,12 @@ 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+
self.scroll.position += 1;
1178+
}
1179+
ctrl!('k') => {
1180+
self.scroll.position = self.scroll.position.saturating_sub(1);
1181+
}
11461182
_ => {
11471183
self.prompt_handle_event(event, ctx);
11481184
}

0 commit comments

Comments
 (0)