prettyrb 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/Gemfile.lock +1 -1
- data/lib/prettyrb.rb +12 -0
- data/lib/prettyrb/builder.rb +6 -1
- data/lib/prettyrb/cli.rb +15 -6
- data/lib/prettyrb/document.rb +164 -0
- data/lib/prettyrb/formatter.rb +1 -10
- data/lib/prettyrb/nodes/and_node.rb +11 -0
- data/lib/prettyrb/nodes/base_node.rb +5 -1
- data/lib/prettyrb/nodes/def_node.rb +17 -0
- data/lib/prettyrb/nodes/if_node.rb +17 -1
- data/lib/prettyrb/nodes/logical_operator_helper.rb +13 -0
- data/lib/prettyrb/nodes/or_node.rb +11 -0
- data/lib/prettyrb/nodes/regexp_node.rb +41 -0
- data/lib/prettyrb/nodes/send_node.rb +63 -0
- data/lib/prettyrb/nodes/string_helper.rb +17 -1
- data/lib/prettyrb/version.rb +1 -1
- data/lib/prettyrb/visitor.rb +820 -636
- data/lib/prettyrb/writer.rb +157 -0
- metadata +11 -3
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Prettyrb
|
4
|
+
class Writer
|
5
|
+
extend Forwardable
|
6
|
+
|
7
|
+
def_delegators :@writer, :indent_level
|
8
|
+
|
9
|
+
def initialize(builder, indent_level: 0, max_length: 100, force_break: false)
|
10
|
+
@builder = builder
|
11
|
+
@writer = writer
|
12
|
+
@indent_level = indent_level
|
13
|
+
@max_length = max_length
|
14
|
+
@force_break = force_break
|
15
|
+
end
|
16
|
+
|
17
|
+
def render_group(builder)
|
18
|
+
# render without force
|
19
|
+
# render only non-groups with force
|
20
|
+
# render only groups with force
|
21
|
+
# render non-groups and groups with force
|
22
|
+
|
23
|
+
# without breaks
|
24
|
+
content = builder.parts.compact.map do |part|
|
25
|
+
write_child(part, force_break: false)
|
26
|
+
end.compact.join("")
|
27
|
+
|
28
|
+
# break self
|
29
|
+
if content.split("\n").any? { |l| l.length > max_length }
|
30
|
+
content = builder.parts.compact.map do |part|
|
31
|
+
write_child(part, force_break: !part.is_a?(Document::Group))
|
32
|
+
end.join("")
|
33
|
+
end
|
34
|
+
|
35
|
+
# break child groups if over length, but not self
|
36
|
+
if content.split("\n").any? { |l| l.length > max_length }
|
37
|
+
content = builder.parts.compact.map do |part|
|
38
|
+
if part.is_a?(Document::Group)
|
39
|
+
possible_output = write_child(part, force_break: false)
|
40
|
+
|
41
|
+
if possible_output && possible_output.split("\n").any? { |l| l.length > max_length }
|
42
|
+
content = write_child(part, force_break: true)
|
43
|
+
else
|
44
|
+
content = possible_output
|
45
|
+
end
|
46
|
+
else
|
47
|
+
write_child(part, force_break: false)
|
48
|
+
end
|
49
|
+
end.join("")
|
50
|
+
end
|
51
|
+
|
52
|
+
# always break self, attempt to break child groups too
|
53
|
+
if content.split("\n").any? { |l| l.length > max_length }
|
54
|
+
content = builder.parts.compact.map do |part|
|
55
|
+
if part.is_a?(Document::Group)
|
56
|
+
possible_output = write_child(part, force_break: false)
|
57
|
+
|
58
|
+
if possible_output && possible_output.split("\n").any? { |l| l.length > max_length }
|
59
|
+
content = write_child(part, force_break: true)
|
60
|
+
else
|
61
|
+
content = possible_output
|
62
|
+
end
|
63
|
+
else
|
64
|
+
write_child(part, force_break: true)
|
65
|
+
end
|
66
|
+
end.join("")
|
67
|
+
end
|
68
|
+
|
69
|
+
content
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_s
|
73
|
+
case builder
|
74
|
+
when Document::Join
|
75
|
+
separator = break_up? ? builder.separator : builder.separator + " "
|
76
|
+
parts = builder.parts.compact
|
77
|
+
|
78
|
+
output = []
|
79
|
+
parts.each do |part|
|
80
|
+
output << write_child(part)
|
81
|
+
output << separator if part != parts.last
|
82
|
+
end
|
83
|
+
output.join("")
|
84
|
+
when Document::Concat
|
85
|
+
builder.parts.compact.map { |p| write_child(p) }.compact.join("")
|
86
|
+
when Document::Indent
|
87
|
+
builder.parts.compact.map do |part|
|
88
|
+
if builder.only_when_break && !break_up?
|
89
|
+
write_child(part)
|
90
|
+
else
|
91
|
+
write_child(part, indent_level: indent_level + 1)
|
92
|
+
end
|
93
|
+
end.compact.join("")
|
94
|
+
when Document::Dedent
|
95
|
+
builder.parts.compact.map do |part|
|
96
|
+
write_child(part, indent_level: indent_level - 1)
|
97
|
+
end.compact.join("")
|
98
|
+
when Document::Group
|
99
|
+
render_group(builder)
|
100
|
+
when Document::Hardline
|
101
|
+
if builder.skip_indent
|
102
|
+
"\n" * builder.count
|
103
|
+
else
|
104
|
+
"\n" * builder.count + indent_string
|
105
|
+
end
|
106
|
+
when Document::Softline
|
107
|
+
if break_up?
|
108
|
+
"\n" + indent_string
|
109
|
+
else
|
110
|
+
builder.fallback
|
111
|
+
end
|
112
|
+
when Document::IfBreak
|
113
|
+
if break_up?
|
114
|
+
builder.with_break
|
115
|
+
else
|
116
|
+
builder.without_break
|
117
|
+
end
|
118
|
+
when Symbol, String
|
119
|
+
builder.to_s
|
120
|
+
when nil
|
121
|
+
nil
|
122
|
+
else
|
123
|
+
raise "unhandled type: #{builder.class}"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
protected
|
128
|
+
|
129
|
+
attr_reader :builder, :writer, :indent_level, :break_group_levels, :max_length, :force_break
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def break_up?
|
134
|
+
force_break
|
135
|
+
end
|
136
|
+
|
137
|
+
def write_softline?
|
138
|
+
break_up?
|
139
|
+
end
|
140
|
+
|
141
|
+
def indent_string(extra: 0)
|
142
|
+
" " * (indent_level + extra)
|
143
|
+
end
|
144
|
+
|
145
|
+
def write_child(child, indent_level: nil, group_level: nil, force_break: nil)
|
146
|
+
indent_level ||= self.indent_level
|
147
|
+
force_break ||= self.force_break
|
148
|
+
|
149
|
+
self.class.new(
|
150
|
+
child,
|
151
|
+
indent_level: indent_level,
|
152
|
+
max_length: max_length,
|
153
|
+
force_break: force_break,
|
154
|
+
).to_s
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prettyrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parser
|
@@ -131,14 +131,22 @@ files:
|
|
131
131
|
- lib/prettyrb.rb
|
132
132
|
- lib/prettyrb/builder.rb
|
133
133
|
- lib/prettyrb/cli.rb
|
134
|
+
- lib/prettyrb/document.rb
|
134
135
|
- lib/prettyrb/formatter.rb
|
136
|
+
- lib/prettyrb/nodes/and_node.rb
|
135
137
|
- lib/prettyrb/nodes/base_node.rb
|
138
|
+
- lib/prettyrb/nodes/def_node.rb
|
136
139
|
- lib/prettyrb/nodes/dstr_node.rb
|
137
140
|
- lib/prettyrb/nodes/if_node.rb
|
141
|
+
- lib/prettyrb/nodes/logical_operator_helper.rb
|
142
|
+
- lib/prettyrb/nodes/or_node.rb
|
143
|
+
- lib/prettyrb/nodes/regexp_node.rb
|
144
|
+
- lib/prettyrb/nodes/send_node.rb
|
138
145
|
- lib/prettyrb/nodes/str_node.rb
|
139
146
|
- lib/prettyrb/nodes/string_helper.rb
|
140
147
|
- lib/prettyrb/version.rb
|
141
148
|
- lib/prettyrb/visitor.rb
|
149
|
+
- lib/prettyrb/writer.rb
|
142
150
|
- prettyrb.gemspec
|
143
151
|
homepage: https://github.com/blakewilliams/prettyrb
|
144
152
|
licenses:
|
@@ -160,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
168
|
- !ruby/object:Gem::Version
|
161
169
|
version: '0'
|
162
170
|
requirements: []
|
163
|
-
rubygems_version: 3.1
|
171
|
+
rubygems_version: 3.0.1
|
164
172
|
signing_key:
|
165
173
|
specification_version: 4
|
166
174
|
summary: Ruby source code formatter
|