Class: PrettyPrint::IndentLevel

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

Overview

This object represents the current level of indentation within the printer. It has the ability to generate new levels of indentation through the #align and #indent methods.

Defined Under Namespace

Classes: NumberAlignPart, StringAlignPart

Constant Summary collapse

IndentPart =
Object.new
DedentPart =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(genspace:, value: genspace.call(0), length: 0, queue: [], root: nil) ⇒ IndentLevel



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/syntax_tree/prettyprint.rb', line 501

def initialize(
  genspace:,
  value: genspace.call(0),
  length: 0,
  queue: [],
  root: nil
)
  @genspace = genspace
  @value = value
  @length = length
  @queue = queue
  @root = root
end

Instance Attribute Details

#genspaceObject (readonly)

Returns the value of attribute genspace.



499
500
501
# File 'lib/syntax_tree/prettyprint.rb', line 499

def genspace
  @genspace
end

#lengthObject (readonly)

Returns the value of attribute length.



499
500
501
# File 'lib/syntax_tree/prettyprint.rb', line 499

def length
  @length
end

#queueObject (readonly)

Returns the value of attribute queue.



499
500
501
# File 'lib/syntax_tree/prettyprint.rb', line 499

def queue
  @queue
end

#rootObject (readonly)

Returns the value of attribute root.



499
500
501
# File 'lib/syntax_tree/prettyprint.rb', line 499

def root
  @root
end

#valueObject (readonly)

Returns the value of attribute value.



499
500
501
# File 'lib/syntax_tree/prettyprint.rb', line 499

def value
  @value
end

Instance Method Details

#align(n) ⇒ Object

This can accept a whole lot of different kinds of objects, due to the nature of the flexibility of the Align node.



517
518
519
520
521
522
523
524
525
526
# File 'lib/syntax_tree/prettyprint.rb', line 517

def align(n)
  case n
  when NilClass
    self
  when String
    indent(StringAlignPart.new(n))
  else
    indent(n < 0 ? DedentPart : NumberAlignPart.new(n))
  end
end

#indent(part = IndentPart) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/syntax_tree/prettyprint.rb', line 528

def indent(part = IndentPart)
  next_value = genspace.call(0)
  next_length = 0
  next_queue = (part == DedentPart ? queue[0...-1] : [*queue, part])

  last_spaces = 0

  add_spaces = ->(count) do
    next_value << genspace.call(count)
    next_length += count
  end

  flush_spaces = -> do
    add_spaces[last_spaces] if last_spaces > 0
    last_spaces = 0
  end

  next_queue.each do |part|
    case part
    when IndentPart
      flush_spaces.call
      add_spaces.call(2)
    when StringAlignPart
      flush_spaces.call
      next_value += part.n
      next_length += part.n.length
    when NumberAlignPart
      last_spaces += part.n
    end
  end

  flush_spaces.call

  IndentLevel.new(
    genspace: genspace,
    value: next_value,
    length: next_length,
    queue: next_queue,
    root: root
  )
end