Class: PrettyPrint::SingleLine

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

Overview

PrettyPrint::SingleLine is used by PrettyPrint.singleline_format

It is passed to be similar to a PrettyPrint object itself, by responding to all of the same print tree node builder methods, as well as the #flush method.

The significant difference here is that there are no line breaks in the output. If an IfBreak node is used, only the flat contents are printed. LineSuffix nodes are printed at the end of the buffer when #flush is called.

Defined Under Namespace

Classes: IfBreakBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output, maxwidth = nil, newline = nil) ⇒ SingleLine

Create a PrettyPrint::SingleLine object

Arguments:

  • output - String (or similar) to store rendered text. Needs to respond

    to '<<'.
    
  • maxwidth - Argument position expected to be here for compatibility.

    This argument is a noop.
    
  • newline - Argument position expected to be here for compatibility.

    This argument is a noop.
    


378
379
380
381
382
# File 'lib/syntax_tree/prettyprint.rb', line 378

def initialize(output, maxwidth = nil, newline = nil)
  @output = Buffer.for(output)
  @target = @output
  @line_suffixes = Buffer::ArrayBuffer.new
end

Instance Attribute Details

#line_suffixesObject (readonly)

A buffer output that wraps any calls to line_suffix that will be flushed at the end of printing.



367
368
369
# File 'lib/syntax_tree/prettyprint.rb', line 367

def line_suffixes
  @line_suffixes
end

#outputObject (readonly)

The output object. It stores rendered text and should respond to <<.



359
360
361
# File 'lib/syntax_tree/prettyprint.rb', line 359

def output
  @output
end

#targetObject (readonly)

The current array of contents that the print tree builder methods should append to.



363
364
365
# File 'lib/syntax_tree/prettyprint.rb', line 363

def target
  @target
end

Instance Method Details

#break_parentObject

Here for compatibility, does nothing.



408
409
# File 'lib/syntax_tree/prettyprint.rb', line 408

def break_parent
end

#breakable(separator = " ", width = separator.length, indent: nil, force: nil) ⇒ Object

Appends separator to the text to be output. By default separator is ‘ ’

The width, indent, and force arguments are here for compatibility. They are all noop arguments.



398
399
400
401
402
403
404
405
# File 'lib/syntax_tree/prettyprint.rb', line 398

def breakable(
  separator = " ",
  width = separator.length,
  indent: nil,
  force: nil
)
  target << separator
end

#fill_breakable(separator = " ", width = separator.length) ⇒ Object

Appends separator to the output buffer. width is a noop here for compatibility.



413
414
415
# File 'lib/syntax_tree/prettyprint.rb', line 413

def fill_breakable(separator = " ", width = separator.length)
  target << separator
end

#flushObject

Flushes the line suffixes onto the output buffer.



385
386
387
# File 'lib/syntax_tree/prettyprint.rb', line 385

def flush
  line_suffixes.output.each { |doc| output << doc }
end

#group(indent = nil, open_object = "", close_object = "", open_width = nil, close_width = nil) ⇒ Object

Opens a block for grouping objects to be pretty printed.

Arguments:

  • indent - noop argument. Present for compatibility.

  • open_obj - text appended before the &block. Default is ”

  • close_obj - text appended after the &block. Default is ”

  • open_width - noop argument. Present for compatibility.

  • close_width - noop argument. Present for compatibility.



434
435
436
437
438
439
440
441
442
443
444
# File 'lib/syntax_tree/prettyprint.rb', line 434

def group(
  indent = nil,
  open_object = "",
  close_object = "",
  open_width = nil,
  close_width = nil
)
  target << open_object
  yield
  target << close_object
end

#if_breakObject

Effectively unnecessary, but here for compatibility.



457
458
459
# File 'lib/syntax_tree/prettyprint.rb', line 457

def if_break
  IfBreakBuilder.new
end

#indentObject

A noop that immediately yields.



462
463
464
# File 'lib/syntax_tree/prettyprint.rb', line 462

def indent
  yield
end

#line_suffixObject

Changes the target output buffer to the line suffix output buffer which will get flushed at the end of printing.



468
469
470
471
472
# File 'lib/syntax_tree/prettyprint.rb', line 468

def line_suffix
  previous_target, @target = @target, line_suffixes
  yield
  @target = previous_target
end

#nest(indent) ⇒ Object

Takes indent arg, but does nothing with it.

Yields to a block.



477
478
479
# File 'lib/syntax_tree/prettyprint.rb', line 477

def nest(indent)
  yield
end

#text(object = "", width = nil) ⇒ Object

Add object to the text to be output.

width argument is here for compatibility. It is a noop argument.



484
485
486
# File 'lib/syntax_tree/prettyprint.rb', line 484

def text(object = "", width = nil)
  target << object
end

#trimObject

Immediately trims the output buffer.



418
419
420
# File 'lib/syntax_tree/prettyprint.rb', line 418

def trim
  target.trim!
end