aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui
diff options
context:
space:
mode:
authorBlaž Hrastnik2021-08-24 04:21:06 +0000
committerBlaž Hrastnik2021-08-24 04:25:39 +0000
commita5c3c6c6a9e21e850440130e00ebaff57e7d4f4d (patch)
treee8e48721b32eb61d743a7f5652d41b354341edad /helix-term/src/ui
parent490125f008f22be6150f95005bc081d680a69020 (diff)
ui: Highlight line ranges in the preview
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r--helix-term/src/ui/picker.rs29
1 files changed, 19 insertions, 10 deletions
diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs
index ecf9d528..036b41c8 100644
--- a/helix-term/src/ui/picker.rs
+++ b/helix-term/src/ui/picker.rs
@@ -26,7 +26,7 @@ use helix_view::{
pub const MIN_SCREEN_WIDTH_FOR_PREVIEW: u16 = 80;
/// File path and line number (used to align and highlight a line)
-type FileLocation = (PathBuf, Option<usize>);
+type FileLocation = (PathBuf, Option<(usize, usize)>);
pub struct FilePicker<T> {
picker: Picker<T>,
@@ -113,14 +113,17 @@ impl<T: 'static> Component for FilePicker<T> {
block.render(preview_area, surface);
- if let Some((doc, line)) = self.current_file(cx.editor).and_then(|(path, line)| {
+ if let Some((doc, line)) = self.current_file(cx.editor).and_then(|(path, range)| {
cx.editor
.document_by_path(&path)
.or_else(|| self.preview_cache.get(&path))
- .zip(Some(line))
+ .zip(Some(range))
}) {
// align to middle
- let first_line = line.unwrap_or(0).saturating_sub(inner.height as usize / 2);
+ let first_line = line
+ .map(|(start, _)| start)
+ .unwrap_or(0)
+ .saturating_sub(inner.height as usize / 2);
let offset = Position::new(first_line, 0);
let highlights = EditorView::doc_syntax_highlights(
@@ -140,12 +143,18 @@ impl<T: 'static> Component for FilePicker<T> {
);
// highlight the line
- if let Some(line) = line {
- for x in inner.left()..inner.right() {
- surface
- .get_mut(x, inner.y + line.saturating_sub(first_line) as u16)
- .set_style(cx.editor.theme.get("ui.selection"));
- }
+ if let Some((start, end)) = line {
+ let offset = start.saturating_sub(first_line) as u16;
+ surface.set_style(
+ Rect::new(
+ inner.x,
+ inner.y + offset,
+ inner.width,
+ (end.saturating_sub(start) as u16 + 1)
+ .min(inner.height.saturating_sub(offset)),
+ ),
+ cx.editor.theme.get("ui.selection"),
+ );
}
}
}