mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-25 13:24:22 +00:00
Merge #536
536: Introduce variable semicolon block expr r=matklad a=yerke Fix for https://github.com/rust-analyzer/rust-analyzer/issues/504 Feels a bit hacky... Co-authored-by: Yerkebulan Tulibergenov <yerkebulan@gmail.com>
This commit is contained in:
commit
d75a0368f5
@ -20,12 +20,16 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> {
|
|||||||
|
|
||||||
buf.push_str("let var_name = ");
|
buf.push_str("let var_name = ");
|
||||||
expr.syntax().text().push_to(&mut buf);
|
expr.syntax().text().push_to(&mut buf);
|
||||||
let is_full_stmt = if let Some(expr_stmt) = ast::ExprStmt::cast(anchor_stmt) {
|
let full_stmt = ast::ExprStmt::cast(anchor_stmt);
|
||||||
|
let is_full_stmt = if let Some(expr_stmt) = full_stmt {
|
||||||
Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
|
Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
if is_full_stmt {
|
if is_full_stmt {
|
||||||
|
if !full_stmt.unwrap().has_semi() {
|
||||||
|
buf.push_str(";");
|
||||||
|
}
|
||||||
edit.replace(expr.syntax().range(), buf);
|
edit.replace(expr.syntax().range(), buf);
|
||||||
} else {
|
} else {
|
||||||
buf.push_str(";");
|
buf.push_str(";");
|
||||||
@ -141,4 +145,20 @@ fn foo() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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();
|
||||||
|
}",
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -296,6 +296,15 @@ impl IfExpr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ExprStmt {
|
||||||
|
pub fn has_semi(&self) -> bool {
|
||||||
|
match self.syntax().last_child() {
|
||||||
|
None => false,
|
||||||
|
Some(node) => node.kind() == SEMI,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum PathSegmentKind<'a> {
|
pub enum PathSegmentKind<'a> {
|
||||||
Name(&'a NameRef),
|
Name(&'a NameRef),
|
||||||
|
Loading…
Reference in New Issue
Block a user