Skip to content

Commit 5f08896

Browse files
bors[bot]matklad
andauthored
Merge #5069
5069: DWIM introduce variable r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 6eb0349 + 5f6f994 commit 5f08896

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

crates/ra_assists/src/handlers/introduce_variable.rs

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,26 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
4444
}
4545
let target = expr.syntax().text_range();
4646
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+
4752
let mut buf = String::new();
4853

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+
4963
if wrap_in_block {
50-
buf.push_str("{ let var_name = ");
64+
format_to!(buf, "{{ let {} = ", var_name);
5165
} else {
52-
buf.push_str("let var_name = ");
66+
format_to!(buf, "let {} = ", var_name);
5367
};
5468
format_to!(buf, "{}", expr.syntax());
5569

@@ -64,13 +78,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
6478
if full_stmt.unwrap().semicolon_token().is_none() {
6579
buf.push_str(";");
6680
}
67-
let offset = expr.syntax().text_range();
6881
match ctx.config.snippet_cap {
6982
Some(cap) => {
70-
let snip = buf.replace("let var_name", "let $0var_name");
71-
edit.replace_snippet(cap, offset, snip)
83+
let snip =
84+
buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
85+
edit.replace_snippet(cap, expr_range, snip)
7286
}
73-
None => edit.replace(offset, buf),
87+
None => edit.replace(expr_range, buf),
7488
}
7589
return;
7690
}
@@ -88,11 +102,12 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
88102
buf.push_str(text);
89103
}
90104

91-
edit.replace(expr.syntax().text_range(), "var_name".to_string());
105+
edit.replace(expr_range, var_name.clone());
92106
let offset = anchor_stmt.text_range().start();
93107
match ctx.config.snippet_cap {
94108
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));
96111
edit.insert_snippet(cap, offset, snip)
97112
}
98113
None => edit.insert(offset, buf),
@@ -503,6 +518,32 @@ fn main() {
503518
);
504519
}
505520

521+
#[test]
522+
fn introduce_var_field_shorthand() {
523+
check_assist(
524+
introduce_variable,
525+
r#"
526+
struct S {
527+
foo: i32
528+
}
529+
530+
fn main() {
531+
S { foo: <|>1 + 1<|> }
532+
}
533+
"#,
534+
r#"
535+
struct S {
536+
foo: i32
537+
}
538+
539+
fn main() {
540+
let $0foo = 1 + 1;
541+
S { foo }
542+
}
543+
"#,
544+
)
545+
}
546+
506547
#[test]
507548
fn test_introduce_var_for_return_not_applicable() {
508549
check_assist_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } ");

crates/rust-analyzer/src/global_state.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,7 @@ pub(crate) struct GlobalStateSnapshot {
8484
}
8585

8686
impl GlobalState {
87-
pub(crate) fn new(
88-
sender: Sender<lsp_server::Message>,
89-
lru_capacity: Option<usize>,
90-
config: Config,
91-
) -> GlobalState {
87+
pub(crate) fn new(sender: Sender<lsp_server::Message>, config: Config) -> GlobalState {
9288
let loader = {
9389
let (sender, receiver) = unbounded::<vfs::loader::Message>();
9490
let handle =
@@ -103,12 +99,13 @@ impl GlobalState {
10399
Handle { handle, receiver }
104100
};
105101

102+
let analysis_host = AnalysisHost::new(config.lru_capacity);
106103
GlobalState {
107104
sender,
108105
task_pool,
109106
loader,
110107
config,
111-
analysis_host: AnalysisHost::new(lru_capacity),
108+
analysis_host,
112109
flycheck: None,
113110
diagnostics: Default::default(),
114111
mem_docs: FxHashSet::default(),

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> {
4444
SetThreadPriority(thread, thread_priority_above_normal);
4545
}
4646

47-
GlobalState::new(connection.sender.clone(), config.lru_capacity, config)
48-
.run(connection.receiver)
47+
GlobalState::new(connection.sender.clone(), config).run(connection.receiver)
4948
}
5049

5150
enum Event {

0 commit comments

Comments
 (0)