Class: Oppen::Config

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

Overview

Config.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(eager_print: false, indent_anchor: :end_of_previous_line, trim_trailing_whitespaces: false, upsize_stack: false) ⇒ Config

Returns a new instance of Config.

Examples:

:end_of_previous_line anchor

config = Oppen::Config.new(indent_anchor: :end_of_previous_line)
out = Oppen::Wadler.new config:, width: 13
out.text 'And she said:'
out.group(indent: 4) {
  out.group(indent: 4) {
    out.break
    out.text 'Hello, World!'
  }
}
out.output

# =>
# And she said:
#                  Hello, World!

:current_offset anchor

config = Oppen::Config.new(indent_anchor: :current_offset)
out = Oppen::Wadler.new config:, width: 13
out.text 'And she said:'
out.group(indent: 4) {
  out.group(indent: 4) {
    out.break
    out.text 'Hello, World!'
  }
}
out.output

# =>
# And she said:
#         Hello, World!

Parameters:

  • eager_print (Boolean) (defaults to: false)

    whether to eagerly print.

  • indent_anchor (Symbol) (defaults to: :end_of_previous_line)

    the different ways of handling the indentation of nested groups.

    • :end_of_previous_line: In the case of a new line in a nested group, the next string token will be displayed with indentation = previous line width + last group indentation. Defined in Oppen's paper.

    • :current_offset: When printing a new line in a nested group, the next string token will be displayed with an indentation equal to the sum of the indentations of all its parent groups. This is an extension to Oppen's work.

  • trim_trailing_whitespaces (Boolean) (defaults to: false)

    whether to trim trailing whitespaces.

  • upsize_stack (Boolean) (defaults to: false)

    whether to upsize stack when needed.



94
95
96
97
98
99
100
# File 'lib/oppen.rb', line 94

def initialize(eager_print: false, indent_anchor: :end_of_previous_line,
               trim_trailing_whitespaces: false, upsize_stack: false)
  @eager_print = eager_print
  @indent_anchor = indent_anchor
  @trim_trailing_whitespaces = trim_trailing_whitespaces
  @upsize_stack = upsize_stack
end

Instance Attribute Details

#indent_anchorObject

Returns the value of attribute indent_anchor.



43
44
45
# File 'lib/oppen.rb', line 43

def indent_anchor
  @indent_anchor
end

Class Method Details

.oppenConfig

Default configuration that provides printing behaviour identical to what's been described by Oppen.

Returns:



139
140
141
# File 'lib/oppen.rb', line 139

def self.oppen
  new
end

.wadler(eager_print: true, trim_trailing_whitespaces: true, upsize_stack: true) ⇒ Config

Configure the printer to behave more like ruby/prettyprint:

  1. groups are printed eagerly (we try to flush on a group's close).
  2. The indentation is anchored on the left margin.
  3. Trailing whitespaces are removed.

The name was amusingly chosen in reference to Wadler's work on pretty printing.

Returns:



155
156
157
158
159
160
161
162
# File 'lib/oppen.rb', line 155

def self.wadler(eager_print: true, trim_trailing_whitespaces: true, upsize_stack: true)
  new(
    eager_print:,
    indent_anchor: :current_offset,
    trim_trailing_whitespaces:,
    upsize_stack:,
  )
end

Instance Method Details

#eager_print?Boolean

Print groups eagerly.

Examples:

out = Oppen::Wadler.new(width: 13)
out.group {
  out.group {
    out.text 'abc'
    out.breakable
    out.text 'def'
  }
  out.group {
    out.text 'ghi'
    out.breakable
    out.text 'jkl'
  }
}
out.output

# eager_print: false =>
# abc
# defghi jkl
#
# eager_print: true =>
# abc defghi
#        jkl

Returns:

  • (Boolean)


129
# File 'lib/oppen.rb', line 129

def eager_print? = @eager_print

#trim_trailing_whitespaces?Boolean

Returns:

  • (Boolean)


131
# File 'lib/oppen.rb', line 131

def trim_trailing_whitespaces? = @trim_trailing_whitespaces

#upsize_stack?Boolean

Returns:

  • (Boolean)


133
# File 'lib/oppen.rb', line 133

def upsize_stack? = @upsize_stack