rust/crates/ra_assists/src/introduce_variable.rs

433 lines
8.8 KiB
Rust
Raw Normal View History

2019-02-03 12:26:35 -06:00
use hir::db::HirDatabase;
2019-01-03 06:08:32 -06:00
use ra_syntax::{
ast::{self, AstNode},
SyntaxKind::{
WHITESPACE, MATCH_ARM, LAMBDA_EXPR, PATH_EXPR, BREAK_EXPR, LOOP_EXPR, RETURN_EXPR, COMMENT
}, SyntaxNode, TextUnit,
2019-01-03 06:08:32 -06:00
};
2019-02-03 12:26:35 -06:00
use crate::{AssistCtx, Assist};
2019-01-03 06:08:32 -06:00
2019-02-03 12:26:35 -06:00
pub(crate) fn introduce_variable<'a>(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
2019-01-03 09:59:17 -06:00
let node = ctx.covering_node();
if !valid_covering_node(node) {
return None;
}
let expr = node.ancestors().filter_map(valid_target_expr).next()?;
2019-01-28 08:12:07 -06:00
let (anchor_stmt, wrap_in_block) = anchor_stmt(expr)?;
2019-01-03 06:08:32 -06:00
let indent = anchor_stmt.prev_sibling()?;
if indent.kind() != WHITESPACE {
return None;
}
2019-01-03 09:59:17 -06:00
ctx.build("introduce variable", move |edit| {
2019-01-03 06:08:32 -06:00
let mut buf = String::new();
2019-01-28 08:12:07 -06:00
let cursor_offset = if wrap_in_block {
buf.push_str("{ let var_name = ");
TextUnit::of_str("{ let ")
} else {
buf.push_str("let var_name = ");
TextUnit::of_str("let ")
};
2019-01-03 06:08:32 -06:00
expr.syntax().text().push_to(&mut buf);
2019-01-15 22:27:15 -06:00
let full_stmt = ast::ExprStmt::cast(anchor_stmt);
let is_full_stmt = if let Some(expr_stmt) = full_stmt {
2019-01-03 06:08:32 -06:00
Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
} else {
false
};
if is_full_stmt {
2019-01-15 22:27:15 -06:00
if !full_stmt.unwrap().has_semi() {
buf.push_str(";");
}
2019-01-03 06:08:32 -06:00
edit.replace(expr.syntax().range(), buf);
} else {
buf.push_str(";");
indent.text().push_to(&mut buf);
edit.replace(expr.syntax().range(), "var_name".to_string());
edit.insert(anchor_stmt.range().start(), buf);
2019-01-28 08:12:07 -06:00
if wrap_in_block {
edit.insert(anchor_stmt.range().end(), " }");
}
2019-01-03 06:08:32 -06:00
}
2019-01-28 08:12:07 -06:00
edit.set_cursor(anchor_stmt.range().start() + cursor_offset);
2019-01-03 09:59:17 -06:00
})
}
2019-01-03 06:08:32 -06:00
fn valid_covering_node(node: &SyntaxNode) -> bool {
node.kind() != COMMENT
}
/// Check wether the node is a valid expression which can be extracted to a variable.
/// In general that's true for any expression, but in some cases that would produce invalid code.
fn valid_target_expr(node: &SyntaxNode) -> Option<&ast::Expr> {
return match node.kind() {
PATH_EXPR => None,
BREAK_EXPR => ast::BreakExpr::cast(node).and_then(|e| e.expr()),
RETURN_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
LOOP_EXPR => ast::ReturnExpr::cast(node).and_then(|e| e.expr()),
_ => ast::Expr::cast(node),
};
}
2019-01-28 08:12:07 -06:00
/// Returns the syntax node which will follow the freshly introduced var
/// and a boolean indicating whether we have to wrap it within a { } block
/// to produce correct code.
/// It can be a statement, the last in a block expression or a wanna be block
/// expression like a lamba or match arm.
fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> {
expr.syntax().ancestors().find_map(|node| {
2019-01-03 09:59:17 -06:00
if ast::Stmt::cast(node).is_some() {
2019-01-28 08:12:07 -06:00
return Some((node, false));
2019-01-03 09:59:17 -06:00
}
2019-01-28 08:12:07 -06:00
2019-01-03 09:59:17 -06:00
if let Some(expr) = node
.parent()
.and_then(ast::Block::cast)
.and_then(|it| it.expr())
{
if expr.syntax() == node {
2019-01-28 08:12:07 -06:00
return Some((node, false));
2019-01-03 06:08:32 -06:00
}
2019-01-03 09:59:17 -06:00
}
2019-01-28 08:12:07 -06:00
if let Some(parent) = node.parent() {
if parent.kind() == MATCH_ARM || parent.kind() == LAMBDA_EXPR {
2019-01-28 08:12:07 -06:00
return Some((node, true));
}
}
None
2019-01-03 09:59:17 -06:00
})
2019-01-03 06:08:32 -06:00
}
#[cfg(test)]
mod tests {
use super::*;
2019-02-03 12:26:35 -06:00
use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_range};
2019-01-03 06:08:32 -06:00
#[test]
fn test_introduce_var_simple() {
2019-01-03 09:59:17 -06:00
check_assist_range(
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
foo(<|>1 + 1<|>);
}",
"
fn foo() {
let <|>var_name = 1 + 1;
foo(var_name);
}",
);
}
#[test]
fn test_introduce_var_expr_stmt() {
2019-01-03 09:59:17 -06:00
check_assist_range(
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>1 + 1<|>;
}",
"
fn foo() {
let <|>var_name = 1 + 1;
}",
);
}
#[test]
fn test_introduce_var_part_of_expr_stmt() {
2019-01-03 09:59:17 -06:00
check_assist_range(
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>1<|> + 1;
}",
"
fn foo() {
let <|>var_name = 1;
var_name + 1;
}",
);
}
#[test]
fn test_introduce_var_last_expr() {
2019-01-03 09:59:17 -06:00
check_assist_range(
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
bar(<|>1 + 1<|>)
}",
"
fn foo() {
let <|>var_name = 1 + 1;
bar(var_name)
}",
);
}
#[test]
fn test_introduce_var_last_full_expr() {
2019-01-03 09:59:17 -06:00
check_assist_range(
introduce_variable,
2019-01-03 06:08:32 -06:00
"
fn foo() {
<|>bar(1 + 1)<|>
}",
"
fn foo() {
let <|>var_name = bar(1 + 1);
var_name
}",
);
}
#[test]
fn test_introduce_var_block_expr_second_to_last() {
check_assist_range(
introduce_variable,
"
fn foo() {
<|>{ let x = 0; x }<|>
something_else();
}",
"
fn foo() {
let <|>var_name = { let x = 0; x };
something_else();
}",
);
}
2019-01-28 08:12:07 -06:00
#[test]
fn test_introduce_var_in_match_arm_no_block() {
check_assist_range(
introduce_variable,
"
fn main() {
let x = true;
let tuple = match x {
true => (<|>2 + 2<|>, true)
_ => (0, false)
};
}
",
"
fn main() {
let x = true;
let tuple = match x {
true => { let <|>var_name = 2 + 2; (var_name, true) }
_ => (0, false)
};
}
",
);
}
#[test]
fn test_introduce_var_in_match_arm_with_block() {
check_assist_range(
introduce_variable,
"
fn main() {
let x = true;
let tuple = match x {
true => {
let y = 1;
(<|>2 + y<|>, true)
}
_ => (0, false)
};
}
",
"
fn main() {
let x = true;
let tuple = match x {
true => {
let y = 1;
let <|>var_name = 2 + y;
(var_name, true)
}
_ => (0, false)
};
}
",
);
}
#[test]
fn test_introduce_var_in_closure_no_block() {
check_assist_range(
introduce_variable,
"
fn main() {
let lambda = |x: u32| <|>x * 2<|>;
}
",
"
fn main() {
let lambda = |x: u32| { let <|>var_name = x * 2; var_name };
}
",
);
}
#[test]
fn test_introduce_var_in_closure_with_block() {
check_assist_range(
introduce_variable,
"
fn main() {
let lambda = |x: u32| { <|>x * 2<|> };
}
",
"
fn main() {
let lambda = |x: u32| { let <|>var_name = x * 2; var_name };
}
",
);
}
#[test]
fn test_introduce_var_path_simple() {
check_assist(
introduce_variable,
"
fn main() {
let o = S<|>ome(true);
}
",
"
fn main() {
let <|>var_name = Some(true);
let o = var_name;
}
",
);
}
#[test]
fn test_introduce_var_path_method() {
check_assist(
introduce_variable,
"
fn main() {
let v = b<|>ar.foo();
}
",
"
fn main() {
let <|>var_name = bar.foo();
let v = var_name;
}
",
);
}
#[test]
fn test_introduce_var_return() {
check_assist(
introduce_variable,
"
fn foo() -> u32 {
r<|>eturn 2 + 2;
}
",
"
fn foo() -> u32 {
let <|>var_name = 2 + 2;
return var_name;
}
",
);
}
#[test]
fn test_introduce_var_break() {
check_assist(
introduce_variable,
"
fn main() {
let result = loop {
b<|>reak 2 + 2;
};
}
",
"
fn main() {
let result = loop {
let <|>var_name = 2 + 2;
break var_name;
};
}
",
);
}
#[test]
fn test_introduce_var_for_cast() {
check_assist(
introduce_variable,
"
fn main() {
let v = 0f32 a<|>s u32;
}
",
"
fn main() {
let <|>var_name = 0f32 as u32;
let v = var_name;
}
",
);
}
#[test]
fn test_introduce_var_for_return_not_applicable() {
check_assist_not_applicable(
introduce_variable,
"
fn foo() {
r<|>eturn;
}
",
);
}
#[test]
fn test_introduce_var_for_break_not_applicable() {
check_assist_not_applicable(
introduce_variable,
"
fn main() {
loop {
b<|>reak;
};
}
",
);
}
#[test]
fn test_introduce_var_in_comment_not_applicable() {
check_assist_not_applicable(
introduce_variable,
"
fn main() {
let x = true;
let tuple = match x {
// c<|>omment
true => (2 + 2, true)
_ => (0, false)
};
}
2019-01-28 08:12:07 -06:00
",
);
}
2019-01-03 06:08:32 -06:00
}