mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-31 23:38:45 +00:00
Remove trailing ;
when turning assignment into ==
comparison
This commit is contained in:
parent
b9dd7db817
commit
99e9e52fbc
1 changed files with 70 additions and 1 deletions
|
@ -173,6 +173,9 @@ fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
|
||||||
if let Some(edit) = assign_expr(file, offset) {
|
if let Some(edit) = assign_expr(file, offset) {
|
||||||
return Some(edit);
|
return Some(edit);
|
||||||
}
|
}
|
||||||
|
if let Some(edit) = assign_to_eq(file, offset) {
|
||||||
|
return Some(edit);
|
||||||
|
}
|
||||||
|
|
||||||
return None;
|
return None;
|
||||||
|
|
||||||
|
@ -182,13 +185,13 @@ fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parent must be `ExprStmt` or `StmtList` for `;` to be valid.
|
||||||
if let Some(expr_stmt) = ast::ExprStmt::cast(binop.syntax().parent()?) {
|
if let Some(expr_stmt) = ast::ExprStmt::cast(binop.syntax().parent()?) {
|
||||||
if expr_stmt.semicolon_token().is_some() {
|
if expr_stmt.semicolon_token().is_some() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !ast::StmtList::can_cast(binop.syntax().parent()?.kind()) {
|
if !ast::StmtList::can_cast(binop.syntax().parent()?.kind()) {
|
||||||
// Parent must be `ExprStmt` or `StmtList` for `;` to be valid.
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -205,6 +208,25 @@ fn on_eq_typed(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
|
||||||
Some(TextEdit::insert(offset, ";".to_string()))
|
Some(TextEdit::insert(offset, ";".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `a =$0 b;` removes the semicolon if an expression is valid in this context.
|
||||||
|
fn assign_to_eq(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
|
||||||
|
let binop: ast::BinExpr = find_node_at_offset(file.syntax(), offset)?;
|
||||||
|
if !matches!(binop.op_kind(), Some(ast::BinaryOp::CmpOp(ast::CmpOp::Eq { negated: false })))
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let expr_stmt = ast::ExprStmt::cast(binop.syntax().parent()?)?;
|
||||||
|
let semi = expr_stmt.semicolon_token()?;
|
||||||
|
|
||||||
|
if expr_stmt.syntax().next_sibling().is_some() {
|
||||||
|
// Not the last statement in the list.
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(TextEdit::delete(semi.text_range()))
|
||||||
|
}
|
||||||
|
|
||||||
fn let_stmt(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
|
fn let_stmt(file: &SourceFile, offset: TextSize) -> Option<TextEdit> {
|
||||||
let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
|
let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
|
||||||
if let_stmt.semicolon_token().is_some() {
|
if let_stmt.semicolon_token().is_some() {
|
||||||
|
@ -424,6 +446,53 @@ fn f() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn assign_to_eq() {
|
||||||
|
type_char(
|
||||||
|
'=',
|
||||||
|
r#"
|
||||||
|
fn f(a: u8) {
|
||||||
|
a =$0 0;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn f(a: u8) {
|
||||||
|
a == 0
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
type_char(
|
||||||
|
'=',
|
||||||
|
r#"
|
||||||
|
fn f(a: u8) {
|
||||||
|
a $0= 0;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn f(a: u8) {
|
||||||
|
a == 0
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
type_char_noop(
|
||||||
|
'=',
|
||||||
|
r#"
|
||||||
|
fn f(a: u8) {
|
||||||
|
let e = a =$0 0;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
type_char_noop(
|
||||||
|
'=',
|
||||||
|
r#"
|
||||||
|
fn f(a: u8) {
|
||||||
|
let e = a =$0 0;
|
||||||
|
e
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn indents_new_chain_call() {
|
fn indents_new_chain_call() {
|
||||||
type_char(
|
type_char(
|
||||||
|
|
Loading…
Reference in a new issue