Extract some duplication in lsp goto_ calls

pull/1604/head
Blaž Hrastnik 2 years ago
parent 1cd710fe01
commit 5af9136aec

@ -475,6 +475,21 @@ fn goto_impl(
} }
} }
fn to_locations(definitions: Option<lsp::GotoDefinitionResponse>) -> Vec<lsp::Location> {
match definitions {
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
.into_iter()
.map(|location_link| lsp::Location {
uri: location_link.target_uri,
range: location_link.target_range,
})
.collect(),
None => Vec::new(),
}
}
pub fn goto_definition(cx: &mut Context) { pub fn goto_definition(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let language_server = language_server!(doc); let language_server = language_server!(doc);
@ -492,22 +507,8 @@ pub fn goto_definition(cx: &mut Context) {
cx.callback( cx.callback(
future, future,
move |editor: &mut Editor, move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
compositor: &mut Compositor, let items = to_locations(response);
response: Option<lsp::GotoDefinitionResponse>| {
let items = match response {
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
.into_iter()
.map(|location_link| lsp::Location {
uri: location_link.target_uri,
range: location_link.target_range,
})
.collect(),
None => Vec::new(),
};
goto_impl(editor, compositor, items, offset_encoding); goto_impl(editor, compositor, items, offset_encoding);
}, },
); );
@ -530,22 +531,8 @@ pub fn goto_type_definition(cx: &mut Context) {
cx.callback( cx.callback(
future, future,
move |editor: &mut Editor, move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
compositor: &mut Compositor, let items = to_locations(response);
response: Option<lsp::GotoDefinitionResponse>| {
let items = match response {
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
.into_iter()
.map(|location_link| lsp::Location {
uri: location_link.target_uri,
range: location_link.target_range,
})
.collect(),
None => Vec::new(),
};
goto_impl(editor, compositor, items, offset_encoding); goto_impl(editor, compositor, items, offset_encoding);
}, },
); );
@ -568,22 +555,8 @@ pub fn goto_implementation(cx: &mut Context) {
cx.callback( cx.callback(
future, future,
move |editor: &mut Editor, move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
compositor: &mut Compositor, let items = to_locations(response);
response: Option<lsp::GotoDefinitionResponse>| {
let items = match response {
Some(lsp::GotoDefinitionResponse::Scalar(location)) => vec![location],
Some(lsp::GotoDefinitionResponse::Array(locations)) => locations,
Some(lsp::GotoDefinitionResponse::Link(locations)) => locations
.into_iter()
.map(|location_link| lsp::Location {
uri: location_link.target_uri,
range: location_link.target_range,
})
.collect(),
None => Vec::new(),
};
goto_impl(editor, compositor, items, offset_encoding); goto_impl(editor, compositor, items, offset_encoding);
}, },
); );
@ -606,15 +579,9 @@ pub fn goto_reference(cx: &mut Context) {
cx.callback( cx.callback(
future, future,
move |editor: &mut Editor, move |editor, compositor, response: Option<Vec<lsp::Location>>| {
compositor: &mut Compositor, let items = response.unwrap_or_default();
items: Option<Vec<lsp::Location>>| { goto_impl(editor, compositor, items, offset_encoding);
goto_impl(
editor,
compositor,
items.unwrap_or_default(),
offset_encoding,
);
}, },
); );
} }
@ -635,9 +602,7 @@ pub fn signature_help(cx: &mut Context) {
cx.callback( cx.callback(
future, future,
move |_editor: &mut Editor, move |_editor, _compositor, response: Option<lsp::SignatureHelp>| {
_compositor: &mut Compositor,
response: Option<lsp::SignatureHelp>| {
if let Some(signature_help) = response { if let Some(signature_help) = response {
log::info!("{:?}", signature_help); log::info!("{:?}", signature_help);
// signatures // signatures

Loading…
Cancel
Save