mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-03 15:15:24 +00:00
Remove nesting
This commit is contained in:
parent
f88e13f539
commit
5508c91b3e
1 changed files with 68 additions and 75 deletions
|
@ -131,78 +131,77 @@ fn remove_newline(
|
||||||
node_text: &str,
|
node_text: &str,
|
||||||
offset: TextUnit,
|
offset: TextUnit,
|
||||||
) {
|
) {
|
||||||
if node.kind() == WHITESPACE && node_text.bytes().filter(|&b| b == b'\n').count() == 1 {
|
if node.kind() != WHITESPACE || node_text.bytes().filter(|&b| b == b'\n').count() != 1 {
|
||||||
// Special case that turns something like:
|
// The node is either the first or the last in the file
|
||||||
//
|
let suff = &node_text[TextRange::from_to(
|
||||||
// ```
|
offset - node.range().start() + TextUnit::of_char('\n'),
|
||||||
// my_function({<|>
|
TextUnit::of_str(node_text),
|
||||||
// <some-expr>
|
)];
|
||||||
// })
|
let spaces = suff.bytes().take_while(|&b| b == b' ').count();
|
||||||
// ```
|
|
||||||
//
|
|
||||||
// into `my_function(<some-expr>)`
|
|
||||||
if join_single_expr_block(edit, node).is_some() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if let (Some(prev), Some(next)) = (node.prev_sibling(), node.next_sibling()) {
|
edit.replace(
|
||||||
let range = TextRange::from_to(prev.range().start(), node.range().end());
|
TextRange::offset_len(offset, ((spaces + 1) as u32).into()),
|
||||||
if is_trailing_comma(prev.kind(), next.kind()) {
|
" ".to_string(),
|
||||||
// Removes: trailing comma, newline (incl. surrounding whitespace)
|
);
|
||||||
edit.delete(range);
|
return;
|
||||||
} else if no_space_required(prev.kind(), next.kind()) {
|
|
||||||
// Removes: newline (incl. surrounding whitespace)
|
|
||||||
edit.delete(node.range());
|
|
||||||
} else if prev.kind() == COMMA && next.kind() == R_CURLY {
|
|
||||||
// Removes: comma, newline (incl. surrounding whitespace)
|
|
||||||
// Adds: a single whitespace
|
|
||||||
edit.replace(range, " ".to_string());
|
|
||||||
} else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) {
|
|
||||||
// Removes: newline (incl. surrounding whitespace), start of the next comment
|
|
||||||
let comment_text = next.text();
|
|
||||||
if let Some(newline_pos) = comment_text.find('\n') {
|
|
||||||
// Special case for multi-line c-like comments: join the comment content but
|
|
||||||
// keep the leading `/*`
|
|
||||||
|
|
||||||
let newline_offset = next.syntax().range().start()
|
|
||||||
+ TextUnit::from(newline_pos as u32)
|
|
||||||
+ TextUnit::of_char('\n');
|
|
||||||
|
|
||||||
edit.insert(newline_offset, "/*".to_string());
|
|
||||||
edit.delete(TextRange::from_to(
|
|
||||||
node.range().start(),
|
|
||||||
next.syntax().range().start() + TextUnit::of_str(next.prefix())
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
// Single-line comments
|
|
||||||
edit.delete(TextRange::from_to(
|
|
||||||
node.range().start(),
|
|
||||||
next.syntax().range().start() + TextUnit::of_str(next.prefix())
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Remove newline but add a computed amount of whitespace characters
|
|
||||||
edit.replace(
|
|
||||||
node.range(),
|
|
||||||
compute_ws(prev, next).to_string(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The node is either the first or the last in the file
|
// Special case that turns something like:
|
||||||
let suff = &node_text[TextRange::from_to(
|
//
|
||||||
offset - node.range().start() + TextUnit::of_char('\n'),
|
// ```
|
||||||
TextUnit::of_str(node_text),
|
// my_function({<|>
|
||||||
)];
|
// <some-expr>
|
||||||
let spaces = suff.bytes().take_while(|&b| b == b' ').count();
|
// })
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// into `my_function(<some-expr>)`
|
||||||
|
if join_single_expr_block(edit, node).is_some() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
edit.replace(
|
// The node is between two other nodes
|
||||||
TextRange::offset_len(offset, ((spaces + 1) as u32).into()),
|
let prev = node.prev_sibling().unwrap();
|
||||||
" ".to_string(),
|
let next = node.next_sibling().unwrap();
|
||||||
);
|
if is_trailing_comma(prev.kind(), next.kind()) {
|
||||||
|
// Removes: trailing comma, newline (incl. surrounding whitespace)
|
||||||
|
edit.delete(TextRange::from_to(prev.range().start(), node.range().end()));
|
||||||
|
} else if prev.kind() == COMMA && next.kind() == R_CURLY {
|
||||||
|
// Removes: comma, newline (incl. surrounding whitespace)
|
||||||
|
// Adds: a single whitespace
|
||||||
|
edit.replace(
|
||||||
|
TextRange::from_to(prev.range().start(), node.range().end()),
|
||||||
|
" ".to_string()
|
||||||
|
);
|
||||||
|
} else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) {
|
||||||
|
// Removes: newline (incl. surrounding whitespace), start of the next comment
|
||||||
|
let comment_text = next.text();
|
||||||
|
if let Some(newline_pos) = comment_text.find('\n') {
|
||||||
|
// Special case for multiline comments: join the comment content but
|
||||||
|
// keep the leading `/*`
|
||||||
|
|
||||||
|
let newline_offset = next.syntax().range().start()
|
||||||
|
+ TextUnit::from(newline_pos as u32)
|
||||||
|
+ TextUnit::of_char('\n');
|
||||||
|
|
||||||
|
edit.insert(newline_offset, "/*".to_string());
|
||||||
|
edit.delete(TextRange::from_to(
|
||||||
|
node.range().start(),
|
||||||
|
next.syntax().range().start() + TextUnit::of_str(next.prefix())
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
// Single-line comments
|
||||||
|
edit.delete(TextRange::from_to(
|
||||||
|
node.range().start(),
|
||||||
|
next.syntax().range().start() + TextUnit::of_str(next.prefix())
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Remove newline but add a computed amount of whitespace characters
|
||||||
|
edit.replace(
|
||||||
|
node.range(),
|
||||||
|
compute_ws(prev, next).to_string(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
|
fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
|
||||||
|
@ -212,13 +211,6 @@ fn is_trailing_comma(left: SyntaxKind, right: SyntaxKind) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn no_space_required(left: SyntaxKind, right: SyntaxKind) -> bool {
|
|
||||||
match (left, right) {
|
|
||||||
(_, DOT) => true,
|
|
||||||
_ => false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn join_single_expr_block(
|
fn join_single_expr_block(
|
||||||
edit: &mut EditBuilder,
|
edit: &mut EditBuilder,
|
||||||
node: SyntaxNodeRef,
|
node: SyntaxNodeRef,
|
||||||
|
@ -260,6 +252,7 @@ fn compute_ws(left: SyntaxNodeRef, right: SyntaxNodeRef) -> &'static str {
|
||||||
}
|
}
|
||||||
match right.kind() {
|
match right.kind() {
|
||||||
R_PAREN | R_BRACK => return "",
|
R_PAREN | R_BRACK => return "",
|
||||||
|
DOT => return "",
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
" "
|
" "
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue