From bacd0428fa0fd744eb0aac6d5d7abd18c6c707b7 Mon Sep 17 00:00:00 2001 From: Mikhail Rakhmanov Date: Wed, 3 Jun 2020 18:39:01 +0200 Subject: Fix review comments --- crates/rust-analyzer/src/lsp_ext.rs | 1 - crates/rust-analyzer/src/main_loop/handlers.rs | 27 ++++++++++---------- crates/rust-analyzer/src/to_proto.rs | 34 +++++++++----------------- editors/code/src/client.ts | 2 -- editors/code/src/lsp_ext.ts | 1 - 5 files changed, 24 insertions(+), 41 deletions(-) diff --git a/crates/rust-analyzer/src/lsp_ext.rs b/crates/rust-analyzer/src/lsp_ext.rs index 4b436c301..3b957534d 100644 --- a/crates/rust-analyzer/src/lsp_ext.rs +++ b/crates/rust-analyzer/src/lsp_ext.rs @@ -111,7 +111,6 @@ impl Request for ResolveCodeActionRequest { pub struct ResolveCodeActionParams { pub code_action_params: lsp_types::CodeActionParams, pub id: String, - pub label: String, } pub enum OnEnter {} diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 3c4064441..fab82ff7e 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -756,9 +756,13 @@ pub fn handle_code_action( handle_fixes(&world, ¶ms, &mut res)?; if world.config.client_caps.resolve_code_action { - for assist in world.analysis().unresolved_assists(&world.config.assist, frange)?.into_iter() + for (index, assist) in world + .analysis() + .unresolved_assists(&world.config.assist, frange)? + .into_iter() + .enumerate() { - res.push(to_proto::unresolved_code_action(&world, assist)?); + res.push(to_proto::unresolved_code_action(&world, assist, index)?); } } else { for assist in world.analysis().resolved_assists(&world.config.assist, frange)?.into_iter() { @@ -773,24 +777,19 @@ pub fn handle_resolve_code_action( world: WorldSnapshot, params: lsp_ext::ResolveCodeActionParams, ) -> Result> { - if !world.config.client_caps.resolve_code_action { - return Ok(None); - } - let _p = profile("handle_resolve_code_action"); let file_id = from_proto::file_id(&world, ¶ms.code_action_params.text_document.uri)?; let line_index = world.analysis().file_line_index(file_id)?; let range = from_proto::text_range(&line_index, params.code_action_params.range); let frange = FileRange { file_id, range }; - let mut res: Vec = Vec::new(); - for assist in world.analysis().resolved_assists(&world.config.assist, frange)?.into_iter() { - res.push(to_proto::resolved_code_action(&world, assist)?); - } - Ok(res - .into_iter() - .find(|action| action.id.clone().unwrap() == params.id && action.title == params.label) - .and_then(|action| action.edit)) + let assists = world.analysis().resolved_assists(&world.config.assist, frange)?; + let id_components = params.id.split(":").collect::>(); + let index = id_components.last().unwrap().parse::().unwrap(); + let id_string = id_components.first().unwrap(); + let assist = &assists[index]; + assert!(assist.assist.id.0 == *id_string); + Ok(to_proto::resolved_code_action(&world, assist.clone())?.edit) } pub fn handle_code_lens( diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 3672b1a26..fb33bdd5f 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -622,17 +622,12 @@ fn main() { pub(crate) fn unresolved_code_action( world: &WorldSnapshot, assist: Assist, + index: usize, ) -> Result { let res = lsp_ext::CodeAction { title: assist.label, - id: Some(assist.id.0.to_owned()), - group: assist.group.and_then(|it| { - if world.config.client_caps.code_action_group { - None - } else { - Some(it.0) - } - }), + id: Some(format!("{}:{}", assist.id.0.to_owned(), index.to_string())), + group: assist.group.filter(|_| world.config.client_caps.code_action_group).map(|gr| gr.0), kind: Some(String::new()), edit: None, command: None, @@ -644,21 +639,14 @@ pub(crate) fn resolved_code_action( world: &WorldSnapshot, assist: ResolvedAssist, ) -> Result { - let res = lsp_ext::CodeAction { - title: assist.assist.label, - id: Some(assist.assist.id.0.to_owned()), - group: assist.assist.group.and_then(|it| { - if world.config.client_caps.code_action_group { - None - } else { - Some(it.0) - } - }), - kind: Some(String::new()), - edit: Some(snippet_workspace_edit(world, assist.source_change)?), - command: None, - }; - Ok(res) + let change = assist.source_change; + unresolved_code_action(world, assist.assist, 0).and_then(|it| { + Ok(lsp_ext::CodeAction { + id: None, + edit: Some(snippet_workspace_edit(world, change)?), + ..it + }) + }) } pub(crate) fn runnable( diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index a25091f79..40ad1e3cd 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -61,8 +61,6 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient const id = (item as any).id; const resolveParams: ra.ResolveCodeActionParams = { id: id, - // TODO: delete after discussions if needed - label: item.title, codeActionParams: params }; action.command = { diff --git a/editors/code/src/lsp_ext.ts b/editors/code/src/lsp_ext.ts index 35d73ce31..9793b926c 100644 --- a/editors/code/src/lsp_ext.ts +++ b/editors/code/src/lsp_ext.ts @@ -35,7 +35,6 @@ export const parentModule = new lc.RequestType('experimental/resolveCodeAction'); -- cgit v1.2.3