aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPascal Kuthe2023-09-12 19:12:36 +0000
committerBlaž Hrastnik2023-09-14 02:00:28 +0000
commit13d4463e4146473391e37b7f75e99b731aa55878 (patch)
treef85a47272df8a9e429b9f4f230feaf59ca536c59
parente9d0bd7aefda6962b56245eb7d0f56b5d2fa4859 (diff)
correctly center items in picker preview
-rw-r--r--helix-term/src/ui/picker.rs36
1 files changed, 24 insertions, 12 deletions
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs
index 7e745f72..2c41fc12 100644
--- a/helix-term/src/ui/picker.rs
+++ b/helix-term/src/ui/picker.rs
@@ -710,18 +710,30 @@ impl<T: Item + 'static> Picker<T> {
};
let mut offset = ViewPosition::default();
- if let Some(range) = range {
- let text_fmt = doc.text_format(inner.width, None);
- let annotations = TextAnnotations::default();
- (offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset(
- doc.text().slice(..),
- doc.text().line_to_char(range.0),
- // align to middle
- -(inner.height as isize / 2),
- 0,
- &text_fmt,
- &annotations,
- );
+ if let Some((start_line, end_line)) = range {
+ let height = end_line - start_line;
+ let text = doc.text().slice(..);
+ let start = text.line_to_char(start_line);
+ let middle = text.line_to_char(start_line + height / 2);
+ if height < inner.height as usize {
+ let text_fmt = doc.text_format(inner.width, None);
+ let annotations = TextAnnotations::default();
+ (offset.anchor, offset.vertical_offset) = char_idx_at_visual_offset(
+ text,
+ middle,
+ // align to middle
+ -(inner.height as isize / 2),
+ 0,
+ &text_fmt,
+ &annotations,
+ );
+ if start < offset.anchor {
+ offset.anchor = start;
+ offset.vertical_offset = 0;
+ }
+ } else {
+ offset.anchor = start;
+ }
}
let mut highlights = EditorView::doc_syntax_highlights(