aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-06-25 23:55:26 +0100
committerGitHub <[email protected]>2020-06-25 23:55:26 +0100
commit5f0889642401ac3da7be29b8122d04dbf65b1292 (patch)
treee7cf891ed12ec26e9dc17e774a0c71091dc0eae5 /crates
parent6eb0349a7b2dd71eae6f1541cdd26e1ac50363d0 (diff)
parent5f6f994256a4a91f1caba9cabfd7a4552180a9da (diff)
Merge #5069
5069: DWIM introduce variable r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_assists/src/handlers/introduce_variable.rs57
-rw-r--r--crates/rust-analyzer/src/global_state.rs9
-rw-r--r--crates/rust-analyzer/src/main_loop.rs3
3 files changed, 53 insertions, 16 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
44 } 44 }
45 let target = expr.syntax().text_range(); 45 let target = expr.syntax().text_range();
46 acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| { 46 acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| {
47 let field_shorthand = match expr.syntax().parent().and_then(ast::RecordField::cast) {
48 Some(field) => field.name_ref(),
49 None => None,
50 };
51
47 let mut buf = String::new(); 52 let mut buf = String::new();
48 53
54 let var_name = match &field_shorthand {
55 Some(it) => it.to_string(),
56 None => "var_name".to_string(),
57 };
58 let expr_range = match &field_shorthand {
59 Some(it) => it.syntax().text_range().cover(expr.syntax().text_range()),
60 None => expr.syntax().text_range(),
61 };
62
49 if wrap_in_block { 63 if wrap_in_block {
50 buf.push_str("{ let var_name = "); 64 format_to!(buf, "{{ let {} = ", var_name);
51 } else { 65 } else {
52 buf.push_str("let var_name = "); 66 format_to!(buf, "let {} = ", var_name);
53 }; 67 };
54 format_to!(buf, "{}", expr.syntax()); 68 format_to!(buf, "{}", expr.syntax());
55 69
@@ -64,13 +78,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
64 if full_stmt.unwrap().semicolon_token().is_none() { 78 if full_stmt.unwrap().semicolon_token().is_none() {
65 buf.push_str(";"); 79 buf.push_str(";");
66 } 80 }
67 let offset = expr.syntax().text_range();
68 match ctx.config.snippet_cap { 81 match ctx.config.snippet_cap {
69 Some(cap) => { 82 Some(cap) => {
70 let snip = buf.replace("let var_name", "let $0var_name"); 83 let snip =
71 edit.replace_snippet(cap, offset, snip) 84 buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
85 edit.replace_snippet(cap, expr_range, snip)
72 } 86 }
73 None => edit.replace(offset, buf), 87 None => edit.replace(expr_range, buf),
74 } 88 }
75 return; 89 return;
76 } 90 }
@@ -88,11 +102,12 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
88 buf.push_str(text); 102 buf.push_str(text);
89 } 103 }
90 104
91 edit.replace(expr.syntax().text_range(), "var_name".to_string()); 105 edit.replace(expr_range, var_name.clone());
92 let offset = anchor_stmt.text_range().start(); 106 let offset = anchor_stmt.text_range().start();
93 match ctx.config.snippet_cap { 107 match ctx.config.snippet_cap {
94 Some(cap) => { 108 Some(cap) => {
95 let snip = buf.replace("let var_name", "let $0var_name"); 109 let snip =
110 buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
96 edit.insert_snippet(cap, offset, snip) 111 edit.insert_snippet(cap, offset, snip)
97 } 112 }
98 None => edit.insert(offset, buf), 113 None => edit.insert(offset, buf),
@@ -504,6 +519,32 @@ fn main() {
504 } 519 }
505 520
506 #[test] 521 #[test]
522 fn introduce_var_field_shorthand() {
523 check_assist(
524 introduce_variable,
525 r#"
526struct S {
527 foo: i32
528}
529
530fn main() {
531 S { foo: <|>1 + 1<|> }
532}
533"#,
534 r#"
535struct S {
536 foo: i32
537}
538
539fn main() {
540 let $0foo = 1 + 1;
541 S { foo }
542}
543"#,
544 )
545 }
546
547 #[test]
507 fn test_introduce_var_for_return_not_applicable() { 548 fn test_introduce_var_for_return_not_applicable() {
508 check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } "); 549 check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");
509 } 550 }
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 {
84} 84}
85 85
86impl GlobalState { 86impl GlobalState {
87 pub(crate) fn new( 87 pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
88 sender: Sender<lsp_server::Message>,
89 lru_capacity: Option<usize>,
90 config: Config,
91 ) -> GlobalState {
92 let loader = { 88 let loader = {
93 let (sender, receiver) = unbounded::<vfs::loader::Message>(); 89 let (sender, receiver) = unbounded::<vfs::loader::Message>();
94 let handle = 90 let handle =
@@ -103,12 +99,13 @@ impl GlobalState {
103 Handle { handle, receiver } 99 Handle { handle, receiver }
104 }; 100 };
105 101
102 let analysis_host = AnalysisHost::new(config.lru_capacity);
106 GlobalState { 103 GlobalState {
107 sender, 104 sender,
108 task_pool, 105 task_pool,
109 loader, 106 loader,
110 config, 107 config,
111 analysis_host: AnalysisHost::new(lru_capacity), 108 analysis_host,
112 flycheck: None, 109 flycheck: None,
113 diagnostics: Default::default(), 110 diagnostics: Default::default(),
114 mem_docs: FxHashSet::default(), 111 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<()> {
44 SetThreadPriority(thread, thread_priority_above_normal); 44 SetThreadPriority(thread, thread_priority_above_normal);
45 } 45 }
46 46
47 GlobalState::new(connection.sender.clone(), config.lru_capacity, config) 47 GlobalState::new(connection.sender.clone(), config).run(connection.receiver)
48 .run(connection.receiver)
49} 48}
50 49
51enum Event { 50enum Event {