Class: Prettyrb::Writer

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/prettyrb/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder, indent_level: 0, max_length: 100, force_break: false) ⇒ Writer

Returns a new instance of Writer.



9
10
11
12
13
14
15
# File 'lib/prettyrb/writer.rb', line 9

def initialize(builder, indent_level: 0, max_length: 100, force_break: false)
  @builder = builder
  @writer = writer
  @indent_level = indent_level
  @max_length = max_length
  @force_break = force_break
end

Instance Method Details

#render_group(builder) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/prettyrb/writer.rb', line 17

def render_group(builder)
  # render without force
  # render only non-groups with force
  # render only groups with force
  # render non-groups and groups with force

  # without breaks
  content = builder.parts.compact.map do |part|
    write_child(part, force_break: false)
  end.compact.join("")

  # break self
  if content.split("\n").any? { |l| l.length > max_length }
    content = builder.parts.compact.map do |part|
      write_child(part, force_break: !part.is_a?(Document::Group))
    end.join("")
  end

  # break child groups if over length, but not self
  if content.split("\n").any? { |l| l.length > max_length }
    content = builder.parts.compact.map do |part|
      if part.is_a?(Document::Group)
        possible_output = write_child(part, force_break: false)

        if possible_output && possible_output.split("\n").any? { |l| l.length > max_length }
          content = write_child(part, force_break: true)
        else
          content = possible_output
        end
      else
        write_child(part, force_break: false)
      end
    end.join("")
  end

  # always break self, attempt to break child groups too
  if content.split("\n").any? { |l| l.length > max_length }
    content = builder.parts.compact.map do |part|
      if part.is_a?(Document::Group)
        possible_output = write_child(part, force_break: false)

        if possible_output && possible_output.split("\n").any? { |l| l.length > max_length }
          content = write_child(part, force_break: true)
        else
          content = possible_output
        end
      else
        write_child(part, force_break: true)
      end
    end.join("")
  end

  content
end

#to_sObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/prettyrb/writer.rb', line 72

def to_s
  case builder
  when Document::Join
    separator = break_up? ? builder.separator : builder.separator + " "
    parts = builder.parts.compact

    output = []
    parts.each do |part|
      output << write_child(part)
      output << separator if part != parts.last
    end
    output.join("")
  when Document::Concat
    builder.parts.compact.map { |p| write_child(p) }.compact.join("")
  when Document::Indent
    builder.parts.compact.map do |part|
      if builder.only_when_break && !break_up?
        write_child(part)
      else
        write_child(part, indent_level: indent_level + 1)
      end
    end.compact.join("")
  when Document::Dedent
    builder.parts.compact.map do |part|
      write_child(part, indent_level: indent_level - 1)
    end.compact.join("")
  when Document::Group
    render_group(builder)
  when Document::Hardline
    if builder.skip_indent
      "\n" * builder.count
    else
      "\n" * builder.count + indent_string
    end
  when Document::Softline
    if break_up?
      "\n" + indent_string
    else
      builder.fallback
    end
  when Document::IfBreak
    if break_up?
      builder.with_break
    else
      builder.without_break
    end
  when Symbol, String
    builder.to_s
  when nil
    nil
  else
    raise "unhandled type: #{builder.class}"
  end
end