From 9d7f6db79295f87eab28c3f54ffff28181de52c6 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Tue, 27 Apr 2021 00:00:32 +0200 Subject: [PATCH] fish_indent: preserve escaped newlines around variable assignments In many cases we currently discard escaped newlines, since they are often unnecessary (when used around &|;). Escaped newlines are useful for structuring argument lists. Allow them for variable assignments since they are similar. Closes #7955 --- CHANGELOG.rst | 1 + src/fish_indent.cpp | 20 +++++++++++++++++++- tests/checks/indent.fish | 11 +++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 42ec13d0f..5f34140a2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -22,6 +22,7 @@ Scripting improvements - Builtins now properly report a ``$status`` of 1 upon unsuccessful writes (:issue:`7857`). - ``string match`` with unmatched capture groups and without the ``--all`` flag now sets an empty variable instead of a variable containing the empty string. It also correctly imports the first match if multiple arguments are provided, matching the documentation. (:issue:`7938`). - Better errors when a command in a command substitution wasn't found or is not allowed. +- ``fish_indent`` allows to write inline variable assignments on multiple lines (ending in a backslash), instead of joining them into one line (:issue:`7955`). Interactive improvements ------------------------- diff --git a/src/fish_indent.cpp b/src/fish_indent.cpp index 6c784e14e..30e3cbab6 100644 --- a/src/fish_indent.cpp +++ b/src/fish_indent.cpp @@ -167,9 +167,10 @@ struct pretty_printer_t { static gap_flags_t gap_text_flags_before_node(const node_t &node) { gap_flags_t result = default_flags; switch (node.type) { - // Allow escaped newlines in argument and redirection lists. + // Allow escaped newlines before leaf nodes that can be part of a long command. case type_t::argument: case type_t::redirection: + case type_t::variable_assignment: result |= allow_escaped_newlines; break; @@ -181,6 +182,23 @@ struct pretty_printer_t { case parse_token_type_t::pipe: result |= allow_escaped_newlines; break; + case parse_token_type_t::string: { + // Allow escaped newlines before commands that follow a variable assignment + // since both can be long (#7955). + const node_t *p = node.parent; + if (p->type != type_t::decorated_statement) break; + p = p->parent; + assert(p->type == type_t::statement); + p = p->parent; + if (auto job = p->try_as()) { + if (!job->variables.empty()) result |= allow_escaped_newlines; + } else if (auto job_cnt = p->try_as()) { + if (!job_cnt->variables.empty()) result |= allow_escaped_newlines; + } else if (auto not_stmt = p->try_as()) { + if (!not_stmt->variables.empty()) result |= allow_escaped_newlines; + } + break; + } default: break; } diff --git a/tests/checks/indent.fish b/tests/checks/indent.fish index 5887973d4..7f41361aa 100644 --- a/tests/checks/indent.fish +++ b/tests/checks/indent.fish @@ -405,3 +405,14 @@ function hello_continuations #CHECK: {{^}} echo --opt2 \ #CHECK: {{^}} echo --opt3 #CHECK: end + +echo "\ +a=1 \\ + a=2 \\ + echo" | $fish_indent --check +echo $status #CHECK: 0 + +echo "\ +a=1 \\ + a=2 echo" | $fish_indent --check +echo $status #CHECK: 0