Skip to content

Commit

Permalink
center the preview selection using visual lines
Browse files Browse the repository at this point in the history
this way the preview always shows the selection even if lines were
wrapped
  • Loading branch information
doy committed Aug 5, 2023
1 parent 80d2599 commit 26d31a2
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{collections::HashMap, io::Read, path::PathBuf};

use crate::ui::{Prompt, PromptEvent};
use helix_core::{
movement::Direction, text_annotations::TextAnnotations,
char_idx_at_visual_offset, movement::Direction, text_annotations::TextAnnotations,
unicode::segmentation::UnicodeSegmentation, Position, Syntax,
};
use helix_view::{
Expand Down Expand Up @@ -690,20 +690,19 @@ impl<T: Item + 'static> Picker<T> {
}
};

// align to middle
let first_line = range
.map(|(start, end)| {
let height = end.saturating_sub(start) + 1;
let middle = start + (height.saturating_sub(1) / 2);
middle.saturating_sub(inner.height as usize / 2).min(start)
})
.unwrap_or(0);

let offset = ViewPosition {
anchor: doc.text().line_to_char(first_line),
horizontal_offset: 0,
vertical_offset: 0,
};
let text_fmt = doc.text_format(inner.width, None);
let annotations = TextAnnotations::default();
let mut offset = ViewPosition::default();
(offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset(
doc.text().slice(..),
doc.text()
.line_to_char(range.map(|(start, _)| start).unwrap_or(0)),
// align to middle
-(inner.height as isize / 2),
0,
&text_fmt,
&annotations,
);

let mut highlights = EditorView::doc_syntax_highlights(
doc,
Expand Down

0 comments on commit 26d31a2

Please sign in to comment.