Remove nesting

This commit is contained in:
Adolfo Ochagavía 2018-10-11 16:45:52 +02:00
parent f88e13f539
commit 5508c91b3e

View file

@ -131,7 +131,21 @@ 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 {
// 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'),
TextUnit::of_str(node_text),
)];
let spaces = suff.bytes().take_while(|&b| b == b' ').count();
edit.replace(
TextRange::offset_len(offset, ((spaces + 1) as u32).into()),
" ".to_string(),
);
return;
}
// Special case that turns something like: // Special case that turns something like:
// //
// ``` // ```
@ -145,23 +159,24 @@ fn remove_newline(
return return
} }
if let (Some(prev), Some(next)) = (node.prev_sibling(), node.next_sibling()) { // The node is between two other nodes
let range = TextRange::from_to(prev.range().start(), node.range().end()); let prev = node.prev_sibling().unwrap();
let next = node.next_sibling().unwrap();
if is_trailing_comma(prev.kind(), next.kind()) { if is_trailing_comma(prev.kind(), next.kind()) {
// Removes: trailing comma, newline (incl. surrounding whitespace) // Removes: trailing comma, newline (incl. surrounding whitespace)
edit.delete(range); edit.delete(TextRange::from_to(prev.range().start(), node.range().end()));
} 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 { } else if prev.kind() == COMMA && next.kind() == R_CURLY {
// Removes: comma, newline (incl. surrounding whitespace) // Removes: comma, newline (incl. surrounding whitespace)
// Adds: a single whitespace // Adds: a single whitespace
edit.replace(range, " ".to_string()); 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)) { } else if let (Some(_), Some(next)) = (ast::Comment::cast(prev), ast::Comment::cast(next)) {
// Removes: newline (incl. surrounding whitespace), start of the next comment // Removes: newline (incl. surrounding whitespace), start of the next comment
let comment_text = next.text(); let comment_text = next.text();
if let Some(newline_pos) = comment_text.find('\n') { if let Some(newline_pos) = comment_text.find('\n') {
// Special case for multi-line c-like comments: join the comment content but // Special case for multiline comments: join the comment content but
// keep the leading `/*` // keep the leading `/*`
let newline_offset = next.syntax().range().start() let newline_offset = next.syntax().range().start()
@ -187,22 +202,6 @@ fn remove_newline(
compute_ws(prev, next).to_string(), compute_ws(prev, next).to_string(),
); );
} }
return;
}
}
// 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'),
TextUnit::of_str(node_text),
)];
let spaces = suff.bytes().take_while(|&b| b == b' ').count();
edit.replace(
TextRange::offset_len(offset, ((spaces + 1) as u32).into()),
" ".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 "",
_ => (), _ => (),
} }
" " " "