on master help sign in
Extract some duplication in lsp goto_ calls
commit5af9136aec31f712e2dea0f6545edb6fd3bdf4dd authorBlaž Hrastnik <blaz@mxxn.io> dateFri 18 Feb 2022, 14:11 (5 years ago) parent1cd710f
1 file changed+25−60
helix-term/src/commands/lsp.rs+25−60
helix-term/src/commands/lsp.rs+25−60
@@ -475,6 +475,21 @@ fn goto_impl(
475475 }
476476}
477477
478+fn to_locations(definitions: Option<lsp::GotoDefinitionResponse>) -> Vec<lsp::Location> {
479+ match definitions {
480+ Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
481+ Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
482+ Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
483+ .into_iter()
484+ .map(|location_link| lsp::Location {
485+ uri: location_link.target_uri,
486+ range: location_link.target_range,
487+ })
488+ .collect(),
489+ None => Vec::new(),
490+ }
491+}
492+
478493pub fn goto_definition(cx: &mut Context) {
479494 let (view, doc) = current!(cx.editor);
480495 let language_server = language_server!(doc);
@@ -492,22 +507,8 @@ pub fn goto_definition(cx: &mut Context) {
492507
493508 cx.callback(
494509 future,
495− move |editor: &mut Editor,
496− compositor: &mut Compositor,
497− response: Option<lsp::GotoDefinitionResponse>| {
498− let items = match response {
499− Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
500− Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
501− Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
502− .into_iter()
503− .map(|location_link| lsp::Location {
504− uri: location_link.target_uri,
505− range: location_link.target_range,
506− })
507− .collect(),
508− None => Vec::new(),
509− };
510−
510+ move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
511+ let items = to_locations(response);
511512 goto_impl(editor, compositor, items, offset_encoding);
512513 },
513514 );
@@ -530,22 +531,8 @@ pub fn goto_type_definition(cx: &mut Context) {
530531
531532 cx.callback(
532533 future,
533− move |editor: &mut Editor,
534− compositor: &mut Compositor,
535− response: Option<lsp::GotoDefinitionResponse>| {
536− let items = match response {
537− Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
538− Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
539− Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
540− .into_iter()
541− .map(|location_link| lsp::Location {
542− uri: location_link.target_uri,
543− range: location_link.target_range,
544− })
545− .collect(),
546− None => Vec::new(),
547− };
548−
534+ move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
535+ let items = to_locations(response);
549536 goto_impl(editor, compositor, items, offset_encoding);
550537 },
551538 );
@@ -568,22 +555,8 @@ pub fn goto_implementation(cx: &mut Context) {
568555
569556 cx.callback(
570557 future,
571− move |editor: &mut Editor,
572− compositor: &mut Compositor,
573− response: Option<lsp::GotoDefinitionResponse>| {
574− let items = match response {
575− Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
576− Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
577− Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
578− .into_iter()
579− .map(|location_link| lsp::Location {
580− uri: location_link.target_uri,
581− range: location_link.target_range,
582− })
583− .collect(),
584− None => Vec::new(),
585− };
586−
558+ move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
559+ let items = to_locations(response);
587560 goto_impl(editor, compositor, items, offset_encoding);
588561 },
589562 );
@@ -606,15 +579,9 @@ pub fn goto_reference(cx: &mut Context) {
606579
607580 cx.callback(
608581 future,
609− move |editor: &mut Editor,
610− compositor: &mut Compositor,
611− items: Option<Vec<lsp::Location>>| {
612− goto_impl(
613− editor,
614− compositor,
615− items.unwrap_or_default(),
616− offset_encoding,
617− );
582+ move |editor, compositor, response: Option<Vec<lsp::Location>>| {
583+ let items = response.unwrap_or_default();
584+ goto_impl(editor, compositor, items, offset_encoding);
618585 },
619586 );
620587}
@@ -635,9 +602,7 @@ pub fn signature_help(cx: &mut Context) {
635602
636603 cx.callback(
637604 future,
638− move |_editor: &mut Editor,
639− _compositor: &mut Compositor,
640− response: Option<lsp::SignatureHelp>| {
605+ move |_editor, _compositor, response: Option<lsp::SignatureHelp>| {
641606 if let Some(signature_help) = response {
642607 log::info!("{:?}", signature_help);
643608 // signatures