mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-15 14:43:58 +00:00
Auto merge of #12788 - hasali19:extract-var-mut, r=jonas-schievink
Fix extract variable assist for subexpression in mutable borrow This checks if the expression is in a mutable borrow and if so makes the extracted variable `mut`. Closes #12786
This commit is contained in:
commit
e2eaa99ca1
1 changed files with 31 additions and 5 deletions
|
@ -58,6 +58,12 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
||||||
_ => "",
|
_ => "",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let parent_ref_expr = to_extract.syntax().parent().and_then(ast::RefExpr::cast);
|
||||||
|
let var_modifier = match parent_ref_expr {
|
||||||
|
Some(expr) if expr.mut_token().is_some() => "mut ",
|
||||||
|
_ => "",
|
||||||
|
};
|
||||||
|
|
||||||
let anchor = Anchor::from(&to_extract)?;
|
let anchor = Anchor::from(&to_extract)?;
|
||||||
let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone();
|
let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone();
|
||||||
let target = to_extract.syntax().text_range();
|
let target = to_extract.syntax().text_range();
|
||||||
|
@ -85,7 +91,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
||||||
|
|
||||||
match anchor {
|
match anchor {
|
||||||
Anchor::Before(_) | Anchor::Replace(_) => {
|
Anchor::Before(_) | Anchor::Replace(_) => {
|
||||||
format_to!(buf, "let {} = {}", var_name, reference_modifier)
|
format_to!(buf, "let {}{} = {}", var_modifier, var_name, reference_modifier)
|
||||||
}
|
}
|
||||||
Anchor::WrapInBlock(_) => {
|
Anchor::WrapInBlock(_) => {
|
||||||
format_to!(buf, "{{ let {} = {}", var_name, reference_modifier)
|
format_to!(buf, "{{ let {} = {}", var_name, reference_modifier)
|
||||||
|
@ -100,8 +106,10 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
||||||
}
|
}
|
||||||
match ctx.config.snippet_cap {
|
match ctx.config.snippet_cap {
|
||||||
Some(cap) => {
|
Some(cap) => {
|
||||||
let snip = buf
|
let snip = buf.replace(
|
||||||
.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
|
&format!("let {}{}", var_modifier, var_name),
|
||||||
|
&format!("let {}$0{}", var_modifier, var_name),
|
||||||
|
);
|
||||||
edit.replace_snippet(cap, expr_range, snip)
|
edit.replace_snippet(cap, expr_range, snip)
|
||||||
}
|
}
|
||||||
None => edit.replace(expr_range, buf),
|
None => edit.replace(expr_range, buf),
|
||||||
|
@ -126,8 +134,10 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option
|
||||||
let offset = anchor.syntax().text_range().start();
|
let offset = anchor.syntax().text_range().start();
|
||||||
match ctx.config.snippet_cap {
|
match ctx.config.snippet_cap {
|
||||||
Some(cap) => {
|
Some(cap) => {
|
||||||
let snip =
|
let snip = buf.replace(
|
||||||
buf.replace(&format!("let {}", var_name), &format!("let $0{}", var_name));
|
&format!("let {}{}", var_modifier, var_name),
|
||||||
|
&format!("let {}$0{}", var_modifier, var_name),
|
||||||
|
);
|
||||||
edit.insert_snippet(cap, offset, snip)
|
edit.insert_snippet(cap, offset, snip)
|
||||||
}
|
}
|
||||||
None => edit.insert(offset, buf),
|
None => edit.insert(offset, buf),
|
||||||
|
@ -1247,6 +1257,22 @@ fn foo() {
|
||||||
let local = &S::new();
|
let local = &S::new();
|
||||||
let $0x = &local.sub;
|
let $0x = &local.sub;
|
||||||
x.do_thing();
|
x.do_thing();
|
||||||
|
}"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_extract_var_for_mutable_borrow() {
|
||||||
|
check_assist(
|
||||||
|
extract_variable,
|
||||||
|
r#"
|
||||||
|
fn foo() {
|
||||||
|
let v = &mut $00$0;
|
||||||
|
}"#,
|
||||||
|
r#"
|
||||||
|
fn foo() {
|
||||||
|
let mut $0var_name = 0;
|
||||||
|
let v = &mut var_name;
|
||||||
}"#,
|
}"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue