From 1cd710fe01eb2ccb1d35d25e74d967b5645e2ea8 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Fri, 18 Feb 2022 14:07:35 +0900 Subject: Extract jump_to_location --- helix-term/src/commands/lsp.rs | 57 ++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'helix-term') diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index e1fb4cbb..eee6968e 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -35,6 +35,32 @@ fn location_to_file_location(location: &lsp::Location) -> FileLocation { (path, line) } +// TODO: share with symbol picker(symbol.location) +// TODO: need to use push_jump() before? +fn jump_to_location( + editor: &mut Editor, + location: &lsp::Location, + offset_encoding: OffsetEncoding, + action: Action, +) { + let path = location + .uri + .to_file_path() + .expect("unable to convert URI to filepath"); + let _id = editor.open(path, action).expect("editor.open failed"); + let (view, doc) = current!(editor); + let definition_pos = location.range.start; + // TODO: convert inside server + let new_pos = if let Some(new_pos) = lsp_pos_to_pos(doc.text(), definition_pos, offset_encoding) + { + new_pos + } else { + return; + }; + doc.set_selection(view.id, Selection::point(new_pos)); + align_view(doc, view, Align::Center); +} + fn sym_picker( symbols: Vec, current_path: Option, @@ -407,36 +433,11 @@ fn goto_impl( ) { push_jump(editor); - // TODO: share with symbol picker(symbol.location) - fn jump_to( - editor: &mut Editor, - location: &lsp::Location, - offset_encoding: OffsetEncoding, - action: Action, - ) { - let path = location - .uri - .to_file_path() - .expect("unable to convert URI to filepath"); - let _id = editor.open(path, action).expect("editor.open failed"); - let (view, doc) = current!(editor); - let definition_pos = location.range.start; - // TODO: convert inside server - let new_pos = - if let Some(new_pos) = lsp_pos_to_pos(doc.text(), definition_pos, offset_encoding) { - new_pos - } else { - return; - }; - doc.set_selection(view.id, Selection::point(new_pos)); - align_view(doc, view, Align::Center); - } - let cwdir = std::env::current_dir().expect("couldn't determine current directory"); match locations.as_slice() { [location] => { - jump_to(editor, location, offset_encoding, Action::Replace); + jump_to_location(editor, location, offset_encoding, Action::Replace); } [] => { editor.set_error("No definition found."); @@ -464,7 +465,9 @@ fn goto_impl( let line = location.range.start.line; format!("{}:{}", file, line).into() }, - move |cx, location, action| jump_to(cx.editor, location, offset_encoding, action), + move |cx, location, action| { + jump_to_location(cx.editor, location, offset_encoding, action) + }, move |_editor, location| Some(location_to_file_location(location)), ); compositor.push(Box::new(overlayed(picker))); -- cgit v1.2.3-70-g09d2