syntax_tree 2.2.0 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,7 +79,7 @@ class PrettyPrint
79
79
  end
80
80
 
81
81
  def pretty_print(q)
82
- q.group(2, "align([", "])") do
82
+ q.group(2, "align#{indent}([", "])") do
83
83
  q.seplist(contents) { |content| q.pp(content) }
84
84
  end
85
85
  end
@@ -161,7 +161,7 @@ class PrettyPrint
161
161
  end
162
162
 
163
163
  def pretty_print(q)
164
- q.group(2, "group([", "])") do
164
+ q.group(2, break? ? "breakGroup([" : "group([", "])") do
165
165
  q.seplist(contents) { |content| q.pp(content) }
166
166
  end
167
167
  end
@@ -375,7 +375,7 @@ class PrettyPrint
375
375
  # This argument is a noop.
376
376
  # * +newline+ - Argument position expected to be here for compatibility.
377
377
  # This argument is a noop.
378
- def initialize(output, maxwidth = nil, newline = nil)
378
+ def initialize(output, _maxwidth = nil, _newline = nil)
379
379
  @output = Buffer.for(output)
380
380
  @target = @output
381
381
  @line_suffixes = Buffer::ArrayBuffer.new
@@ -397,7 +397,7 @@ class PrettyPrint
397
397
  # They are all noop arguments.
398
398
  def breakable(
399
399
  separator = " ",
400
- width = separator.length,
400
+ _width = separator.length,
401
401
  indent: nil,
402
402
  force: nil
403
403
  )
@@ -410,7 +410,7 @@ class PrettyPrint
410
410
 
411
411
  # Appends +separator+ to the output buffer. +width+ is a noop here for
412
412
  # compatibility.
413
- def fill_breakable(separator = " ", width = separator.length)
413
+ def fill_breakable(separator = " ", _width = separator.length)
414
414
  target << separator
415
415
  end
416
416
 
@@ -419,9 +419,9 @@ class PrettyPrint
419
419
  target.trim!
420
420
  end
421
421
 
422
- # ----------------------------------------------------------------------------
422
+ # --------------------------------------------------------------------------
423
423
  # Container node builders
424
- # ----------------------------------------------------------------------------
424
+ # --------------------------------------------------------------------------
425
425
 
426
426
  # Opens a block for grouping objects to be pretty printed.
427
427
  #
@@ -432,11 +432,11 @@ class PrettyPrint
432
432
  # * +open_width+ - noop argument. Present for compatibility.
433
433
  # * +close_width+ - noop argument. Present for compatibility.
434
434
  def group(
435
- indent = nil,
435
+ _indent = nil,
436
436
  open_object = "",
437
437
  close_object = "",
438
- open_width = nil,
439
- close_width = nil
438
+ _open_width = nil,
439
+ _close_width = nil
440
440
  )
441
441
  target << open_object
442
442
  yield
@@ -458,6 +458,10 @@ class PrettyPrint
458
458
  IfBreakBuilder.new
459
459
  end
460
460
 
461
+ # Also effectively unnecessary, but here for compatibility.
462
+ def if_flat
463
+ end
464
+
461
465
  # A noop that immediately yields.
462
466
  def indent
463
467
  yield
@@ -474,14 +478,14 @@ class PrettyPrint
474
478
  # Takes +indent+ arg, but does nothing with it.
475
479
  #
476
480
  # Yields to a block.
477
- def nest(indent)
481
+ def nest(_indent)
478
482
  yield
479
483
  end
480
484
 
481
485
  # Add +object+ to the text to be output.
482
486
  #
483
487
  # +width+ argument is here for compatibility. It is a noop argument.
484
- def text(object = "", width = nil)
488
+ def text(object = "", _width = nil)
485
489
  target << object
486
490
  end
487
491
  end
@@ -542,17 +546,17 @@ class PrettyPrint
542
546
  last_spaces = 0
543
547
  end
544
548
 
545
- next_queue.each do |part|
546
- case part
549
+ next_queue.each do |next_part|
550
+ case next_part
547
551
  when IndentPart
548
552
  flush_spaces.call
549
553
  add_spaces.call(2)
550
554
  when StringAlignPart
551
555
  flush_spaces.call
552
- next_value += part.n
553
- next_length += part.n.length
556
+ next_value += next_part.n
557
+ next_length += next_part.n.length
554
558
  when NumberAlignPart
555
- last_spaces += part.n
559
+ last_spaces += next_part.n
556
560
  end
557
561
  end
558
562
 
@@ -619,9 +623,9 @@ class PrettyPrint
619
623
  #
