prettyrb 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +24 -0
- data/Gemfile.lock +10 -6
- data/README.md +3 -1
- data/exe/prettyrb +6 -0
- data/lib/prettyrb/cli.rb +11 -0
- data/lib/prettyrb/formatter.rb +34 -0
- data/lib/prettyrb/version.rb +1 -1
- data/lib/prettyrb/visitor.rb +412 -0
- data/lib/prettyrb.rb +6 -186
- data/prettyrb.gemspec +4 -2
- metadata +42 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea94d815e156b5294c602a74176b57ad6d7f368dbba00e9f5435fec4921c5bb5
|
4
|
+
data.tar.gz: 5e58bd43571e3565dc88bf97fa5f36204f09edd68b7aa15190e1a58a2c9aa6b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 002b78f6a4ad5b5f94c05ac6fd78e685bddc9901b8dc6d29e5170da65933b3159c04c76d5d38a2a4ec645b5936bcb44cc8fae273a39dafbef8a4fd7d1d691100
|
7
|
+
data.tar.gz: ba85b4cc39ace86efe1639e83e7e307ea2a7b22e0b8574fcc490e9ed848bb71dacab2040592729fe0b24c6962a9eab4031f5ccf96a78bcf35f30ebc5387642d6
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby 2.6
|
17
|
+
uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: 2.6.x
|
20
|
+
- name: Build and test with Rake
|
21
|
+
run: |
|
22
|
+
gem install bundler
|
23
|
+
bundle install --jobs 4 --retry 3
|
24
|
+
bundle exec rake
|
data/Gemfile.lock
CHANGED
@@ -2,25 +2,29 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
prettyrb (0.1.0)
|
5
|
-
|
5
|
+
parser (~> 2.7.0.5)
|
6
|
+
thor
|
6
7
|
|
7
8
|
GEM
|
8
9
|
remote: https://rubygems.org/
|
9
10
|
specs:
|
11
|
+
ast (2.4.0)
|
12
|
+
byebug (11.1.1)
|
10
13
|
minitest (5.14.0)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
parser (2.7.0.5)
|
15
|
+
ast (~> 2.4.0)
|
16
|
+
rake (12.3.3)
|
17
|
+
thor (1.0.1)
|
15
18
|
|
16
19
|
PLATFORMS
|
17
20
|
ruby
|
18
21
|
|
19
22
|
DEPENDENCIES
|
20
23
|
bundler (~> 1.17)
|
24
|
+
byebug (~> 11.1)
|
21
25
|
minitest (~> 5.0)
|
22
26
|
prettyrb!
|
23
|
-
rake (~>
|
27
|
+
rake (~> 12.3.3)
|
24
28
|
|
25
29
|
BUNDLED WITH
|
26
30
|
1.17.2
|
data/README.md
CHANGED
data/exe/prettyrb
ADDED
data/lib/prettyrb/cli.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Prettyrb
|
2
|
+
MAX_LINE_LENGTH = 100
|
3
|
+
|
4
|
+
class Formatter
|
5
|
+
def self.for(node)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(code)
|
9
|
+
@code = code
|
10
|
+
end
|
11
|
+
|
12
|
+
def format
|
13
|
+
root_node, comments = Parser::CurrentRuby.parse_with_comments(@code)
|
14
|
+
|
15
|
+
visitor = Visitor.new
|
16
|
+
visitor.visit(root_node)
|
17
|
+
|
18
|
+
visitor.output
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def format_type(node, indentation)
|
24
|
+
case node.node_type
|
25
|
+
when :if
|
26
|
+
IfFormatter.new(node, indentation).format
|
27
|
+
else
|
28
|
+
raise "can't handle #{node}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :code
|
33
|
+
end
|
34
|
+
end
|
data/lib/prettyrb/version.rb
CHANGED
@@ -0,0 +1,412 @@
|
|
1
|
+
module Prettyrb
|
2
|
+
class Visitor
|
3
|
+
MAX_LENGTH = 100
|
4
|
+
|
5
|
+
attr_reader :output
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@indent_level = 0
|
9
|
+
@output = ""
|
10
|
+
|
11
|
+
@newline = true
|
12
|
+
@multiline_conditional_level = 0
|
13
|
+
@__current_node = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def indents
|
17
|
+
' ' * @indent_level
|
18
|
+
end
|
19
|
+
|
20
|
+
def dedent(&block)
|
21
|
+
old_indent_level = @indent_level
|
22
|
+
|
23
|
+
@indent_level = [0, @indent_level - 1].max
|
24
|
+
yield
|
25
|
+
@indent_level = old_indent_level
|
26
|
+
end
|
27
|
+
|
28
|
+
def indent(&block)
|
29
|
+
old_indent_level = @indent_level
|
30
|
+
|
31
|
+
@indent_level += 1
|
32
|
+
yield
|
33
|
+
@indent_level = old_indent_level
|
34
|
+
end
|
35
|
+
|
36
|
+
def multiline_conditional_level
|
37
|
+
@multiline_conditional_level
|
38
|
+
end
|
39
|
+
|
40
|
+
def in_multiline_conditional(&block)
|
41
|
+
old_multiline_condtiional_level = @multiline_conditional_level
|
42
|
+
@multiline_conditional_level += 1
|
43
|
+
@multiline_condtiional_level = 0
|
44
|
+
yield
|
45
|
+
@multiline_conditional_level = old_multiline_condtiional_level
|
46
|
+
end
|
47
|
+
|
48
|
+
def capture(&block)
|
49
|
+
old_newline = @newline
|
50
|
+
old_output = @output
|
51
|
+
|
52
|
+
@output = ""
|
53
|
+
|
54
|
+
yield
|
55
|
+
|
56
|
+
@output.tap do |output|
|
57
|
+
@output = old_output
|
58
|
+
@newline = old_newline
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def newline
|
63
|
+
@output << "\n"
|
64
|
+
@newline = true
|
65
|
+
end
|
66
|
+
|
67
|
+
def write(input)
|
68
|
+
if @newline
|
69
|
+
@output << indents
|
70
|
+
end
|
71
|
+
@newline = false
|
72
|
+
@output << input
|
73
|
+
end
|
74
|
+
|
75
|
+
def visit(node)
|
76
|
+
@previous_node = @__current_node
|
77
|
+
@__current_node = node
|
78
|
+
|
79
|
+
case node.type
|
80
|
+
when :module
|
81
|
+
write "module "
|
82
|
+
visit node.children[0]
|
83
|
+
newline
|
84
|
+
|
85
|
+
indent do
|
86
|
+
visit node.children[1]
|
87
|
+
end
|
88
|
+
|
89
|
+
write "end"
|
90
|
+
newline
|
91
|
+
when :class
|
92
|
+
write "class "
|
93
|
+
visit node.children[0]
|
94
|
+
newline
|
95
|
+
# TODO handle children[1] which is inheritance
|
96
|
+
|
97
|
+
indent do
|
98
|
+
visit node.children[2]
|
99
|
+
end
|
100
|
+
|
101
|
+
newline unless @output.end_with?("\n")
|
102
|
+
write "end"
|
103
|
+
newline
|
104
|
+
when :const
|
105
|
+
write node.children[1].to_s
|
106
|
+
when :casgn
|
107
|
+
write node.children[1].to_s
|
108
|
+
write " = "
|
109
|
+
visit node.children[2]
|
110
|
+
when :block
|
111
|
+
visit node.children[0]
|
112
|
+
write " do"
|
113
|
+
|
114
|
+
if node.children[1].children.length > 0
|
115
|
+
write " |"
|
116
|
+
visit node.children[1]
|
117
|
+
write "|"
|
118
|
+
end
|
119
|
+
|
120
|
+
newline
|
121
|
+
|
122
|
+
indent do
|
123
|
+
visit node.children[2]
|
124
|
+
end
|
125
|
+
|
126
|
+
newline
|
127
|
+
|
128
|
+
write "end"
|
129
|
+
newline
|
130
|
+
when :send
|
131
|
+
if [:!=, :==, :+, :-, :*, :/, :<<].include?(node.children[1])
|
132
|
+
visit node.children[0]
|
133
|
+
write " "
|
134
|
+
write node.children[1].to_s
|
135
|
+
write " "
|
136
|
+
visit node.children[2]
|
137
|
+
elsif node.children[1] == :[]
|
138
|
+
visit node.children[0]
|
139
|
+
write "["
|
140
|
+
visit node.children[2]
|
141
|
+
write "]"
|
142
|
+
elsif node.children[1] == :!
|
143
|
+
write "!"
|
144
|
+
visit node.children[0]
|
145
|
+
elsif node.children[0] == nil
|
146
|
+
write node.children[1].to_s
|
147
|
+
|
148
|
+
# TODO possible > MAX via `capture`
|
149
|
+
arguments = node.children[2..-1]
|
150
|
+
if arguments.length > 0
|
151
|
+
write "("
|
152
|
+
arguments.each_with_index do |node, index|
|
153
|
+
visit node
|
154
|
+
write ", " unless index == arguments.length - 1
|
155
|
+
end
|
156
|
+
write ")"
|
157
|
+
end
|
158
|
+
|
159
|
+
newline if @previous_node.type == :class
|
160
|
+
else
|
161
|
+
visit node.children[0]
|
162
|
+
write "."
|
163
|
+
write node.children[1].to_s
|
164
|
+
|
165
|
+
# TODO possible > MAX via `capture`
|
166
|
+
arguments = node.children[2..-1]
|
167
|
+
if arguments.length > 0
|
168
|
+
write "("
|
169
|
+
arguments.each_with_index do |node, index|
|
170
|
+
visit node
|
171
|
+
write ", " unless index == arguments.length - 1
|
172
|
+
end
|
173
|
+
write ")"
|
174
|
+
end
|
175
|
+
end
|
176
|
+
when :if
|
177
|
+
write "if"
|
178
|
+
indent do
|
179
|
+
conditions = capture do
|
180
|
+
visit node.children[0]
|
181
|
+
end
|
182
|
+
|
183
|
+
if !conditions.start_with?("\n")
|
184
|
+
write(" ")
|
185
|
+
end
|
186
|
+
write conditions
|
187
|
+
|
188
|
+
newline
|
189
|
+
|
190
|
+
if node.children[1]
|
191
|
+
visit node.children[1]
|
192
|
+
newline
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
if node.children[2]
|
197
|
+
if node.children[2] == :if
|
198
|
+
write "elsif"
|
199
|
+
visit node.children[2]
|
200
|
+
else
|
201
|
+
write "else"
|
202
|
+
newline
|
203
|
+
|
204
|
+
indent do
|
205
|
+
visit node.children[2]
|
206
|
+
end
|
207
|
+
end
|
208
|
+
newline
|
209
|
+
end
|
210
|
+
|
211
|
+
write "end"
|
212
|
+
when :true
|
213
|
+
write "true"
|
214
|
+
when :false
|
215
|
+
write "false"
|
216
|
+
when :nil
|
217
|
+
write "nil"
|
218
|
+
when :int
|
219
|
+
write node.children[0].to_s
|
220
|
+
when :array
|
221
|
+
possible_output = capture do
|
222
|
+
write "["
|
223
|
+
node.children.each_with_index do |child, index|
|
224
|
+
visit(child)
|
225
|
+
write ", " unless index == node.children.length - 1
|
226
|
+
end
|
227
|
+
write "]"
|
228
|
+
end
|
229
|
+
|
230
|
+
if possible_output.length > MAX_LENGTH
|
231
|
+
write "["
|
232
|
+
newline
|
233
|
+
|
234
|
+
indent do
|
235
|
+
node.children.map do |child|
|
236
|
+
visit(child)
|
237
|
+
write ","
|
238
|
+
newline
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
write "]"
|
243
|
+
else
|
244
|
+
write possible_output.lstrip
|
245
|
+
end
|
246
|
+
when :str
|
247
|
+
write '"'
|
248
|
+
write node.children[0].gsub("\n", "\\n")
|
249
|
+
write '"'
|
250
|
+
when :dstr
|
251
|
+
write "\""
|
252
|
+
node.children.map do |child|
|
253
|
+
if child.type == :str
|
254
|
+
write child.children[0]
|
255
|
+
write child.children[0].gsub("\n", "\\n")
|
256
|
+
else
|
257
|
+
write '#{'
|
258
|
+
visit child
|
259
|
+
write '}'
|
260
|
+
end
|
261
|
+
end
|
262
|
+
write "\""
|
263
|
+
when :begin
|
264
|
+
if @previous_node.type == :or || @previous_node.type == :and
|
265
|
+
write "("
|
266
|
+
node.children.map { |child| visit child }
|
267
|
+
write ")"
|
268
|
+
else
|
269
|
+
node.children.each_with_index do |child, index|
|
270
|
+
visit child
|
271
|
+
newline unless index == node.children.length - 1
|
272
|
+
end
|
273
|
+
end
|
274
|
+
when :or, :and
|
275
|
+
possible_output = capture do
|
276
|
+
visit node.children[0]
|
277
|
+
if node.type == :or
|
278
|
+
write " || "
|
279
|
+
elsif node.type == :and
|
280
|
+
write " && "
|
281
|
+
end
|
282
|
+
visit node.children[1]
|
283
|
+
end
|
284
|
+
|
285
|
+
|
286
|
+
if @multiline_conditional_level > 0 # TODO track and check currently level
|
287
|
+
write_multiline_conditional(node)
|
288
|
+
elsif possible_output.length > MAX_LENGTH
|
289
|
+
in_multiline_conditional do
|
290
|
+
newline
|
291
|
+
write_multiline_conditional(node)
|
292
|
+
end
|
293
|
+
|
294
|
+
dedent do
|
295
|
+
newline
|
296
|
+
write "then"
|
297
|
+
end
|
298
|
+
else
|
299
|
+
write possible_output
|
300
|
+
end
|
301
|
+
when :def
|
302
|
+
write "def "
|
303
|
+
write node.children[0].to_s
|
304
|
+
if node.children[1].children.length > 0
|
305
|
+
write "("
|
306
|
+
visit node.children[1]
|
307
|
+
write ")"
|
308
|
+
end
|
309
|
+
newline
|
310
|
+
|
311
|
+
indent do
|
312
|
+
visit node.children[2]
|
313
|
+
end
|
314
|
+
|
315
|
+
newline
|
316
|
+
write "end"
|
317
|
+
when :args
|
318
|
+
node.children.each_with_index do |child, index|
|
319
|
+
visit child
|
320
|
+
write ", " unless index == node.children.length - 1
|
321
|
+
end
|
322
|
+
when :arg
|
323
|
+
write node.children[0].to_s
|
324
|
+
when :lvar
|
325
|
+
write node.children[0].to_s
|
326
|
+
when :self
|
327
|
+
"self"
|
328
|
+
when :sym
|
329
|
+
write ":"
|
330
|
+
write node.children[0].to_s
|
331
|
+
when :return
|
332
|
+
write "return"
|
333
|
+
|
334
|
+
possible_output = capture do
|
335
|
+
visit node.children[0]
|
336
|
+
end
|
337
|
+
|
338
|
+
if !possible_output.start_with?("\n")
|
339
|
+
write " "
|
340
|
+
end
|
341
|
+
when :case
|
342
|
+
write "case "
|
343
|
+
visit node.children[0]
|
344
|
+
newline
|
345
|
+
node.children[1..-1].each do |child|
|
346
|
+
if child.type != :when
|
347
|
+
write "else"
|
348
|
+
newline
|
349
|
+
|
350
|
+
indent do
|
351
|
+
visit child
|
352
|
+
end
|
353
|
+
else
|
354
|
+
visit child
|
355
|
+
newline
|
356
|
+
end
|
357
|
+
end
|
358
|
+
write "end"
|
359
|
+
when :when
|
360
|
+
write "when "
|
361
|
+
visit node.children[0]
|
362
|
+
newline
|
363
|
+
indent do
|
364
|
+
visit node.children[1]
|
365
|
+
end
|
366
|
+
when :or_asgn
|
367
|
+
visit node.children[0]
|
368
|
+
write " ||= " # TODO handle long lines here too
|
369
|
+
visit node.children[1]
|
370
|
+
when :ivasgn
|
371
|
+
write node.children[0].to_s
|
372
|
+
when :ivar
|
373
|
+
write node.children[0].to_s
|
374
|
+
when :blockarg
|
375
|
+
write node.children[0].to_s
|
376
|
+
when :yield
|
377
|
+
write "yield"
|
378
|
+
when :op_asgn
|
379
|
+
visit node.children[0]
|
380
|
+
write " "
|
381
|
+
write node.children[1].to_s
|
382
|
+
write " "
|
383
|
+
visit node.children[2]
|
384
|
+
when :lvasgn
|
385
|
+
write node.children[0].to_s
|
386
|
+
write " = "
|
387
|
+
|
388
|
+
visit node.children[1]
|
389
|
+
when :irange
|
390
|
+
visit node.children[0]
|
391
|
+
write ".."
|
392
|
+
visit node.children[1]
|
393
|
+
else
|
394
|
+
raise "unhandled node type `#{node.type}`\nnode: #{node}"
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
def write_multiline_conditional(node)
|
399
|
+
visit node.children[0]
|
400
|
+
|
401
|
+
if node.type == :or
|
402
|
+
write " ||"
|
403
|
+
elsif node.type == :and
|
404
|
+
write " &&"
|
405
|
+
end
|
406
|
+
|
407
|
+
newline
|
408
|
+
|
409
|
+
visit node.children[1]
|
410
|
+
end
|
411
|
+
end
|
412
|
+
end
|
data/lib/prettyrb.rb
CHANGED
@@ -1,191 +1,11 @@
|
|
1
|
-
require "
|
1
|
+
require "parser/current"
|
2
2
|
require "prettyrb/version"
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
class Formatter
|
8
|
-
def self.for(sexp)
|
9
|
-
case sexp.sexp_type
|
10
|
-
when :if
|
11
|
-
IfFormatter
|
12
|
-
when :str
|
13
|
-
StringFormatter
|
14
|
-
when :or
|
15
|
-
OrFormatter
|
16
|
-
when :and
|
17
|
-
AndFormatter
|
18
|
-
when :lit
|
19
|
-
LitFormatter
|
20
|
-
when :call
|
21
|
-
CallFormatter
|
22
|
-
when :class
|
23
|
-
ClassFormatter
|
24
|
-
when :cdecl
|
25
|
-
CdeclFormatter
|
26
|
-
when :array
|
27
|
-
ArrayFormatter
|
28
|
-
else
|
29
|
-
raise "can't handle #{sexp}"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def initialize(code)
|
34
|
-
@code = code
|
35
|
-
end
|
36
|
-
|
37
|
-
def format
|
38
|
-
sexp_tree = RubyParser.new.parse(@code)
|
39
|
-
|
40
|
-
self.class.for(sexp_tree).new(sexp_tree, 0, nil).format
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def format_type(sexp, indentation)
|
46
|
-
case sexp.sexp_type
|
47
|
-
when :if
|
48
|
-
IfFormatter.new(sexp, indentation).format
|
49
|
-
else
|
50
|
-
raise "can't handle #{sexp}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
attr_reader :code
|
55
|
-
end
|
56
|
-
|
57
|
-
class BaseFormatter
|
58
|
-
attr_reader :sexp, :indentation, :parent
|
59
|
-
|
60
|
-
def initialize(sexp, indentation, parent)
|
61
|
-
@sexp = sexp
|
62
|
-
@indentation = indentation
|
63
|
-
@parent = parent
|
64
|
-
end
|
65
|
-
|
66
|
-
def type
|
67
|
-
sexp.sexp_type
|
68
|
-
end
|
69
|
-
|
70
|
-
def indents
|
71
|
-
' ' * indentation
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
class LitFormatter < BaseFormatter
|
76
|
-
def format
|
77
|
-
sexp[1]
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
class StringFormatter < BaseFormatter
|
82
|
-
def format
|
83
|
-
if parent.type == :array
|
84
|
-
"\"#{sexp[1]}\""
|
85
|
-
else
|
86
|
-
"#{indents}\"#{sexp[1]}\""
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
class IfFormatter < BaseFormatter
|
92
|
-
def format
|
93
|
-
start = "#{' ' * @indentation}if "
|
94
|
-
condition = Formatter.for(sexp[1]).new(@sexp[1], @indentation + 2, self).format
|
95
|
-
body = Formatter.for(sexp[2]).new(@sexp[2], @indentation + 2, self).format
|
96
|
-
ending = "#{' ' * @indentation}end"
|
97
|
-
|
98
|
-
"#{start}#{condition}\n#{body}\n#{ending}"
|
99
|
-
end
|
100
|
-
end
|
4
|
+
require "prettyrb/formatter"
|
5
|
+
require "prettyrb/visitor"
|
101
6
|
|
102
|
-
|
103
|
-
def format
|
104
|
-
left = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
105
|
-
right = Formatter.for(sexp[2]).new(sexp[2], @indentation, self).format
|
7
|
+
require "prettyrb/cli"
|
106
8
|
|
107
|
-
|
108
|
-
|
109
|
-
else
|
110
|
-
"#{left} || #{right}"
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
private
|
115
|
-
|
116
|
-
def needs_parens?
|
117
|
-
parent&.type == :or || parent&.type == :and
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
class AndFormatter < BaseFormatter
|
122
|
-
def format
|
123
|
-
left = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
124
|
-
right = Formatter.for(sexp[2]).new(sexp[2], @indentation, self).format
|
125
|
-
|
126
|
-
if needs_parens?
|
127
|
-
"(#{left} && #{right})"
|
128
|
-
else
|
129
|
-
"#{left} && #{right}"
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
private
|
134
|
-
|
135
|
-
def needs_parens?
|
136
|
-
parent&.type == :or || parent&.type == :and
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
class CallFormatter < BaseFormatter
|
141
|
-
def format
|
142
|
-
if infix?
|
143
|
-
left = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
144
|
-
right = Formatter.for(sexp[3]).new(sexp[3], @indentation, self).format
|
145
|
-
|
146
|
-
if needs_parens?
|
147
|
-
"(#{left} #{sexp[2]} #{right})"
|
148
|
-
else
|
149
|
-
"#{left} #{sexp[2]} #{right}"
|
150
|
-
end
|
151
|
-
else
|
152
|
-
content = Formatter.for(sexp[1]).new(sexp[1], @indentation, self).format
|
153
|
-
"#{content}.#{sexp[2]}"
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
private
|
158
|
-
|
159
|
-
def infix?
|
160
|
-
sexp.length == 4
|
161
|
-
end
|
162
|
-
|
163
|
-
def needs_parens?
|
164
|
-
parent&.type == :or || parent&.type == :and
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
class ClassFormatter < BaseFormatter
|
169
|
-
def format
|
170
|
-
content = Formatter.for(sexp[3]).new(sexp[3], indentation + 2, self).format
|
171
|
-
"#{indents}class #{sexp[1]}\n#{content}\n#{indents}end" # TODO handle inheritance
|
172
|
-
end
|
173
|
-
end
|
174
|
-
|
175
|
-
class CdeclFormatter < BaseFormatter
|
176
|
-
def format
|
177
|
-
content = Formatter.for(sexp[2]).new(sexp[2], indentation + 2, self).format
|
178
|
-
"#{indents}#{sexp[1]} = #{content}"
|
179
|
-
end
|
180
|
-
end
|
181
|
-
|
182
|
-
class ArrayFormatter < BaseFormatter
|
183
|
-
def format
|
184
|
-
elements = sexp[1..-1].map do |nested_sexp|
|
185
|
-
Formatter.for(nested_sexp).new(nested_sexp, indentation + 2, self).format
|
186
|
-
end
|
187
|
-
|
188
|
-
"[#{elements.join(", ")}]"
|
189
|
-
end
|
190
|
-
end
|
9
|
+
module Prettyrb
|
10
|
+
class Error < StandardError; end
|
191
11
|
end
|
data/prettyrb.gemspec
CHANGED
@@ -32,9 +32,11 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
33
|
spec.require_paths = ["lib"]
|
34
34
|
|
35
|
-
spec.add_dependency "
|
35
|
+
spec.add_dependency "parser", "~> 2.7.0.5"
|
36
|
+
spec.add_dependency "thor"
|
36
37
|
|
37
38
|
spec.add_development_dependency "bundler", "~> 1.17"
|
38
|
-
spec.add_development_dependency "rake", "~>
|
39
|
+
spec.add_development_dependency "rake", "~> 12.3.3"
|
39
40
|
spec.add_development_dependency "minitest", "~> 5.0"
|
41
|
+
spec.add_development_dependency "byebug", "~> 11.1"
|
40
42
|
end
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prettyrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.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-03-
|
11
|
+
date: 2020-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: parser
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.7.0.5
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.7.0.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -44,14 +58,14 @@ dependencies:
|
|
44
58
|
requirements:
|
45
59
|
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
61
|
+
version: 12.3.3
|
48
62
|
type: :development
|
49
63
|
prerelease: false
|
50
64
|
version_requirements: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
68
|
+
version: 12.3.3
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: minitest
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,13 +80,29 @@ dependencies:
|
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: byebug
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '11.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '11.1'
|
69
97
|
description: Ruby source code formatter
|
70
98
|
email:
|
71
99
|
- blake@blakewilliams.me
|
72
|
-
executables:
|
100
|
+
executables:
|
101
|
+
- prettyrb
|
73
102
|
extensions: []
|
74
103
|
extra_rdoc_files: []
|
75
104
|
files:
|
105
|
+
- ".github/workflows/ruby.yml"
|
76
106
|
- ".gitignore"
|
77
107
|
- ".travis.yml"
|
78
108
|
- Gemfile
|
@@ -82,8 +112,12 @@ files:
|
|
82
112
|
- Rakefile
|
83
113
|
- bin/console
|
84
114
|
- bin/setup
|
115
|
+
- exe/prettyrb
|
85
116
|
- lib/prettyrb.rb
|
117
|
+
- lib/prettyrb/cli.rb
|
118
|
+
- lib/prettyrb/formatter.rb
|
86
119
|
- lib/prettyrb/version.rb
|
120
|
+
- lib/prettyrb/visitor.rb
|
87
121
|
- prettyrb.gemspec
|
88
122
|
homepage: https://github.com/blakewilliams/prettyrb
|
89
123
|
licenses:
|