Skip to content

Commit 13d4463

Browse files
pascalkuthearchseer
authored andcommitted
correctly center items in picker preview
1 parent e9d0bd7 commit 13d4463

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

helix-term/src/ui/picker.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -710,18 +710,30 @@ impl<T: Item + 'static> Picker<T> {
710710
};
711711

712712
let mut offset = ViewPosition::default();
713-
if let Some(range) = range {
714-
let text_fmt = doc.text_format(inner.width, None);
715-
let annotations = TextAnnotations::default();
716-
(offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset(
717-
doc.text().slice(..),
718-
doc.text().line_to_char(range.0),
719-
// align to middle
720-
-(inner.height as isize / 2),
721-
0,
722-
&text_fmt,
723-
&annotations,
724-
);
713+
if let Some((start_line, end_line)) = range {
714+
let height = end_line - start_line;
715+
let text = doc.text().slice(..);
716+
let start = text.line_to_char(start_line);
717+
let middle = text.line_to_char(start_line + height / 2);
718+
if height < inner.height as usize {
719+
let text_fmt = doc.text_format(inner.width, None);
720+
let annotations = TextAnnotations::default();
721+
(offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset(
722+
text,
723+
middle,
724+
// align to middle
725+
-(inner.height as isize / 2),
726+
0,
727+
&text_fmt,
728+
&annotations,
729+
);
730+
if start < offset.anchor {
731+
offset.anchor = start;
732+
offset.vertical_offset = 0;
733+
}
734+
} else {
735+
offset.anchor = start;
736+
}
725737
}
726738

727739
let mut highlights = EditorView::doc_syntax_highlights(

0 commit comments

Comments
 (0)