mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 23:24:29 +00:00
Snippetify introduce/inline var
This commit is contained in:
parent
65fa586410
commit
fd77170718
4 changed files with 90 additions and 77 deletions
|
@ -116,7 +116,6 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
|
|||
let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() };
|
||||
builder.replace(desc.file_range.range, replacement)
|
||||
}
|
||||
builder.set_cursor(delete_range.start())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -149,7 +148,7 @@ fn foo() {
|
|||
r"
|
||||
fn bar(a: usize) {}
|
||||
fn foo() {
|
||||
<|>1 + 1;
|
||||
1 + 1;
|
||||
if 1 > 10 {
|
||||
}
|
||||
|
||||
|
@ -183,7 +182,7 @@ fn foo() {
|
|||
r"
|
||||
fn bar(a: usize) {}
|
||||
fn foo() {
|
||||
<|>(1 + 1) + 1;
|
||||
(1 + 1) + 1;
|
||||
if (1 + 1) > 10 {
|
||||
}
|
||||
|
||||
|
@ -217,7 +216,7 @@ fn foo() {
|
|||
r"
|
||||
fn bar(a: usize) {}
|
||||
fn foo() {
|
||||
<|>bar(1) + 1;
|
||||
bar(1) + 1;
|
||||
if bar(1) > 10 {
|
||||
}
|
||||
|
||||
|
@ -251,7 +250,7 @@ fn foo() {
|
|||
r"
|
||||
fn bar(a: usize): usize { a }
|
||||
fn foo() {
|
||||
<|>(bar(1) as u64) + 1;
|
||||
(bar(1) as u64) + 1;
|
||||
if (bar(1) as u64) > 10 {
|
||||
}
|
||||
|
||||
|
@ -283,7 +282,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>{ 10 + 1 } + 1;
|
||||
{ 10 + 1 } + 1;
|
||||
if { 10 + 1 } > 10 {
|
||||
}
|
||||
|
||||
|
@ -315,7 +314,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>( 10 + 1 ) + 1;
|
||||
( 10 + 1 ) + 1;
|
||||
if ( 10 + 1 ) > 10 {
|
||||
}
|
||||
|
||||
|
@ -353,7 +352,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>let b = bar(10 + 1) * 10;
|
||||
let b = bar(10 + 1) * 10;
|
||||
let c = bar(10 + 1) as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -373,7 +372,7 @@ fn foo() {
|
|||
r"
|
||||
fn foo() {
|
||||
let x = vec![1, 2, 3];
|
||||
<|>let b = x[0] * 10;
|
||||
let b = x[0] * 10;
|
||||
let c = x[0] as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -393,7 +392,7 @@ fn foo() {
|
|||
r"
|
||||
fn foo() {
|
||||
let bar = vec![1];
|
||||
<|>let b = bar.len() * 10;
|
||||
let b = bar.len() * 10;
|
||||
let c = bar.len() as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -421,7 +420,7 @@ struct Bar {
|
|||
|
||||
fn foo() {
|
||||
let bar = Bar { foo: 1 };
|
||||
<|>let b = bar.foo * 10;
|
||||
let b = bar.foo * 10;
|
||||
let c = bar.foo as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -442,7 +441,7 @@ fn foo() -> Option<usize> {
|
|||
r"
|
||||
fn foo() -> Option<usize> {
|
||||
let bar = Some(1);
|
||||
<|>let b = bar? * 10;
|
||||
let b = bar? * 10;
|
||||
let c = bar? as usize;
|
||||
None
|
||||
}",
|
||||
|
@ -462,7 +461,7 @@ fn foo() {
|
|||
r"
|
||||
fn foo() {
|
||||
let bar = 10;
|
||||
<|>let b = &bar * 10;
|
||||
let b = &bar * 10;
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
@ -478,7 +477,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>let b = (10, 20)[0];
|
||||
let b = (10, 20)[0];
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
@ -494,7 +493,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>let b = [1, 2, 3].len();
|
||||
let b = [1, 2, 3].len();
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
@ -511,7 +510,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>let b = (10 + 20) * 10;
|
||||
let b = (10 + 20) * 10;
|
||||
let c = (10 + 20) as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -531,7 +530,7 @@ fn foo() {
|
|||
r"
|
||||
fn foo() {
|
||||
let d = 10;
|
||||
<|>let b = d * 10;
|
||||
let b = d * 10;
|
||||
let c = d as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -549,7 +548,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>let b = { 10 } * 10;
|
||||
let b = { 10 } * 10;
|
||||
let c = { 10 } as usize;
|
||||
}",
|
||||
);
|
||||
|
@ -569,7 +568,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>let b = (10 + 20) * 10;
|
||||
let b = (10 + 20) * 10;
|
||||
let c = (10 + 20, 20);
|
||||
let d = [10 + 20, 10];
|
||||
let e = (10 + 20);
|
||||
|
@ -588,7 +587,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>for i in vec![10, 20] {}
|
||||
for i in vec![10, 20] {}
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
@ -604,7 +603,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>while 1 > 0 {}
|
||||
while 1 > 0 {}
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
@ -622,7 +621,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>loop {
|
||||
loop {
|
||||
break 1 + 1;
|
||||
}
|
||||
}",
|
||||
|
@ -640,7 +639,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>return 1 > 0;
|
||||
return 1 > 0;
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
@ -656,7 +655,7 @@ fn foo() {
|
|||
}",
|
||||
r"
|
||||
fn foo() {
|
||||
<|>match 1 > 0 {}
|
||||
match 1 > 0 {}
|
||||
}",
|
||||
);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use ra_syntax::{
|
|||
BLOCK_EXPR, BREAK_EXPR, COMMENT, LAMBDA_EXPR, LOOP_EXPR, MATCH_ARM, PATH_EXPR, RETURN_EXPR,
|
||||
WHITESPACE,
|
||||
},
|
||||
SyntaxNode, TextSize,
|
||||
SyntaxNode,
|
||||
};
|
||||
use stdx::format_to;
|
||||
use test_utils::mark;
|
||||
|
@ -23,7 +23,7 @@ use crate::{AssistContext, AssistId, Assists};
|
|||
// ->
|
||||
// ```
|
||||
// fn main() {
|
||||
// let var_name = (1 + 2);
|
||||
// let $0var_name = (1 + 2);
|
||||
// var_name * 4;
|
||||
// }
|
||||
// ```
|
||||
|
@ -46,14 +46,13 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
|||
acc.add(AssistId("introduce_variable"), "Extract into variable", target, move |edit| {
|
||||
let mut buf = String::new();
|
||||
|
||||
let cursor_offset = if wrap_in_block {
|
||||
if wrap_in_block {
|
||||
buf.push_str("{ let var_name = ");
|
||||
TextSize::of("{ let ")
|
||||
} else {
|
||||
buf.push_str("let var_name = ");
|
||||
TextSize::of("let ")
|
||||
};
|
||||
format_to!(buf, "{}", expr.syntax());
|
||||
|
||||
let full_stmt = ast::ExprStmt::cast(anchor_stmt.clone());
|
||||
let is_full_stmt = if let Some(expr_stmt) = &full_stmt {
|
||||
Some(expr.syntax().clone()) == expr_stmt.expr().map(|e| e.syntax().clone())
|
||||
|
@ -65,28 +64,43 @@ pub(crate) fn introduce_variable(acc: &mut Assists, ctx: &AssistContext) -> Opti
|
|||
if full_stmt.unwrap().semicolon_token().is_none() {
|
||||
buf.push_str(";");
|
||||
}
|
||||
edit.replace(expr.syntax().text_range(), buf);
|
||||
} else {
|
||||
buf.push_str(";");
|
||||
|
||||
// We want to maintain the indent level,
|
||||
// but we do not want to duplicate possible
|
||||
// extra newlines in the indent block
|
||||
let text = indent.text();
|
||||
if text.starts_with('\n') {
|
||||
buf.push_str("\n");
|
||||
buf.push_str(text.trim_start_matches('\n'));
|
||||
} else {
|
||||
buf.push_str(text);
|
||||
}
|
||||
|
||||
edit.replace(expr.syntax().text_range(), "var_name".to_string());
|
||||
edit.insert(anchor_stmt.text_range().start(), buf);
|
||||
if wrap_in_block {
|
||||
edit.insert(anchor_stmt.text_range().end(), " }");
|
||||
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)
|
||||
}
|
||||
None => edit.replace(offset, buf),
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
buf.push_str(";");
|
||||
|
||||
// We want to maintain the indent level,
|
||||
// but we do not want to duplicate possible
|
||||
// extra newlines in the indent block
|
||||
let text = indent.text();
|
||||
if text.starts_with('\n') {
|
||||
buf.push_str("\n");
|
||||
buf.push_str(text.trim_start_matches('\n'));
|
||||
} else {
|
||||
buf.push_str(text);
|
||||
}
|
||||
|
||||
edit.replace(expr.syntax().text_range(), "var_name".to_string());
|
||||
let offset = anchor_stmt.text_range().start();
|
||||
match ctx.config.snippet_cap {
|
||||
Some(cap) => {
|
||||
let snip = buf.replace("let var_name", "let $0var_name");
|
||||
edit.insert_snippet(cap, offset, snip)
|
||||
}
|
||||
None => edit.insert(offset, buf),
|
||||
}
|
||||
|
||||
if wrap_in_block {
|
||||
edit.insert(anchor_stmt.text_range().end(), " }");
|
||||
}
|
||||
edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset);
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -144,15 +158,15 @@ mod tests {
|
|||
fn test_introduce_var_simple() {
|
||||
check_assist(
|
||||
introduce_variable,
|
||||
"
|
||||
r#"
|
||||
fn foo() {
|
||||
foo(<|>1 + 1<|>);
|
||||
}",
|
||||
"
|
||||
}"#,
|
||||
r#"
|
||||
fn foo() {
|
||||
let <|>var_name = 1 + 1;
|
||||
let $0var_name = 1 + 1;
|
||||
foo(var_name);
|
||||
}",
|
||||
}"#,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -167,14 +181,14 @@ fn foo() {
|
|||
mark::check!(test_introduce_var_expr_stmt);
|
||||
check_assist(
|
||||
introduce_variable,
|
||||
"
|
||||
r#"
|
||||
fn foo() {
|
||||
<|>1 + 1<|>;
|
||||
}",
|
||||
"
|
||||
}"#,
|
||||
r#"
|
||||
fn foo() {
|
||||
let <|>var_name = 1 + 1;
|
||||
}",
|
||||
let $0var_name = 1 + 1;
|
||||
}"#,
|
||||
);
|
||||
check_assist(
|
||||
introduce_variable,
|
||||
|
@ -185,7 +199,7 @@ fn foo() {
|
|||
}",
|
||||
"
|
||||
fn foo() {
|
||||
let <|>var_name = { let x = 0; x };
|
||||
let $0var_name = { let x = 0; x };
|
||||
something_else();
|
||||
}",
|
||||
);
|
||||
|
@ -201,7 +215,7 @@ fn foo() {
|
|||
}",
|
||||
"
|
||||
fn foo() {
|
||||
let <|>var_name = 1;
|
||||
let $0var_name = 1;
|
||||
var_name + 1;
|
||||
}",
|
||||
);
|
||||
|
@ -218,7 +232,7 @@ fn foo() {
|
|||
}",
|
||||
"
|
||||
fn foo() {
|
||||
let <|>var_name = 1 + 1;
|
||||
let $0var_name = 1 + 1;
|
||||
bar(var_name)
|
||||
}",
|
||||
);
|
||||
|
@ -230,7 +244,7 @@ fn foo() {
|
|||
}",
|
||||
"
|
||||
fn foo() {
|
||||
let <|>var_name = bar(1 + 1);
|
||||
let $0var_name = bar(1 + 1);
|
||||
var_name
|
||||
}",
|
||||
)
|
||||
|
@ -253,7 +267,7 @@ fn main() {
|
|||
fn main() {
|
||||
let x = true;
|
||||
let tuple = match x {
|
||||
true => { let <|>var_name = 2 + 2; (var_name, true) }
|
||||
true => { let $0var_name = 2 + 2; (var_name, true) }
|
||||
_ => (0, false)
|
||||
};
|
||||
}
|
||||
|
@ -283,7 +297,7 @@ fn main() {
|
|||
let tuple = match x {
|
||||
true => {
|
||||
let y = 1;
|
||||
let <|>var_name = 2 + y;
|
||||
let $0var_name = 2 + y;
|
||||
(var_name, true)
|
||||
}
|
||||
_ => (0, false)
|
||||
|
@ -304,7 +318,7 @@ fn main() {
|
|||
",
|
||||
"
|
||||
fn main() {
|
||||
let lambda = |x: u32| { let <|>var_name = x * 2; var_name };
|
||||
let lambda = |x: u32| { let $0var_name = x * 2; var_name };
|
||||
}
|
||||
",
|
||||
);
|
||||
|
@ -321,7 +335,7 @@ fn main() {
|
|||
",
|
||||
"
|
||||
fn main() {
|
||||
let lambda = |x: u32| { let <|>var_name = x * 2; var_name };
|
||||
let lambda = |x: u32| { let $0var_name = x * 2; var_name };
|
||||
}
|
||||
",
|
||||
);
|
||||
|
@ -338,7 +352,7 @@ fn main() {
|
|||
",
|
||||
"
|
||||
fn main() {
|
||||
let <|>var_name = Some(true);
|
||||
let $0var_name = Some(true);
|
||||
let o = var_name;
|
||||
}
|
||||
",
|
||||
|
@ -356,7 +370,7 @@ fn main() {
|
|||
",
|
||||
"
|
||||
fn main() {
|
||||
let <|>var_name = bar.foo();
|
||||
let $0var_name = bar.foo();
|
||||
let v = var_name;
|
||||
}
|
||||
",
|
||||
|
@ -374,7 +388,7 @@ fn foo() -> u32 {
|
|||
",
|
||||
"
|
||||
fn foo() -> u32 {
|
||||
let <|>var_name = 2 + 2;
|
||||
let $0var_name = 2 + 2;
|
||||
return var_name;
|
||||
}
|
||||
",
|
||||
|
@ -396,7 +410,7 @@ fn foo() -> u32 {
|
|||
fn foo() -> u32 {
|
||||
|
||||
|
||||
let <|>var_name = 2 + 2;
|
||||
let $0var_name = 2 + 2;
|
||||
return var_name;
|
||||
}
|
||||
",
|
||||
|
@ -413,7 +427,7 @@ fn foo() -> u32 {
|
|||
"
|
||||
fn foo() -> u32 {
|
||||
|
||||
let <|>var_name = 2 + 2;
|
||||
let $0var_name = 2 + 2;
|
||||
return var_name;
|
||||
}
|
||||
",
|
||||
|
@ -438,7 +452,7 @@ fn foo() -> u32 {
|
|||
// bar
|
||||
|
||||
|
||||
let <|>var_name = 2 + 2;
|
||||
let $0var_name = 2 + 2;
|
||||
return var_name;
|
||||
}
|
||||
",
|
||||
|
@ -459,7 +473,7 @@ fn main() {
|
|||
"
|
||||
fn main() {
|
||||
let result = loop {
|
||||
let <|>var_name = 2 + 2;
|
||||
let $0var_name = 2 + 2;
|
||||
break var_name;
|
||||
};
|
||||
}
|
||||
|
@ -478,7 +492,7 @@ fn main() {
|
|||
",
|
||||
"
|
||||
fn main() {
|
||||
let <|>var_name = 0f32 as u32;
|
||||
let $0var_name = 0f32 as u32;
|
||||
let v = var_name;
|
||||
}
|
||||
",
|
||||
|
|
|
@ -443,7 +443,7 @@ fn main() {
|
|||
"#####,
|
||||
r#####"
|
||||
fn main() {
|
||||
let var_name = (1 + 2);
|
||||
let $0var_name = (1 + 2);
|
||||
var_name * 4;
|
||||
}
|
||||
"#####,
|
||||
|
|
|
@ -426,7 +426,7 @@ fn main() {
|
|||
|
||||
// AFTER
|
||||
fn main() {
|
||||
let var_name = (1 + 2);
|
||||
let $0var_name = (1 + 2);
|
||||
var_name * 4;
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue