From dd5841556c97e4f42d20393cfc0543973742833c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 26 Jun 2020 00:51:34 +0200 Subject: Smarter introduce variable Use field init shorthand --- .../ra_assists/src/handlers/introduce_variable.rs | 57 +++++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index 88b62278f..96affe49d 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs @@ -44,12 +44,26 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti } let target = expr.syntax().text_range(); acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| { + let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) { + Some(field) => field.name_ref(), + None => None, + }; + let mut buf = String::new(); + let var_name = match &field_shorthand { + Some(it) => it.to_string(), + None => "var_name".to_string(), + }; + let expr_range = match &field_shorthand { + Some(it) => it.syntax().text_range().cover(expr.syntax().text_range()), + None => expr.syntax().text_range(), + }; + if wrap_in_block { - buf.push_str("{ let var_name = "); + format_to!(buf, "{{ let {} = ", var_name); } else { - buf.push_str("let var_name = "); + format_to!(buf, "let {} = ", var_name); }; format_to!(buf, "{}", expr.syntax()); @@ -64,13 +78,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti if full_stmt.unwrap().semicolon_token().is_none() { buf.push_str(";"); } - let offset = expr.syntax().text_range(); match ctx.config.snippet_cap { Some(cap) => { - let snip = buf.replace("let var_name", "let $0var_name"); - edit.replace_snippet(cap, offset, snip) + let snip = + buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name)); + edit.replace_snippet(cap, expr_range, snip) } - None => edit.replace(offset, buf), + None => edit.replace(expr_range, buf), } return; } @@ -88,11 +102,12 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti buf.push_str(text); } - edit.replace(expr.syntax().text_range(), "var_name".to_string()); + edit.replace(expr_range, var_name.clone()); let offset = anchor_stmt.text_range().start(); match ctx.config.snippet_cap { Some(cap) => { - let snip = buf.replace("let var_name", "let $0var_name"); + let snip = + buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name)); edit.insert_snippet(cap, offset, snip) } None => edit.insert(offset, buf), @@ -503,6 +518,32 @@ fn main() { ); } + #[test] + fn introduce_var_field_shorthand() { + check_assist( + introduce_variable, + r#" +struct S { + foo: i32 +} + +fn main() { + S { foo: <|>1 + 1<|> } +} +"#, + r#" +struct S { + foo: i32 +} + +fn main() { + let $0foo = 1 + 1; + S { foo } +} +"#, + ) + } + #[test] fn test_introduce_var_for_return_not_applicable() { check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } "); -- cgit v1.2.3 From 5f6f994256a4a91f1caba9cabfd7a4552180a9da Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 26 Jun 2020 00:54:41 +0200 Subject: Simplify --- crates/rust-analyzer/src/global_state.rs | 9 +++------ crates/rust-analyzer/src/main_loop.rs | 3 +-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 17de2a075..a8cc71249 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -84,11 +84,7 @@ pub(crate) struct GlobalStateSnapshot { } impl GlobalState { - pub(crate) fn new( - sender: Sender, - lru_capacity: Option, - config: Config, - ) -> GlobalState { + pub(crate) fn new(sender: Sender, config: Config) -> GlobalState { let loader = { let (sender, receiver) = unbounded::(); let handle = @@ -103,12 +99,13 @@ impl GlobalState { Handle { handle, receiver } }; + let analysis_host = AnalysisHost::new(config.lru_capacity); GlobalState { sender, task_pool, loader, config, - analysis_host: AnalysisHost::new(lru_capacity), + analysis_host, flycheck: None, diagnostics: Default::default(), mem_docs: FxHashSet::default(), diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index d4879283d..d03c68edf 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -44,8 +44,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> { SetThreadPriority(thread, thread_priority_above_normal); } - GlobalState::new(connection.sender.clone(), config.lru_capacity, config) - .run(connection.receiver) + GlobalState::new(connection.sender.clone(), config).run(connection.receiver) } enum Event { -- cgit v1.2.3