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.
    


373
374
375
376
377
# File 'lib/syntax_tree/prettyprint.rb', line 373

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.



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

def line_suffixes
  @line_suffixes
end

#outputObject (readonly)

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



354
355
356
# File 'lib/syntax_tree/prettyprint.rb', line 354

def output
  @output
end

#targetObject (readonly)

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



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

def target
  @target
end

Instance Method Details

#break_parentObject

Here for compatibility, does nothing.



403
404
# File 'lib/syntax_tree/prettyprint.rb', line 403

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.



393
394
395
396
397
398
399
400
# File 'lib/syntax_tree/prettyprint.rb', line 393

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.



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

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

#flushObject

Flushes the line suffixes onto the output buffer.



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

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.



429
430
431
432
433
434
435
436
437
438
439
# File 'lib/syntax_tree/prettyprint.rb', line 429

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.



452
453
454
# File 'lib/syntax_tree/prettyprint.rb', line 452

def if_break
  IfBreakBuilder.new
end

#indentObject

A noop that immediately yields.



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

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.



463
464
465
466
467
# File 'lib/syntax_tree/prettyprint.rb', line 463

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.



472
473
474
# File 'lib/syntax_tree/prettyprint.rb', line 472

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.



479
480
481
# File 'lib/syntax_tree/prettyprint.rb', line 479

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

#trimObject

Immediately trims the output buffer.



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

def trim
  target.trim!
end