Module: Oppen

Extended by:
Mixins
Defined in:
lib/oppen.rb,
lib/oppen/token.rb,
lib/oppen/mixins.rb,
lib/wadler/print.rb,
lib/oppen/printer.rb,
lib/oppen/version.rb,
lib/oppen/scan_stack.rb,
lib/oppen/print_stack.rb

Overview

Oppen.

Defined Under Namespace

Modules: Mixins Classes: Config, PrintStack, Printer, ScanStack, Token, Wadler

Constant Summary collapse

VERSION =

Oppen version

Returns:

  • (String)

    current version

'1.0.0'

Class Method Summary collapse

Methods included from Mixins

tokens_to_wadler

Class Method Details

.begin_consistent(offset: 2) ⇒ Token::Begin

In a consistent group, the presence of a new line inside the group will propagate to the other Break tokens in the group causing them all to act as a new line.

Examples:

Function Arguments

fun(
    arg1,
    arg2,
    arg3,
    arg4,
   )

Parameters:

  • offset (Integer) (defaults to: 2)

    the additional indentation of the group.

Returns:

See Also:



234
235
236
# File 'lib/oppen.rb', line 234

def self.begin_consistent(offset: 2)
  Token::Begin.new(break_type: :consistent, offset:)
end

.begin_inconsistent(offset: 2) ⇒ Token::Begin

In an inconsistent group, the presence of a new line inside the group will not propagate to the other Break tokens in the group letting them decide if they need to act as a new line or not.

Examples:

when used for the display of a function's arguments.

fun(
    arg1, arg2,
    arg3, arg4,
   )

Parameters:

  • offset (Integer) (defaults to: 2)

    the additional indentation of the group.

Returns:

See Also:



253
254
255
# File 'lib/oppen.rb', line 253

def self.begin_inconsistent(offset: 2)
  Token::Begin.new(break_type: :inconsistent, offset:)
end

.break(str = ' ', line_continuation: '', offset: 0, width: str.length) ⇒ Token::Break

Returns a new Break token.

Parameters:

  • str (String) (defaults to: ' ')

    value shown if no new line is needed.

  • line_continuation (String) (defaults to: '')

    printed before the line break.

  • offset (Integer) (defaults to: 0)

    additional indentation to be added to the current indentation level.

  • width (Integer) (defaults to: str.length)

    the string's effective width. Useful when printing HTML, e.g. <span>value</span>, where the effective width is that of the inner text.

Returns:

See Also:



198
199
200
# File 'lib/oppen.rb', line 198

def self.break(str = ' ', line_continuation: '', offset: 0, width: str.length)
  Token::Break.new(str, width:, line_continuation:, offset:)
end

.endToken::End

Returns a new End token.

Returns:



258
259
260
# File 'lib/oppen.rb', line 258

def self.end
  Token::End.new
end

.eofToken::EOF

Returns a new EOF token.

Returns:



263
264
265
# File 'lib/oppen.rb', line 263

def self.eof
  Token::EOF.new
end

.line_break(line_continuation: '', offset: 0) ⇒ Token::LineBreak

Returns a new LineBreak token.

Parameters:

  • line_continuation (String) (defaults to: '')

    printed before the line break.

  • offset (Integer) (defaults to: 0)

    additional indentation to be added to the current indentation level.

Returns:

See Also:



211
212
213
# File 'lib/oppen.rb', line 211

def self.line_break(line_continuation: '', offset: 0)
  Token::LineBreak.new(line_continuation:, offset:)
end

Entry point of the pretty printer.

Parameters:

  • config (Config) (defaults to: Config.oppen)

    to customize the printer's behavior.

  • new_line (String) (defaults to: "\n")

    the delimiter between lines.

  • out (Object) (defaults to: StringIO.new)

    the output string buffer. It should have a write and string methods.

  • space (String, Proc) (defaults to: ' ')

    indentation string or a string generator.

    • If a String, spaces will be generated with the the lambda ->(n){ space * n }, where n is the number of columns to indent.
    • If a Proc, it will receive n and it needs to return a String.
  • tokens (Array<Token>) (defaults to: [])

    the list of tokens to be printed.

  • width (Integer) (defaults to: 80)

    maximum line width desired.

Returns:

  • (String)

    output of the pretty printer.



32
33
34
35
36
37
38
39
# File 'lib/oppen.rb', line 32

def self.print(config: Config.oppen, new_line: "\n",
               out: StringIO.new, space: ' ', tokens: [], width: 80)
  printer = Printer.new width, new_line, config, space, out
  tokens.each do |token|
    printer.print token
  end
  printer.output
end

.string(value, width: value.length) ⇒ Token::String

Returns a new String token.

Parameters:

  • value (String)

    the string to print.

  • width (Integer) (defaults to: value.length)

    the string's effective width. Useful when printing HTML, e.g. <span>value</span>, where the effective width is that of the inner text.

Returns:



174
175
176
# File 'lib/oppen.rb', line 174

def self.string(value, width: value.length)
  Token::String.new(value, width:)
end

.whitespace(value) ⇒ Token::Whitespace

Returns a new Whitespace token.

Returns:



179
180
181
# File 'lib/oppen.rb', line 179

def self.whitespace(value)
  Token::Whitespace.new(value, width: value.bytesize)
end