Skip to content

Commit 0bdf8de

Browse files
bors[bot]flodiebold
andcommitted
Merge #252
252: Improve 'introduce variable' r=matklad a=flodiebold - make it possible to extract a prefix of an expression statement (e.g. `<|>foo.bar()<|>.baz()`) - don't turn the last expression in a block into a let statement - also fix a few typos Co-authored-by: Florian Diebold <[email protected]>
2 parents bcc2342 + 2706456 commit 0bdf8de

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

crates/ra_editor/src/code_actions.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn introduce_variable<'a>(
122122
let node = find_covering_node(file.syntax(), range);
123123
let expr = node.ancestors().filter_map(ast::Expr::cast).next()?;
124124

125-
let anchor_stmt = ahchor_stmt(expr)?;
125+
let anchor_stmt = anchor_stmt(expr)?;
126126
let indent = anchor_stmt.prev_sibling()?;
127127
if indent.kind() != WHITESPACE {
128128
return None;
@@ -133,7 +133,12 @@ pub fn introduce_variable<'a>(
133133

134134
buf.push_str("let var_name = ");
135135
expr.syntax().text().push_to(&mut buf);
136-
if expr.syntax().range().start() == anchor_stmt.range().start() {
136+
let is_full_stmt = if let Some(expr_stmt) = ast::ExprStmt::cast(anchor_stmt) {
137+
Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
138+
} else {
139+
false
140+
};
141+
if is_full_stmt {
137142
edit.replace(expr.syntax().range(), buf);
138143
} else {
139144
buf.push_str(";");
@@ -150,7 +155,7 @@ pub fn introduce_variable<'a>(
150155

151156
/// Statement or last in the block expression, which will follow
152157
/// the freshly introduced var.
153-
fn ahchor_stmt(expr: ast::Expr) -> Option<SyntaxNodeRef> {
158+
fn anchor_stmt(expr: ast::Expr) -> Option<SyntaxNodeRef> {
154159
expr.syntax().ancestors().find(|&node| {
155160
if ast::Stmt::cast(node).is_some() {
156161
return true;
@@ -250,7 +255,7 @@ struct Foo { a: i32, }
250255
}
251256

252257
#[test]
253-
fn test_intrdoduce_var_simple() {
258+
fn test_introduce_var_simple() {
254259
check_action_range(
255260
"
256261
fn foo() {
@@ -266,7 +271,7 @@ fn foo() {
266271
}
267272

268273
#[test]
269-
fn test_intrdoduce_var_expr_stmt() {
274+
fn test_introduce_var_expr_stmt() {
270275
check_action_range(
271276
"
272277
fn foo() {
@@ -281,7 +286,23 @@ fn foo() {
281286
}
282287

283288
#[test]
284-
fn test_intrdoduce_var_last_expr() {
289+
fn test_introduce_var_part_of_expr_stmt() {
290+
check_action_range(
291+
"
292+
fn foo() {
293+
<|>1<|> + 1;
294+
}",
295+
"
296+
fn foo() {
297+
let <|>var_name = 1;
298+
var_name + 1;
299+
}",
300+
|file, range| introduce_variable(file, range).map(|f| f()),
301+
);
302+
}
303+
304+
#[test]
305+
fn test_introduce_var_last_expr() {
285306
check_action_range(
286307
"
287308
fn foo() {
@@ -296,4 +317,20 @@ fn foo() {
296317
);
297318
}
298319

320+
#[test]
321+
fn test_introduce_var_last_full_expr() {
322+
check_action_range(
323+
"
324+
fn foo() {
325+
<|>bar(1 + 1)<|>
326+
}",
327+
"
328+
fn foo() {
329+
let <|>var_name = bar(1 + 1);
330+
var_name
331+
}",
332+
|file, range| introduce_variable(file, range).map(|f| f()),
333+
);
334+
}
335+
299336
}

crates/ra_editor/src/edit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl EditBuilder {
2626
}
2727
pub fn finish(self) -> Edit {
2828
let mut atoms = self.atoms;
29-
atoms.sort_by_key(|a| a.delete.start());
29+
atoms.sort_by_key(|a| (a.delete.start(), a.delete.end()));
3030
for (a1, a2) in atoms.iter().zip(atoms.iter().skip(1)) {
3131
assert!(a1.delete.end() <= a2.delete.start())
3232
}

0 commit comments

Comments
 (0)