Class: SyntaxTree::BlockFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Responsible for formatting either a BraceBlock or a DoBlock.

Defined Under Namespace

Classes: BlockOpenFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, block_open, statements) ⇒ BlockFormatter

Returns a new instance of BlockFormatter.



2691
2692
2693
2694
2695
# File 'lib/syntax_tree.rb', line 2691

def initialize(node, block_open, statements)
  @node = node
  @block_open = block_open
  @statements = statements
end

Instance Attribute Details

#block_openObject (readonly)

LBrace | Keyword

the node that opens the block



2686
2687
2688
# File 'lib/syntax_tree.rb', line 2686

def block_open
  @block_open
end

#nodeObject (readonly)

BraceBlock | DoBlock

the block node to be formatted



2683
2684
2685
# File 'lib/syntax_tree.rb', line 2683

def node
  @node
end

#statementsObject (readonly)

BodyStmt | Statements

the statements inside the block



2689
2690
2691
# File 'lib/syntax_tree.rb', line 2689

def statements
  @statements
end

Instance Method Details

#format(q) ⇒ Object



2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
# File 'lib/syntax_tree.rb', line 2697

def format(q)
  q.group do
    q.text(" ")

    q.if_break do
      q.format(BlockOpenFormatter.new("do", block_open))

      if node.block_var
        q.text(" ")
        q.format(node.block_var)
      end

      unless statements.empty?
        q.indent do
          q.breakable
          q.format(statements)
        end
      end

      q.breakable
      q.text("end")
    end.if_flat do
      q.format(BlockOpenFormatter.new("{", block_open))

      if node.block_var
        q.breakable
        q.format(node.block_var)
        q.breakable
      end

      unless statements.empty?
        q.breakable unless node.block_var
        q.format(statements)
        q.breakable
      end

      q.text("}")
    end
  end
end