mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 17:28:09 +00:00
add semi only if it wasn't present before or after
This commit is contained in:
parent
5d6cf59f60
commit
4149285bf5
1 changed files with 19 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode},
|
||||||
SyntaxKind::{WHITESPACE, BLOCK_EXPR},
|
SyntaxKind::{WHITESPACE, SEMI},
|
||||||
SyntaxNode, TextUnit,
|
SyntaxNode, TextUnit,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -26,8 +26,10 @@ pub fn introduce_variable<'a>(ctx: AssistCtx) -> Option<Assist> {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
if is_full_stmt {
|
if is_full_stmt {
|
||||||
if expr.syntax().kind() == BLOCK_EXPR {
|
if let Some(last_child) = expr.syntax().last_child() {
|
||||||
buf.push_str(";");
|
if last_child.kind() != SEMI && !is_semi_right_after(expr.syntax()) {
|
||||||
|
buf.push_str(";");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
edit.replace(expr.syntax().range(), buf);
|
edit.replace(expr.syntax().range(), buf);
|
||||||
} else {
|
} else {
|
||||||
|
@ -60,6 +62,20 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<&SyntaxNode> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_semi_right_after(node: &SyntaxNode) -> bool {
|
||||||
|
let mut node = node;
|
||||||
|
loop {
|
||||||
|
if let Some(next) = node.next_sibling() {
|
||||||
|
if next.kind() == WHITESPACE {
|
||||||
|
node = next;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return next.kind() == SEMI;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
Loading…
Reference in a new issue