Class: Oppen::Wadler
- Inherits:
-
Object
- Object
- Oppen::Wadler
- Defined in:
- lib/wadler/print.rb
Overview
Wadler.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#current_indent ⇒ Object
readonly
Returns the value of attribute current_indent.
-
#new_line ⇒ Object
readonly
Returns the value of attribute new_line.
-
#out ⇒ Object
readonly
Returns the value of attribute out.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #break(line_continuation: '') ⇒ Nil
- #breakable(str = ' ', width: str.length, line_continuation: '') ⇒ Nil
- #group(indent = 0, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) { ... } ⇒ Nil
-
#initialize(config: Config.wadler, space: ' ', new_line: "\n", out: StringIO.new, width: 80) ⇒ Wadler
constructor
A new instance of Wadler.
- #nest(indent, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) ⇒ Nil
- #output ⇒ String
- #text(value, width: value.length) ⇒ Nil
Constructor Details
#initialize(config: Config.wadler, space: ' ', new_line: "\n", out: StringIO.new, width: 80) ⇒ Wadler
Returns a new instance of Wadler.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wadler/print.rb', line 25 def initialize(config: Config.wadler, space: ' ', new_line: "\n", out: StringIO.new, width: 80) @config = config @current_indent = 0 @space = space @width = width @new_line = new_line @out = out @tokens = [] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/wadler/print.rb', line 7 def config @config end |
#current_indent ⇒ Object (readonly)
Returns the value of attribute current_indent.
8 9 10 |
# File 'lib/wadler/print.rb', line 8 def current_indent @current_indent end |
#new_line ⇒ Object (readonly)
Returns the value of attribute new_line.
10 11 12 |
# File 'lib/wadler/print.rb', line 10 def new_line @new_line end |
#out ⇒ Object (readonly)
Returns the value of attribute out.
11 12 13 |
# File 'lib/wadler/print.rb', line 11 def out @out end |
#space ⇒ Object (readonly)
Returns the value of attribute space.
9 10 11 |
# File 'lib/wadler/print.rb', line 9 def space @space end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
12 13 14 |
# File 'lib/wadler/print.rb', line 12 def tokens @tokens end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
13 14 15 |
# File 'lib/wadler/print.rb', line 13 def width @width end |
Instance Method Details
#break(line_continuation: '') ⇒ Nil
128 129 130 |
# File 'lib/wadler/print.rb', line 128 def break(line_continuation: '') tokens << Oppen.line_break(line_continuation:, offset: current_indent) end |
#breakable(str = ' ', width: str.length, line_continuation: '') ⇒ Nil
121 122 123 |
# File 'lib/wadler/print.rb', line 121 def breakable(str = ' ', width: str.length, line_continuation: '') tokens << Oppen.break(str, width:, line_continuation:, offset: current_indent) end |
#group(indent = 0, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) { ... } ⇒ Nil
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wadler/print.rb', line 52 def group(indent = 0, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) raise ArgumentError, "#{open_obj.nil? ? 'open_obj' : 'close_obj'} cannot be nil" \ if open_obj.nil? || close_obj.nil? tokens << case break_type in Oppen::Token::BreakType::CONSISTENT Oppen.begin_consistent(offset: indent) in Oppen::Token::BreakType::INCONSISTENT Oppen.begin_inconsistent(offset: indent) end if !open_obj.empty? self.break text(open_obj) end yield if !close_obj.empty? self.break text(close_obj) end tokens << Oppen.end end |
#nest(indent, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) ⇒ Nil
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/wadler/print.rb', line 86 def nest(indent, open_obj = '', close_obj = '', break_type = Oppen::Token::BreakType::CONSISTENT) raise ArgumentError, "#{open_obj.nil? ? 'open_obj' : 'close_obj'} cannot be nil" \ if open_obj.nil? || close_obj.nil? @current_indent += indent if !open_obj.empty? text(open_obj) self.break end begin yield ensure @current_indent -= indent end return if close_obj.empty? self.break text(close_obj) end |