2019-01-03 06:08:32 -06:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode},
|
2019-01-03 09:59:17 -06:00
|
|
|
SyntaxKind::WHITESPACE,
|
2019-01-07 07:53:24 -06:00
|
|
|
SyntaxNode, TextUnit,
|
2019-01-03 06:08:32 -06:00
|
|
|
};
|
|
|
|
|
2019-01-03 09:59:17 -06:00
|
|
|
use crate::assists::{AssistCtx, Assist};
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-01-03 09:59:17 -06:00
|
|
|
pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> {
|
|
|
|
let node = ctx.covering_node();
|
2019-01-03 06:08:32 -06:00
|
|
|
let expr = node.ancestors().filter_map(ast::Expr::cast).next()?;
|
|
|
|
|
|
|
|
let anchor_stmt = anchor_stmt(expr)?;
|
|
|
|
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();
|
|
|
|
|
|
|
|
buf.push_str("let var_name = ");
|
|
|
|
expr.syntax().text().push_to(&mut buf);
|
|
|
|
let is_full_stmt = if let Some(expr_stmt) = ast::ExprStmt::cast(anchor_stmt) {
|
|
|
|
Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if is_full_stmt {
|
|
|
|
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-03 09:59:17 -06:00
|
|
|
edit.set_cursor(anchor_stmt.range().start() + TextUnit::of_str("let "));
|
|
|
|
})
|
|
|
|
}
|
2019-01-03 06:08:32 -06:00
|
|
|
|
2019-01-03 09:59:17 -06:00
|
|
|
/// Statement or last in the block expression, which will follow
|
|
|
|
/// the freshly introduced var.
|
2019-01-07 07:53:24 -06:00
|
|
|
fn anchor_stmt(expr: &ast::Expr) -> Option<&SyntaxNode> {
|
2019-01-03 09:59:17 -06:00
|
|
|
expr.syntax().ancestors().find(|&node| {
|
|
|
|
if ast::Stmt::cast(node).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if let Some(expr) = node
|
|
|
|
.parent()
|
|
|
|
.and_then(ast::Block::cast)
|
|
|
|
.and_then(|it| it.expr())
|
|
|
|
{
|
|
|
|
if expr.syntax() == node {
|
2019-01-03 06:08:32 -06:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-03 09:59:17 -06:00
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
2019-01-03 06:08:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-01-03 09:59:17 -06:00
|
|
|
use crate::assists::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
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-13 19:59:56 -06:00
|
|
|
#[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-03 06:08:32 -06:00
|
|
|
}
|