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.
    


376
377
378
379
380
# File 'lib/syntax_tree/prettyprint.rb', line 376

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.



365
366
367
# File 'lib/syntax_tree/prettyprint.rb', line 365

def line_suffixes
  @line_suffixes
end

#outputObject (readonly)

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



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

def output
  @output
end

#targetObject (readonly)

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



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

def target
  @target
end

Instance Method Details

#break_parentObject

Here for compatibility, does nothing.



406
407
# File 'lib/syntax_tree/prettyprint.rb', line 406

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.



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

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.



411
412
413
# File 'lib/syntax_tree/prettyprint.rb', line 411

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

#flushObject

Flushes the line suffixes onto the output buffer.



383
384
385
# File 'lib/syntax_tree/prettyprint.rb', line 383

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.



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

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.



455
456
457
# File 'lib/syntax_tree/prettyprint.rb', line 455

def if_break
  IfBreakBuilder.new
end

#indentObject

A noop that immediately yields.



460
461
462
# File 'lib/syntax_tree/prettyprint.rb', line 460

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.



466
467
468
469
470
# File 'lib/syntax_tree/prettyprint.rb', line 466

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.



475
476
477
# File 'lib/syntax_tree/prettyprint.rb', line 475

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.



482
483
484
# File 'lib/syntax_tree/prettyprint.rb', line 482

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

#trimObject

Immediately trims the output buffer.



416
417
418
# File 'lib/syntax_tree/prettyprint.rb', line 416

def trim
  target.trim!
end