fish_indent: Ignore consecutive semicolons

This removes semicolons at the end of the line and collapses
consecutive ones, while replacing meaningful semicolons with newlines.

I.e.

```fish
echo;
```

becomes

```fish
echo
```

but

```fish
echo; echo
```

becomes

```fish
echo
echo
```

Fixes #5859.
This commit is contained in:
Fabian Homborg 2019-05-03 19:00:55 +02:00
parent c7cbf6dad0
commit 3bea947bb5
3 changed files with 23 additions and 1 deletions

View file

@ -91,6 +91,9 @@ struct prettifier_t {
// Whether we are at the beginning of a new line. // Whether we are at the beginning of a new line.
bool has_new_line = true; bool has_new_line = true;
// Whether the last token was a semicolon.
bool last_was_semicolon = false;
// Whether we need to append a continuation new line before continuing. // Whether we need to append a continuation new line before continuing.
bool needs_continuation_newline = false; bool needs_continuation_newline = false;
@ -215,9 +218,22 @@ void prettifier_t::prettify_node(const parse_node_tree_t &tree, node_offset_t no
} }
if (node_type == parse_token_type_end) { if (node_type == parse_token_type_end) {
append_newline(); // For historical reasons, semicolon also get "TOK_END".
// We need to distinguish between them, because otherwise `a;;;;` gets extra lines
// instead of the semicolons. Semicolons are just ignored, unless they are followed by a
// command. So `echo;` removes the semicolon, but `echo; echo` removes it and adds a
// newline.
if (node.get_source(source) == L"\n") {
append_newline();
} else {
last_was_semicolon = true;
}
} else if ((node_type >= FIRST_PARSE_TOKEN_TYPE && node_type <= LAST_PARSE_TOKEN_TYPE) || } else if ((node_type >= FIRST_PARSE_TOKEN_TYPE && node_type <= LAST_PARSE_TOKEN_TYPE) ||
node_type == parse_special_type_parse_error) { node_type == parse_special_type_parse_error) {
if (last_was_semicolon) {
append_newline();
last_was_semicolon = false;
}
if (node.keyword != parse_keyword_none) { if (node.keyword != parse_keyword_none) {
append_whitespace(node_indent); append_whitespace(node_indent);
output.append(keyword_description(node.keyword)); output.append(keyword_description(node.keyword));

View file

@ -123,3 +123,6 @@ echo hi |
echo bye echo bye
' | ../test/root/bin/fish_indent ' | ../test/root/bin/fish_indent
echo 'a;;;;;;' | ../test/root/bin/fish_indent
echo 'echo; echo' | ../test/root/bin/fish_indent

View file

@ -125,3 +125,6 @@ end
echo hi | echo hi |
echo bye echo bye
a
echo
echo