Class: PrettierPrint::SingleLine

Inherits:
Object
  • Object
show all
Defined in:
lib/prettier_print/single_line.rb

Overview

PrettierPrint::SingleLine is used by PrettierPrint.singleline_format

It is passed to be similar to a PrettierPrint 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 PrettierPrint::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.
    


34
35
36
37
38
# File 'lib/prettier_print/single_line.rb', line 34

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.



23
24
25
# File 'lib/prettier_print/single_line.rb', line 23

def line_suffixes
  @line_suffixes
end

#outputObject (readonly)

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



15
16
17
# File 'lib/prettier_print/single_line.rb', line 15

def output
  @output
end

#targetObject (readonly)

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



19
20
21
# File 'lib/prettier_print/single_line.rb', line 19

def target
  @target
end

Instance Method Details

#break_parentObject

Here for compatibility, does nothing.



64
65
# File 'lib/prettier_print/single_line.rb', line 64

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.



54
55
56
57
58
59
60
61
# File 'lib/prettier_print/single_line.rb', line 54

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.



69
70
71
# File 'lib/prettier_print/single_line.rb', line 69

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

#flushObject

Flushes the line suffixes onto the output buffer.



41
42
43
# File 'lib/prettier_print/single_line.rb', line 41

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.



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/prettier_print/single_line.rb', line 90

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.



113
114
115
# File 'lib/prettier_print/single_line.rb', line 113

def if_break
  IfBreakBuilder.new
end

#if_flatObject

Also effectively unnecessary, but here for compatibility.



118
119
# File 'lib/prettier_print/single_line.rb', line 118

def if_flat
end

#indentObject

A noop that immediately yields.



122
123
124
# File 'lib/prettier_print/single_line.rb', line 122

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.



128
129
130
131
132
# File 'lib/prettier_print/single_line.rb', line 128

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.



137
138
139
# File 'lib/prettier_print/single_line.rb', line 137

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.



144
145
146
# File 'lib/prettier_print/single_line.rb', line 144

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

#trimObject

Immediately trims the output buffer.



74
75
76
# File 'lib/prettier_print/single_line.rb', line 74

def trim
  target.trim!
end