on master help sign in
Extract jump_to_location
1cd710fBlaž Hrastnik
helix-term/src/commands/lsp.rs+30−27
@@ -35,6 +35,32 @@ fn location_to_file_location(location: &lsp::Location) -> FileLocation {
35 (path, line)
35 (path, line)
36}
36}
37
37
38+// TODO: share with symbol picker(symbol.location)
39+// TODO: need to use push_jump() before?
40+fn jump_to_location(
41+ editor: &mut Editor,
42+ location: &lsp::Location,
43+ offset_encoding: OffsetEncoding,
44+ action: Action,
45+) {
46+ let path = location
47+ .uri
48+ .to_file_path()
49+ .expect("unable to convert URI to filepath");
50+ let _id = editor.open(path, action).expect("editor.open failed");
51+ let (view, doc) = current!(editor);
52+ let definition_pos = location.range.start;
53+ // TODO: convert inside server
54+ let new_pos = if let Some(new_pos) = lsp_pos_to_pos(doc.text(), definition_pos, offset_encoding)
55+ {
56+ new_pos
57+ } else {
58+ return;
59+ };
60+ doc.set_selection(view.id, Selection::point(new_pos));
61+ align_view(doc, view, Align::Center);
62+}
63+
38fn sym_picker(
64fn sym_picker(
39 symbols: Vec<lsp::SymbolInformation>,
65 symbols: Vec<lsp::SymbolInformation>,
40 current_path: Option<lsp::Url>,
66 current_path: Option<lsp::Url>,
@@ -407,36 +433,11 @@ fn goto_impl(
407) {
433) {
408 push_jump(editor);
434 push_jump(editor);
409
435
410− // TODO: share with symbol picker(symbol.location)
411− fn jump_to(
412− editor: &mut Editor,
413− location: &lsp::Location,
414− offset_encoding: OffsetEncoding,
415− action: Action,
416− ) {
417− let path = location
418− .uri
419− .to_file_path()
420− .expect("unable to convert URI to filepath");
421− let _id = editor.open(path, action).expect("editor.open failed");
422− let (view, doc) = current!(editor);
423− let definition_pos = location.range.start;
424− // TODO: convert inside server
425− let new_pos =
426− if let Some(new_pos) = lsp_pos_to_pos(doc.text(), definition_pos, offset_encoding) {
427− new_pos
428− } else {
429− return;
430− };
431− doc.set_selection(view.id, Selection::point(new_pos));
432− align_view(doc, view, Align::Center);
433− }
434−
435 let cwdir = std::env::current_dir().expect("couldn't determine current directory");
436 let cwdir = std::env::current_dir().expect("couldn't determine current directory");
436
437
437 match locations.as_slice() {
438 match locations.as_slice() {
438 [location] => {
439 [location] => {
439− jump_to(editor, location, offset_encoding, Action::Replace);
440+ jump_to_location(editor, location, offset_encoding, Action::Replace);
440 }
441 }
441 [] => {
442 [] => {
442 editor.set_error("No definition found.");
443 editor.set_error("No definition found.");
@@ -464,7 +465,9 @@ fn goto_impl(
464 let line = location.range.start.line;
465 let line = location.range.start.line;
465 format!("{}:{}", file, line).into()
466 format!("{}:{}", file, line).into()
466 },
467 },
468+ move |cx, location, action| {
467− move |cx, location, action| jump_to(cx.editor, location, offset_encoding, action),
469+ jump_to_location(cx.editor, location, offset_encoding, action)
470+ },
468 move |_editor, location| Some(location_to_file_location(location)),
471 move |_editor, location| Some(location_to_file_location(location)),
469 );
472 );
470 compositor.push(Box::new(overlayed(picker)));
473 compositor.push(Box::new(overlayed(picker)));