From 2e52b27e7139095393c7e4eebd7da0c6c26b053e Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sun, 6 Jan 2019 09:41:11 +0100 Subject: refactor --- crates/ra_lsp_server/src/main_loop/handlers.rs | 47 ++++++++++++-------------- 1 file changed, 21 insertions(+), 26 deletions(-) (limited to 'crates/ra_lsp_server/src/main_loop') diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 2ec9073e4..51f134e8a 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -93,36 +93,31 @@ pub fn handle_on_type_formatting( world: ServerWorld, params: req::DocumentOnTypeFormattingParams, ) -> Result>> { - if params.ch != "=" || params.ch != "." { - return Ok(None); - } - - let file_id = params.text_document.try_conv_with(&world)?; - let line_index = world.analysis().file_line_index(file_id); - let position = FilePosition { - file_id, - offset: params.position.conv_with(&line_index), + let analysis: Option Option>> = match params.ch.as_str() { + "=" => Some(Box::new(|pos| world.analysis().on_eq_typed(pos))), + "." => Some(Box::new(|pos| world.analysis().on_dot_typed(pos))), + _ => None, }; - let analysis: Vec Option>> = vec![ - Box::new(|pos| world.analysis().on_eq_typed(pos)), - Box::new(|pos| world.analysis().on_dot_typed(pos)), - ]; + if let Some(ana) = analysis { + let file_id = params.text_document.try_conv_with(&world)?; + let line_index = world.analysis().file_line_index(file_id); + let position = FilePosition { + file_id, + offset: params.position.conv_with(&line_index), + }; - // try all analysis until one succeeds - for ana in analysis { if let Some(mut action) = ana(position) { - return Ok(Some( - action - .source_file_edits - .pop() - .unwrap() - .edit - .as_atoms() - .iter() - .map_conv_with(&line_index) - .collect(), - )); + let change: Vec = action + .source_file_edits + .pop() + .unwrap() + .edit + .as_atoms() + .iter() + .map_conv_with(&line_index) + .collect(); + return Ok(Some(change)); } } -- cgit v1.2.3