Skip to content

Commit

Permalink
better introduce
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Sep 5, 2018
1 parent bb64edf commit 751562d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions crates/libeditor/src/code_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,22 @@ pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl F
}
Some(move || {
let mut buf = String::new();
let mut edit = EditBuilder::new();

buf.push_str("let var_name = ");
expr.syntax().text().push_to(&mut buf);
buf.push_str(";");
indent.text().push_to(&mut buf);

let mut edit = EditBuilder::new();
edit.replace(expr.syntax().range(), "var_name".to_string());
edit.insert(anchor_stmt.syntax().range().start(), buf);
if expr.syntax().range().start() == anchor_stmt.syntax().range().start() {
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.syntax().range().start(), buf);
}
let cursor_position = anchor_stmt.syntax().range().start() + TextUnit::of_str("let ");
LocalEdit {
edit: edit.finish(),
cursor_position: Some(anchor_stmt.syntax().range().start() + TextUnit::of_str("let ")),
cursor_position: Some(cursor_position),
}
})
}
Expand Down Expand Up @@ -183,7 +188,7 @@ mod tests {
}

#[test]
fn test_intrdoduce_var() {
fn test_intrdoduce_var_simple() {
check_action_range(
"
fn foo() {
Expand All @@ -192,6 +197,19 @@ fn foo() {
fn foo() {
let <|>var_name = 1 + 1;
foo(var_name);
}",
|file, range| introduce_variable(file, range).map(|f| f()),
);
}
#[test]
fn test_intrdoduce_var_expr_stmt() {
check_action_range(
"
fn foo() {
<|>1 + 1<|>;
}", "
fn foo() {
let <|>var_name = 1 + 1;
}",
|file, range| introduce_variable(file, range).map(|f| f()),
);
Expand Down

0 comments on commit 751562d

Please sign in to comment.