620
624
  def self.singleline_format(
621
625
  output = "".dup,
622
- maxwidth = nil,
623
- newline = nil,
624
- genspace = nil
626
+ _maxwidth = nil,
627
+ _newline = nil,
628
+ _genspace = nil
625
629
  )
626
630
  q = SingleLine.new(output)
627
631
  yield q
@@ -759,9 +763,7 @@ class PrettyPrint
759
763
 
760
764
  # This is a linear stack instead of a mutually recursive call defined on
761
765
  # the individual doc nodes for efficiency.
762
- while commands.any?
763
- indent, mode, doc = commands.pop
764
-
766
+ while (indent, mode, doc = commands.pop)
765
767
  case doc
766
768
  when Text
767
769
  doc.objects.each { |object| buffer << object }
@@ -776,23 +778,26 @@ class PrettyPrint
776
778
  position -= buffer.trim!
777
779
  when Group
778
780
  if mode == MODE_FLAT && !should_remeasure
779
- commands <<
780
- [indent, doc.break? ? MODE_BREAK : MODE_FLAT, doc.contents]
781
+ commands << [
782
+ indent,
783
+ doc.break? ? MODE_BREAK : MODE_FLAT,
784
+ doc.contents
785
+ ]
781
786
  else
782
787
  should_remeasure = false
783
788
  next_cmd = [indent, MODE_FLAT, doc.contents]
784
-
785
- if !doc.break? && fits?(next_cmd, commands, maxwidth - position)
786
- commands << next_cmd
789
+ commands << if !doc.break? &&
790
+ fits?(next_cmd, commands, maxwidth - position)
791
+ next_cmd
787
792
  else
788
- commands << [indent, MODE_BREAK, doc.contents]
793
+ [indent, MODE_BREAK, doc.contents]
789
794
  end
790
795
  end
791
796
  when IfBreak
792
- if mode == MODE_BREAK
793
- commands << [indent, mode, doc.break_contents] if doc.break_contents
794
- elsif mode == MODE_FLAT
795
- commands << [indent, mode, doc.flat_contents] if doc.flat_contents
797
+ if mode == MODE_BREAK && doc.break_contents.any?
798
+ commands << [indent, mode, doc.break_contents]
799
+ elsif mode == MODE_FLAT && doc.flat_contents.any?
800
+ commands << [indent, mode, doc.flat_contents]
796
801
  end
797
802
  when LineSuffix
798
803
  line_suffixes << [indent, mode, doc.contents, doc.priority]
@@ -1011,6 +1016,16 @@ class PrettyPrint
1011
1016
  IfBreakBuilder.new(self, doc)
1012
1017
  end
1013
1018
 
1019
+ # This is similar to if_break in that it also inserts an IfBreak node into the
1020
+ # print tree, however it's starting from the flat contents, and cannot be used
1021
+ # to build the break contents.
1022
+ def if_flat
1023
+ doc = IfBreak.new
1024
+ target << doc
1025
+
1026
+ with_target(doc.flat_contents) { yield }
1027
+ end
1028
+
1014
1029
  # Very similar to the #nest method, this indents the nested content by one
1015
1030
  # level by inserting an Indent node into the print tree. The contents of the
1016
1031
  # node are determined by the block.
@@ -1048,7 +1063,7 @@ class PrettyPrint
1048
1063
  def text(object = "", width = object.length)
1049
1064
  doc = target.last
1050
1065
 
1051
- unless Text === doc
1066
+ unless doc.is_a?(Text)
1052
1067
  doc = Text.new
1053
1068
  target << doc
1054
1069
  end
@@ -1116,10 +1131,10 @@ class PrettyPrint
1116
1131
  when Group
1117
1132
  commands << [indent, doc.break? ? MODE_BREAK : mode, doc.contents]
1118
1133
  when IfBreak
1119
- if mode == MODE_BREAK
1120
- commands << [indent, mode, doc.break_contents] if doc.break_contents
1121
- else
1122
- commands << [indent, mode, doc.flat_contents] if doc.flat_contents
1134
+ if mode == MODE_BREAK && doc.break_contents.any?
1135
+ commands << [indent, mode, doc.break_contents]
1136
+ elsif mode == MODE_FLAT && doc.flat_contents.any?
1137
+ commands << [indent, mode, doc.flat_contents]
1123
1138
  end
1124
1139
  when Breakable
1125
1140
  if mode == MODE_FLAT && !doc.force?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SyntaxTree
4
- VERSION = "2.2.0"
4
+ VERSION = "2.4.0"
5
5
  end