diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-03-07 09:45:40 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-03-07 10:58:42 +0100 |
commit | fec8d7d699c6dfe71390a73e264f95d834ceb1eb (patch) | |
tree | ce2aa733039ea3a5749145c9d469f5d52c2cfb0f /Userland | |
parent | e1512d59688be4155a6b378de06b9a076063a640 (diff) | |
download | serenity-fec8d7d699c6dfe71390a73e264f95d834ceb1eb.zip |
Shell: Corrently indent offset newlines when formatting
Previously, formatting the following would incorrectly skip the indents:
```
{
foo
bar
}
```
to create:
```
{
foo
bar
}
```
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Shell/Formatter.cpp | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/Userland/Shell/Formatter.cpp b/Userland/Shell/Formatter.cpp index ea7b6b4019..95a0c40a07 100644 --- a/Userland/Shell/Formatter.cpp +++ b/Userland/Shell/Formatter.cpp @@ -108,10 +108,10 @@ void Formatter::will_visit(const AST::Node* node) auto direct_sequence_child = !m_parent_node || m_parent_node->kind() == AST::Node::Kind::Sequence; - if (direct_sequence_child && node->kind() != AST::Node::Kind::Sequence) { + if (direct_sequence_child && node->kind() != AST::Node::Kind::Sequence && node->kind() != AST::Node::Kind::Execute) { // Collapse more than one empty line to a single one. if (node->position().start_line.line_number - m_last_visited_node->position().end_line.line_number > 1) - current_builder().append('\n'); + insert_separator(); } } @@ -213,14 +213,10 @@ void Formatter::visit(const AST::CastToCommand* node) { will_visit(node); test_and_update_output_cursor(node); - if (m_options.explicit_parentheses) - current_builder().append('('); TemporaryChange<const AST::Node*> parent { m_parent_node, node }; NodeVisitor::visit(node); - if (m_options.explicit_parentheses) - current_builder().append(')'); visited(node); } @@ -243,7 +239,7 @@ void Formatter::visit(const AST::CloseFdRedirection* node) test_and_update_output_cursor(node); TemporaryChange<const AST::Node*> parent { m_parent_node, node }; - current_builder().appendf(" %d>&-", node->fd()); + current_builder().appendf("%d>&-", node->fd()); visited(node); } @@ -305,7 +301,7 @@ void Formatter::visit(const AST::Fd2FdRedirection* node) test_and_update_output_cursor(node); TemporaryChange<const AST::Node*> parent { m_parent_node, node }; - current_builder().appendf(" %d>&%d", node->source_fd(), node->dest_fd()); + current_builder().appendf("%d>&%d", node->source_fd(), node->dest_fd()); if (m_hit_node == node) ++m_output_cursor; visited(node); @@ -433,12 +429,14 @@ void Formatter::visit(const AST::Execute* node) TemporaryChange<const AST::Node*> parent { m_parent_node, node }; ScopedValueRollback options_rollback { m_options }; - if (node->does_capture_stdout()) { - builder.append("$"); - m_options.explicit_parentheses = true; - } + if (node->does_capture_stdout()) + builder.append("$("); NodeVisitor::visit(node); + + if (node->does_capture_stdout()) + builder.append(")"); + visited(node); } @@ -486,7 +484,11 @@ void Formatter::visit(const AST::Join* node) if (should_parenthesise) current_builder().append('('); - NodeVisitor::visit(node); + node->left()->visit(*this); + + current_builder().append(' '); + + node->right()->visit(*this); if (should_parenthesise) current_builder().append(')'); @@ -652,9 +654,7 @@ void Formatter::visit(const AST::Subshell* node) TemporaryChange<const AST::Node*> parent { m_parent_node, node }; in_new_block([&] { - insert_separator(); NodeVisitor::visit(node); - insert_separator(); }); visited(node); } |