Class: PrettyPrint::IndentLevel
- Inherits:
-
Object
- Object
- PrettyPrint::IndentLevel
- 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
-
#genspace ⇒ Object
readonly
Returns the value of attribute genspace.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#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.
- #indent(part = IndentPart) ⇒ Object
-
#initialize(genspace:, value: genspace.call(0), length: 0, queue: [], root: nil) ⇒ IndentLevel
constructor
A new instance of IndentLevel.
Constructor Details
#initialize(genspace:, value: genspace.call(0), length: 0, queue: [], root: nil) ⇒ IndentLevel
Returns a new instance of IndentLevel.
499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/syntax_tree/prettyprint.rb', line 499 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
#genspace ⇒ Object (readonly)
Returns the value of attribute genspace.
497 498 499 |
# File 'lib/syntax_tree/prettyprint.rb', line 497 def genspace @genspace end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
497 498 499 |
# File 'lib/syntax_tree/prettyprint.rb', line 497 def length @length end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
497 498 499 |
# File 'lib/syntax_tree/prettyprint.rb', line 497 def queue @queue end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
497 498 499 |
# File 'lib/syntax_tree/prettyprint.rb', line 497 def root @root end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
497 498 499 |
# File 'lib/syntax_tree/prettyprint.rb', line 497 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.
515 516 517 518 519 520 521 522 523 524 |
# File 'lib/syntax_tree/prettyprint.rb', line 515 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
526 527 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 |
# File 'lib/syntax_tree/prettyprint.rb', line 526 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 |