on master help sign in
ui: Highlight line ranges in the preview
commita5c3c6c6a9e21e850440130e00ebaff57e7d4f4d authorBlaž Hrastnik <blaz@mxxn.io> dateTue 24 Aug 2021, 13:21 (5 years ago) parent490125f
2 files changed+28−13
helix-term/src/commands.rs+9−3 helix-term/src/ui/picker.rs+19−10
helix-term/src/commands.rs+9−3
@@ -2314,7 +2314,7 @@ fn buffer_picker(cx: &mut Context) {
23142314 .selection(view_id)
23152315 .primary()
23162316 .cursor_line(doc.text().slice(..));
2317− Some((path.clone()?, Some(line)))
2317+ Some((path.clone()?, Some((line, line))))
23182318 },
23192319 );
23202320 cx.push_layer(Box::new(picker));
@@ -2387,7 +2387,10 @@ fn symbol_picker(cx: &mut Context) {
23872387 },
23882388 move |_editor, symbol| {
23892389 let path = symbol.location.uri.to_file_path().unwrap();
2390− let line = Some(symbol.location.range.start.line as usize);
2390+ let line = Some((
2391+ symbol.location.range.start.line as usize,
2392+ symbol.location.range.end.line as usize,
2393+ ));
23912394 Some((path, line))
23922395 },
23932396 );
@@ -2820,7 +2823,10 @@ fn goto_impl(
28202823 },
28212824 |_editor, location| {
28222825 let path = location.uri.to_file_path().unwrap();
2823− let line = Some(location.range.start.line as usize);
2826+ let line = Some((
2827+ location.range.start.line as usize,
2828+ location.range.end.line as usize,
2829+ ));
28242830 Some((path, line))
28252831 },
28262832 );
helix-term/src/ui/picker.rs+19−10
@@ -26,7 +26,7 @@ use helix_view::{
2626pub const MIN_SCREEN_WIDTH_FOR_PREVIEW: u16 = 80;
2727
2828/// File path and line number (used to align and highlight a line)
29−type FileLocation = (PathBuf, Option<usize>);
29+type FileLocation = (PathBuf, Option<(usize, usize)>);
3030
3131pub struct FilePicker<T> {
3232 picker: Picker<T>,
@@ -113,14 +113,17 @@ impl<T: 'static> Component for FilePicker<T> {
113113
114114 block.render(preview_area, surface);
115115
116− if let Some((doc, line)) = self.current_file(cx.editor).and_then(|(path, line)| {
116+ if let Some((doc, line)) = self.current_file(cx.editor).and_then(|(path, range)| {
117117 cx.editor
118118 .document_by_path(&path)
119119 .or_else(|| self.preview_cache.get(&path))
120− .zip(Some(line))
120+ .zip(Some(range))
121121 }) {
122122 // align to middle
123− let first_line = line.unwrap_or(0).saturating_sub(inner.height as usize / 2);
123+ let first_line = line
124+ .map(|(start, _)| start)
125+ .unwrap_or(0)
126+ .saturating_sub(inner.height as usize / 2);
124127 let offset = Position::new(first_line, 0);
125128
126129 let highlights = EditorView::doc_syntax_highlights(
@@ -140,12 +143,18 @@ impl<T: 'static> Component for FilePicker<T> {
140143 );
141144
142145 // highlight the line
143− if let Some(line) = line {
144− for x in inner.left()..inner.right() {
145− surface
146− .get_mut(x, inner.y + line.saturating_sub(first_line) as u16)
147− .set_style(cx.editor.theme.get("ui.selection"));
148− }
146+ if let Some((start, end)) = line {
147+ let offset = start.saturating_sub(first_line) as u16;
148+ surface.set_style(
149+ Rect::new(
150+ inner.x,
151+ inner.y + offset,
152+ inner.width,
153+ (end.saturating_sub(start) as u16 + 1)
154+ .min(inner.height.saturating_sub(offset)),
155+ ),
156+ cx.editor.theme.get("ui.selection"),
157+ );
149158 }
150159 }
151160 }