From 1141d448d960eedba0a5647d525910de706bf778 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 14 Aug 2018 13:33:44 +0300 Subject: Add derive intention --- crates/server/src/main_loop/handlers.rs | 107 ++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 47 deletions(-) (limited to 'crates/server/src/main_loop') diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index 8789ef0d2..9de6f480b 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -87,27 +87,28 @@ pub fn handle_code_action( let file = world.file_syntax(&path)?; let line_index = world.file_line_index(&path)?; let offset = params.range.conv_with(&line_index).start(); - let ret = if libeditor::flip_comma(&file, offset).is_some() { - let cmd = apply_code_action_cmd( - ActionId::FlipComma, - params.text_document, - offset, - ); - Some(vec![cmd]) - } else { - None - }; - Ok(ret) + let mut ret = Vec::new(); + + let actions = &[ + (ActionId::FlipComma, libeditor::flip_comma(&file, offset).is_some()), + (ActionId::AddDerive, libeditor::add_derive(&file, offset).is_some()), + ]; + + for (id, edit) in actions { + if *edit { + let cmd = apply_code_action_cmd(*id, params.text_document.clone(), offset); + ret.push(cmd); + } + } + return Ok(Some(ret)); } pub fn handle_workspace_symbol( world: World, params: req::WorkspaceSymbolParams, ) -> Result>> { - let mut acc = Vec::new(); - + let all_symbols = params.query.contains("#"); let query = { - let all_symbols = params.query.contains("#"); let query: String = params.query.chars() .filter(|&c| c != '#') .collect(); @@ -118,19 +119,29 @@ pub fn handle_workspace_symbol( q.limit(128); q }; + let mut res = exec_query(&world, query)?; + if res.is_empty() && !all_symbols { + let mut query = Query::new(params.query); + query.limit(128); + res = exec_query(&world, query)?; + } - for (path, symbol) in world.world_symbols(query) { - let line_index = world.file_line_index(path)?; - let info = SymbolInformation { - name: symbol.name.to_string(), - kind: symbol.kind.conv(), - location: (path, symbol.node_range).try_conv_with(&line_index)?, - container_name: None, - }; - acc.push(info); - }; + return Ok(Some(res)); - Ok(Some(acc)) + fn exec_query(world: &World, query: Query) -> Result> { + let mut res = Vec::new(); + for (path, symbol) in world.world_symbols(query) { + let line_index = world.file_line_index(path)?; + let info = SymbolInformation { + name: symbol.name.to_string(), + kind: symbol.kind.conv(), + location: (path, symbol.node_range).try_conv_with(&line_index)?, + container_name: None, + }; + res.push(info); + }; + Ok(res) + } } pub fn handle_goto_definition( @@ -161,28 +172,28 @@ pub fn handle_execute_command( } let arg = params.arguments.pop().unwrap(); let arg: ActionRequest = from_value(arg)?; - match arg.id { - ActionId::FlipComma => { - let path = arg.text_document.file_path()?; - let file = world.file_syntax(&path)?; - let line_index = world.file_line_index(&path)?; - let edit = match libeditor::flip_comma(&file, arg.offset) { - Some(edit) => edit(), - None => bail!("command not applicable"), - }; - let mut changes = HashMap::new(); - changes.insert( - arg.text_document.uri, - edit.conv_with(&line_index), - ); - let edit = WorkspaceEdit { - changes: Some(changes), - document_changes: None, - }; + let path = arg.text_document.file_path()?; + let file = world.file_syntax(&path)?; + let edit = match arg.id { + ActionId::FlipComma => libeditor::flip_comma(&file, arg.offset).map(|edit| edit()), + ActionId::AddDerive => libeditor::add_derive(&file, arg.offset).map(|edit| edit()), + }; + let edit = match edit { + Some(edit) => edit, + None => bail!("command not applicable"), + }; + let line_index = world.file_line_index(&path)?; + let mut changes = HashMap::new(); + changes.insert( + arg.text_document.uri, + edit.conv_with(&line_index), + ); + let edit = WorkspaceEdit { + changes: Some(changes), + document_changes: None, + }; - Ok(req::ApplyWorkspaceEditParams { edit }) - } - } + Ok(req::ApplyWorkspaceEditParams { edit }) } #[derive(Serialize, Deserialize)] @@ -207,13 +218,15 @@ fn apply_code_action_cmd(id: ActionId, doc: TextDocumentIdentifier, offset: Text #[derive(Serialize, Deserialize, Clone, Copy)] enum ActionId { - FlipComma + FlipComma, + AddDerive, } impl ActionId { fn title(&self) -> &'static str { match *self { ActionId::FlipComma => "Flip `,`", + ActionId::AddDerive => "Add `#[derive]`", } } } -- cgit v1.2.3