mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:17:35 +00:00
Shell: Implement formatting for Heredocs
This commit is contained in:
parent
c5c6161406
commit
4c40151160
2 changed files with 80 additions and 20 deletions
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue