diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-04-29 16:05:25 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-29 20:25:56 +0200 |
commit | 4c4015116086606487233d87f4e05a2b58deef59 (patch) | |
tree | 4fc1bd489aa8c3028321b108daedbf36229d4476 /Userland/Shell/Formatter.h | |
parent | c5c6161406728b33aedf3e9ca9d8616a813cf80d (diff) | |
download | serenity-4c4015116086606487233d87f4e05a2b58deef59.zip |
Shell: Implement formatting for Heredocs
Diffstat (limited to 'Userland/Shell/Formatter.h')
-rw-r--r-- | Userland/Shell/Formatter.h | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Userland/Shell/Formatter.h b/Userland/Shell/Formatter.h index 44e6fa1771..a8316b42ed 100644 --- a/Userland/Shell/Formatter.h +++ b/Userland/Shell/Formatter.h @@ -8,6 +8,7 @@ #include "NodeVisitor.h" #include <AK/Forward.h> +#include <AK/String.h> #include <AK/StringBuilder.h> #include <AK/StringView.h> #include <AK/Types.h> @@ -18,7 +19,7 @@ namespace Shell { class Formatter final : public AST::NodeVisitor { public: Formatter(const StringView& source, ssize_t cursor = -1) - : m_builder(round_up_to_power_of_two(source.length(), 16)) + : m_builders({ StringBuilder { round_up_to_power_of_two(source.length(), 16) } }) , m_source(source) , m_cursor(cursor) { @@ -30,7 +31,8 @@ public: } explicit Formatter(const AST::Node& node) - : m_cursor(-1) + : m_builders({ StringBuilder {} }) + , m_cursor(-1) , m_root_node(node) { } @@ -57,6 +59,7 @@ private: virtual void visit(const AST::FunctionDeclaration*) override; virtual void visit(const AST::ForLoop*) override; virtual void visit(const AST::Glob*) override; + virtual void visit(const AST::Heredoc*) override; virtual void visit(const AST::HistoryEvent*) override; virtual void visit(const AST::Execute*) override; virtual void visit(const AST::IfCond*) override; @@ -85,24 +88,26 @@ private: void test_and_update_output_cursor(const AST::Node*); void visited(const AST::Node*); void will_visit(const AST::Node*); - void insert_separator(); + void insert_separator(bool escaped = false); void insert_indent(); ALWAYS_INLINE void with_added_indent(int indent, Function<void()>); ALWAYS_INLINE void in_new_block(Function<void()>); + ALWAYS_INLINE String in_new_builder(Function<void()>, StringBuilder new_builder = StringBuilder {}); - StringBuilder& current_builder() { return m_builder; } + StringBuilder& current_builder() { return m_builders.last(); } struct Options { size_t max_line_length_hint { 80 }; bool explicit_parentheses { false }; bool explicit_braces { false }; bool in_double_quotes { false }; + bool in_heredoc { false }; } m_options; size_t m_current_indent { 0 }; - StringBuilder m_builder; + Vector<StringBuilder> m_builders; StringView m_source; size_t m_output_cursor { 0 }; @@ -114,6 +119,7 @@ private: const AST::Node* m_last_visited_node { nullptr }; StringView m_trivia; + Vector<String> m_heredocs_to_append_after_sequence; }; } |