From c06155ace4ef4aa65b680093da920bded320b8f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Fri, 18 Feb 2022 14:01:50 +0900 Subject: [PATCH] Extract a helper function for lsp::Location --- helix-term/src/commands/lsp.rs | 30 ++++++++++++------------------ helix-term/src/ui/mod.rs | 2 +- helix-term/src/ui/picker.rs | 2 +- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 084c7c6a8..722490b26 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -11,11 +11,20 @@ use crate::{ compositor::{self, Compositor}, - ui::{self, overlay::overlayed, FilePicker, Popup, Prompt, PromptEvent}, + ui::{self, overlay::overlayed, FileLocation, FilePicker, Popup, Prompt, PromptEvent}, }; use std::borrow::Cow; +fn location_to_file_location(location: &lsp::Location) -> FileLocation { + let path = location.uri.to_file_path().unwrap(); + let line = Some(( + location.range.start.line as usize, + location.range.end.line as usize, + )); + (path, line) +} + fn sym_picker( symbols: Vec, current_path: Option, @@ -55,14 +64,7 @@ fn sym_picker( align_view(doc, view, Align::Center); } }, - move |_editor, symbol| { - let path = symbol.location.uri.to_file_path().unwrap(); - let line = Some(( - symbol.location.range.start.line as usize, - symbol.location.range.end.line as usize, - )); - Some((path, line)) - }, + move |_editor, symbol| Some(location_to_file_location(&symbol.location)), ); picker.truncate_start = false; picker @@ -465,15 +467,7 @@ fn jump_to( format!("{}:{}", file, line).into() }, move |cx, location, action| jump_to(cx.editor, location, offset_encoding, action), - |_editor, location| { - // TODO: share code for symbol.location and location - let path = location.uri.to_file_path().unwrap(); - let line = Some(( - location.range.start.line as usize, - location.range.end.line as usize, - )); - Some((path, line)) - }, + move |_editor, location| Some(location_to_file_location(location)), ); compositor.push(Box::new(overlayed(picker))); } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index e269c8edd..21c1f7aa8 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -14,7 +14,7 @@ pub use editor::EditorView; pub use markdown::Markdown; pub use menu::Menu; -pub use picker::{FilePicker, Picker}; +pub use picker::{FileLocation, FilePicker, Picker}; pub use popup::Popup; pub use prompt::{Prompt, PromptEvent}; pub use spinner::{ProgressSpinners, Spinner}; diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 622af3876..9e236510f 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -33,7 +33,7 @@ pub const MAX_FILE_SIZE_FOR_PREVIEW: u64 = 10 * 1024 * 1024; /// File path and range of lines (used to align and highlight lines) -type FileLocation = (PathBuf, Option<(usize, usize)>); +pub type FileLocation = (PathBuf, Option<(usize, usize)>); pub struct FilePicker { picker: Picker,