fish_indent: Allow escaped newlines only for certain things

Things like

```fish
\
echo foo
```

or

```fish
echo foo; \
echo bar
```

are a formatting blunder and should be handled.

This makes it so the escaped newline is removed, and the
semicolon/token_type_end handling will then put the statements on
different lines.

One case this doesn't handle brilliantly is an escaped newline after a
pipe:

```fish
echo foo | \
cat
```

is turned into

```fish
echo foo | cat
```

which here works great, but in long pipelines can cause issues.

Pipes at the end of the line cause fish to continue parsing on the
next line, so this can just be written as

```fish
echo foo |
cat
```

for now.
This commit is contained in:
Fabian Homborg 2020-03-15 15:25:34 +01:00
parent 65d277a581
commit cb16d9e741
2 changed files with 18 additions and 4 deletions

View File

@ -206,8 +206,23 @@ void prettifier_t::prettify_node(const parse_node_tree_t &tree, node_offset_t no
if (dump_parse_tree) dump_node(node_indent, node, source);
// Prepend any escaped newline.
maybe_prepend_escaped_newline(node);
// Prepend any escaped newline, but only for certain cases.
// We allow it to split arguments (including at the end - this is like trailing commas in lists, makes for better diffs),
// to separate pipelines (but it has to be *before* the pipe, so the pipe symbol is the first thing on the new line after the indent)
// and to separate &&/|| job lists (`and` and `or` are handled separately below, as they *allow* semicolons)
// TODO: Handle
// foo | \
// bar
// so it just removes the escape - pipes don't need it. This was changed in some fish version, figure out which it was and if
// it is worth supporting.
if (prev_node_type == symbol_arguments_or_redirections_list
|| prev_node_type == symbol_argument_list
|| node_type == parse_token_type_andand
|| node_type == parse_token_type_pipe
|| node_type == parse_token_type_end
) {
maybe_prepend_escaped_newline(node);
}
// handle comments, which come before the text
if (node.has_comments()) {

View File

@ -107,8 +107,7 @@ while true
builtin yes
end
alpha | \
beta
alpha | beta
gamma | \
# comment3