From cb16d9e74123df326d2e2b497e1f5814ca32d702 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sun, 15 Mar 2020 15:25:34 +0100 Subject: [PATCH] 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. --- src/fish_indent.cpp | 19 +++++++++++++++++-- tests/indent.out | 3 +-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp index 6c1a45a8d..b292ab595 100644 --- a/src/fish_indent.cpp +++ b/src/fish_indent.cpp @@ -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()) { diff --git a/tests/indent.out b/tests/indent.out index 204b42521..bb450ce46 100644 --- a/tests/indent.out +++ b/tests/indent.out @@ -107,8 +107,7 @@ while true builtin yes end -alpha | \ - beta +alpha | beta gamma | \ # comment3