rufo 0.0.17 → 0.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/rufo/command.rb +1 -1
- data/lib/rufo/formatter.rb +146 -79
- data/lib/rufo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82f3fd868e4d701c1e505e16a057d8d8e494a925
|
4
|
+
data.tar.gz: de7c89b69cac54b2640d899310623539d9ec7e25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28620000e4f9723f908da738c364c35390ae9308aa79590b3485eed9bc30aa0bba7e709927b2357dcd691bfc14552a209d478d38cce03677d0788b23aafdd18e
|
7
|
+
data.tar.gz: cece98ad578f1a43dadb98791773c062208b40953394b484315b15268293ae50a0766898c644aeb9dfee36bae2a3dfc9b53ad672ea46cc32bf6f1992ef44a3dd
|
data/README.md
CHANGED
@@ -76,8 +76,8 @@ issue if you need something else to be configurable.
|
|
76
76
|
|
77
77
|
## Status
|
78
78
|
|
79
|
-
The formatter
|
80
|
-
|
79
|
+
The formatter is able to format `rails` and other projects, so at this point
|
80
|
+
it's pretty mature. There might still be some bugs. Don't hesitate
|
81
81
|
to open an issue if you find something is not working well. In any case, if the formatter
|
82
82
|
chokes on some valid input you will get an error prompting you to submit a bug report here :-)
|
83
83
|
|
data/lib/rufo/command.rb
CHANGED
data/lib/rufo/formatter.rb
CHANGED
@@ -53,10 +53,10 @@ class Rufo::Formatter
|
|
53
53
|
@hash_keys_positions = []
|
54
54
|
|
55
55
|
# Settings
|
56
|
-
@indent_size
|
57
|
-
@align_comments
|
58
|
-
@align_assignments
|
59
|
-
@align_hash_keys
|
56
|
+
@indent_size = options.fetch(:indent_size, 2)
|
57
|
+
@align_comments = options.fetch(:align_comments, true)
|
58
|
+
@align_assignments = options.fetch(:align_assignments, true)
|
59
|
+
@align_hash_keys = options.fetch(:align_hash_keys, true)
|
60
60
|
end
|
61
61
|
|
62
62
|
# The indent size (default: 2)
|
@@ -394,18 +394,17 @@ class Rufo::Formatter
|
|
394
394
|
end
|
395
395
|
|
396
396
|
skip_space
|
397
|
-
if newline? || comment?
|
398
|
-
check_pending_heredocs
|
399
|
-
end
|
400
397
|
|
401
398
|
line_before_endline = @line
|
402
399
|
|
403
400
|
is_last = last?(i, exps)
|
404
401
|
if with_lines
|
405
|
-
|
402
|
+
exp_needs_two_lines = needs_two_lines?(exp)
|
403
|
+
|
404
|
+
consume_end_of_line(false, !is_last, !is_last, exp_needs_two_lines)
|
406
405
|
|
407
406
|
# Make sure to put two lines before defs, class and others
|
408
|
-
if !is_last && (
|
407
|
+
if !is_last && (exp_needs_two_lines || needs_two_lines?(exps[i + 1])) && @line <= line_before_endline + 1
|
409
408
|
write_line
|
410
409
|
end
|
411
410
|
else
|
@@ -414,13 +413,23 @@ class Rufo::Formatter
|
|
414
413
|
end
|
415
414
|
end
|
416
415
|
|
417
|
-
def needs_two_lines?(
|
418
|
-
|
416
|
+
def needs_two_lines?(exp)
|
417
|
+
kind = exp[0]
|
418
|
+
case kind
|
419
419
|
when :def, :class, :module
|
420
|
-
true
|
421
|
-
|
422
|
-
|
420
|
+
return true
|
421
|
+
when :vcall
|
422
|
+
# Check if it's private/protected/public
|
423
|
+
nested = exp[1]
|
424
|
+
if nested[0] == :@ident
|
425
|
+
case nested[1]
|
426
|
+
when "private", "protected", "public"
|
427
|
+
return true
|
428
|
+
end
|
429
|
+
end
|
423
430
|
end
|
431
|
+
|
432
|
+
false
|
424
433
|
end
|
425
434
|
|
426
435
|
def visit_string_literal(node)
|
@@ -430,30 +439,18 @@ class Rufo::Formatter
|
|
430
439
|
|
431
440
|
if heredoc
|
432
441
|
write current_token_value.rstrip
|
442
|
+
# Accumulate heredoc: we'll write it once
|
443
|
+
# we find a newline.
|
444
|
+
@heredocs << [node, tilde]
|
433
445
|
next_token
|
434
|
-
|
435
|
-
|
436
|
-
# If something other than a newline follows the heredoc
|
437
|
-
# beginning it means some other code follows and
|
438
|
-
# we have to accumulate the heredoc and print it
|
439
|
-
# later, when the line ends.
|
440
|
-
if !newline? || !@heredocs.empty?
|
441
|
-
@heredocs << [node, tilde]
|
442
|
-
return
|
443
|
-
end
|
446
|
+
return
|
444
447
|
elsif current_token_kind == :on_backtick
|
445
448
|
consume_token :on_backtick
|
446
449
|
else
|
447
450
|
consume_token :on_tstring_beg
|
448
451
|
end
|
449
452
|
|
450
|
-
if heredoc
|
451
|
-
@current_heredoc = [node, tilde]
|
452
|
-
end
|
453
|
-
|
454
453
|
visit_string_literal_end(node)
|
455
|
-
|
456
|
-
@current_heredoc = nil if heredoc
|
457
454
|
end
|
458
455
|
|
459
456
|
def visit_string_literal_end(node)
|
@@ -473,10 +470,8 @@ class Rufo::Formatter
|
|
473
470
|
next_token
|
474
471
|
skip_space
|
475
472
|
|
476
|
-
|
477
|
-
|
478
|
-
write_indent
|
479
|
-
end
|
473
|
+
# Simulate a newline after the heredoc
|
474
|
+
@tokens << [[0, 0], :on_ignored_nl, "\n"]
|
480
475
|
when :on_backtick
|
481
476
|
consume_token :on_backtick
|
482
477
|
else
|
@@ -869,15 +864,12 @@ class Rufo::Formatter
|
|
869
864
|
end
|
870
865
|
end
|
871
866
|
|
872
|
-
def
|
867
|
+
def flush_heredocs
|
873
868
|
printed = false
|
874
869
|
|
875
870
|
until @heredocs.empty?
|
876
871
|
heredoc, tilde = @heredocs.first
|
877
872
|
|
878
|
-
# Need to print a line between consecutive heredoc ends
|
879
|
-
write_line if printed
|
880
|
-
|
881
873
|
@heredocs.shift
|
882
874
|
@current_heredoc = [heredoc, tilde]
|
883
875
|
visit_string_literal_end(heredoc)
|
@@ -921,7 +913,26 @@ class Rufo::Formatter
|
|
921
913
|
end
|
922
914
|
|
923
915
|
def visit_command_args(args)
|
924
|
-
|
916
|
+
needed_indent = @column
|
917
|
+
|
918
|
+
# Check if there's a single argument and it's
|
919
|
+
# a def, class or module. In that case we don't
|
920
|
+
# want to align the content to the position of
|
921
|
+
# that keyword.
|
922
|
+
if args[0] == :args_add_block
|
923
|
+
nested_args = args[1]
|
924
|
+
if nested_args.is_a?(Array) && nested_args.size == 1
|
925
|
+
first = nested_args[0]
|
926
|
+
if first.is_a?(Array)
|
927
|
+
case first[0]
|
928
|
+
when :def, :class, :module
|
929
|
+
needed_indent = @indent
|
930
|
+
end
|
931
|
+
end
|
932
|
+
end
|
933
|
+
end
|
934
|
+
|
935
|
+
indent(needed_indent) do
|
925
936
|
if args[0].is_a?(Symbol)
|
926
937
|
visit args
|
927
938
|
else
|
@@ -1003,29 +1014,56 @@ class Rufo::Formatter
|
|
1003
1014
|
end
|
1004
1015
|
|
1005
1016
|
def visit_block_arguments(node)
|
1006
|
-
# [:block_var, params,
|
1007
|
-
_, params,
|
1017
|
+
# [:block_var, params, local_params]
|
1018
|
+
_, params, local_params = node
|
1019
|
+
|
1020
|
+
empty_params = empty_params?(params)
|
1008
1021
|
|
1009
1022
|
check :on_op
|
1010
1023
|
|
1011
1024
|
# check for ||
|
1012
|
-
if
|
1025
|
+
if empty_params && !local_params
|
1013
1026
|
# Don't write || as it's meaningless
|
1014
|
-
|
1015
|
-
else
|
1016
|
-
next_token
|
1017
|
-
skip_space_or_newline
|
1018
|
-
|
1019
|
-
# This means it's an empty | |, so we remove it
|
1020
|
-
if current_token_kind == :on_op && current_token_value == "|"
|
1027
|
+
if current_token_value == "|"
|
1021
1028
|
next_token
|
1022
|
-
else
|
1023
|
-
write "|"
|
1024
|
-
visit params
|
1025
1029
|
skip_space_or_newline
|
1026
|
-
|
1030
|
+
check :on_op
|
1031
|
+
next_token
|
1032
|
+
else
|
1033
|
+
next_token
|
1027
1034
|
end
|
1035
|
+
return
|
1036
|
+
end
|
1037
|
+
|
1038
|
+
consume_token :on_op
|
1039
|
+
found_semicolon = skip_space_or_newline(true, true)
|
1040
|
+
|
1041
|
+
if found_semicolon
|
1042
|
+
# Nothing
|
1043
|
+
elsif empty_params && local_params
|
1044
|
+
consume_token :on_semicolon
|
1045
|
+
found_semicolon = true
|
1046
|
+
end
|
1047
|
+
|
1048
|
+
skip_space_or_newline
|
1049
|
+
|
1050
|
+
unless empty_params
|
1051
|
+
visit params
|
1052
|
+
skip_space
|
1028
1053
|
end
|
1054
|
+
|
1055
|
+
if local_params
|
1056
|
+
if semicolon?
|
1057
|
+
consume_token :on_semicolon
|
1058
|
+
consume_space
|
1059
|
+
end
|
1060
|
+
|
1061
|
+
visit_comma_separated_list local_params
|
1062
|
+
else
|
1063
|
+
skip_space_or_newline
|
1064
|
+
end
|
1065
|
+
|
1066
|
+
consume_op "|"
|
1029
1067
|
end
|
1030
1068
|
|
1031
1069
|
def visit_call_args(node)
|
@@ -1036,7 +1074,7 @@ class Rufo::Formatter
|
|
1036
1074
|
# arg1, ..., *star
|
1037
1075
|
visit args
|
1038
1076
|
else
|
1039
|
-
visit_comma_separated_list args
|
1077
|
+
visit_comma_separated_list args
|
1040
1078
|
end
|
1041
1079
|
|
1042
1080
|
if block_arg
|
@@ -1258,7 +1296,7 @@ class Rufo::Formatter
|
|
1258
1296
|
end
|
1259
1297
|
end
|
1260
1298
|
|
1261
|
-
def visit_comma_separated_list(nodes
|
1299
|
+
def visit_comma_separated_list(nodes)
|
1262
1300
|
# When there's *x inside a left hand side assignment
|
1263
1301
|
# or a case when, it comes as [:op, ...]
|
1264
1302
|
if nodes[0].is_a?(Symbol)
|
@@ -1268,15 +1306,13 @@ class Rufo::Formatter
|
|
1268
1306
|
|
1269
1307
|
needs_indent = false
|
1270
1308
|
|
1271
|
-
if
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
base_column = @column
|
1279
|
-
end
|
1309
|
+
if newline? || comment?
|
1310
|
+
needs_indent = true
|
1311
|
+
base_column = next_indent
|
1312
|
+
consume_end_of_line
|
1313
|
+
write_indent(base_column)
|
1314
|
+
else
|
1315
|
+
base_column = @column
|
1280
1316
|
end
|
1281
1317
|
|
1282
1318
|
nodes.each_with_index do |exp, i|
|
@@ -1760,7 +1796,7 @@ class Rufo::Formatter
|
|
1760
1796
|
if elements
|
1761
1797
|
# [:assoclist_from_args, elements]
|
1762
1798
|
push_hash(node) do
|
1763
|
-
visit_literal_elements(elements[1])
|
1799
|
+
visit_literal_elements(elements[1], true)
|
1764
1800
|
end
|
1765
1801
|
else
|
1766
1802
|
skip_space_or_newline
|
@@ -2115,11 +2151,18 @@ class Rufo::Formatter
|
|
2115
2151
|
visit_comma_separated_list exps
|
2116
2152
|
end
|
2117
2153
|
|
2118
|
-
def visit_literal_elements(elements)
|
2119
|
-
base_column
|
2120
|
-
|
2154
|
+
def visit_literal_elements(elements, inside_hash = false)
|
2155
|
+
base_column = @column
|
2156
|
+
needs_final_space = inside_hash && space?
|
2121
2157
|
skip_space
|
2122
2158
|
|
2159
|
+
if newline? || comment?
|
2160
|
+
needs_final_space = false
|
2161
|
+
elsif needs_final_space
|
2162
|
+
consume_space
|
2163
|
+
base_column = @column
|
2164
|
+
end
|
2165
|
+
|
2123
2166
|
# If there's a newline right at the beginning,
|
2124
2167
|
# write it, and we'll indent element and always
|
2125
2168
|
# add a trailing comma to the last element
|
@@ -2167,7 +2210,11 @@ class Rufo::Formatter
|
|
2167
2210
|
elsif comment?
|
2168
2211
|
consume_end_of_line
|
2169
2212
|
else
|
2170
|
-
|
2213
|
+
if needs_final_space
|
2214
|
+
consume_space
|
2215
|
+
else
|
2216
|
+
skip_space_or_newline
|
2217
|
+
end
|
2171
2218
|
end
|
2172
2219
|
end
|
2173
2220
|
|
@@ -2388,10 +2435,11 @@ class Rufo::Formatter
|
|
2388
2435
|
has_slash_newline
|
2389
2436
|
end
|
2390
2437
|
|
2391
|
-
def skip_space_or_newline(want_semicolon = false)
|
2392
|
-
found_newline
|
2393
|
-
found_comment
|
2394
|
-
|
2438
|
+
def skip_space_or_newline(want_semicolon = false, write_first_semicolon = false)
|
2439
|
+
found_newline = false
|
2440
|
+
found_comment = false
|
2441
|
+
found_semicolon = false
|
2442
|
+
last = nil
|
2395
2443
|
|
2396
2444
|
while true
|
2397
2445
|
case current_token_kind
|
@@ -2402,11 +2450,12 @@ class Rufo::Formatter
|
|
2402
2450
|
last = :newline
|
2403
2451
|
found_newline = true
|
2404
2452
|
when :on_semicolon
|
2405
|
-
if !found_newline && !found_comment
|
2453
|
+
if (!found_newline && !found_comment) || (!found_semicolon && write_first_semicolon)
|
2406
2454
|
write "; "
|
2407
2455
|
end
|
2408
2456
|
next_token
|
2409
|
-
last
|
2457
|
+
last = :semicolon
|
2458
|
+
found_semicolon = true
|
2410
2459
|
when :on_comment
|
2411
2460
|
write_line if last == :newline
|
2412
2461
|
|
@@ -2424,6 +2473,8 @@ class Rufo::Formatter
|
|
2424
2473
|
break
|
2425
2474
|
end
|
2426
2475
|
end
|
2476
|
+
|
2477
|
+
found_semicolon
|
2427
2478
|
end
|
2428
2479
|
|
2429
2480
|
def skip_semicolons
|
@@ -2480,11 +2531,12 @@ class Rufo::Formatter
|
|
2480
2531
|
# - at_prefix: are we at a point before an expression? (if so, we don't need a space before the first comment)
|
2481
2532
|
# - want_semicolon: do we want do print a semicolon to separate expressions?
|
2482
2533
|
# - want_multiline: do we want multiple lines to appear, or at most one?
|
2483
|
-
def consume_end_of_line(at_prefix = false, want_semicolon = false, want_multiline = true)
|
2534
|
+
def consume_end_of_line(at_prefix = false, want_semicolon = false, want_multiline = true, needs_two_lines_on_comment = false)
|
2484
2535
|
found_newline = false # Did we find any newline during this method?
|
2485
2536
|
last = nil # Last token kind found
|
2486
2537
|
multilple_lines = false # Did we pass through more than one newline?
|
2487
2538
|
last_comment_has_newline = false # Does the last comment has a newline?
|
2539
|
+
newline_count = 0 # Number of newlines we passed
|
2488
2540
|
|
2489
2541
|
while true
|
2490
2542
|
case current_token_kind
|
@@ -2516,7 +2568,8 @@ class Rufo::Formatter
|
|
2516
2568
|
end
|
2517
2569
|
found_newline = true
|
2518
2570
|
next_token
|
2519
|
-
last
|
2571
|
+
last = :newline
|
2572
|
+
newline_count += 1
|
2520
2573
|
when :on_semicolon
|
2521
2574
|
next_token
|
2522
2575
|
# If we want to print semicolons and we didn't find a newline yet,
|
@@ -2539,6 +2592,16 @@ class Rufo::Formatter
|
|
2539
2592
|
write_indent
|
2540
2593
|
else
|
2541
2594
|
if found_newline
|
2595
|
+
if newline_count == 1 && needs_two_lines_on_comment
|
2596
|
+
if multilple_lines
|
2597
|
+
write_line
|
2598
|
+
multilple_lines = false
|
2599
|
+
else
|
2600
|
+
multilple_lines = true
|
2601
|
+
end
|
2602
|
+
needs_two_lines_on_comment = false
|
2603
|
+
end
|
2604
|
+
|
2542
2605
|
# Write line or second line if needed
|
2543
2606
|
write_line if last != :newline || multilple_lines
|
2544
2607
|
write_indent
|
@@ -2750,9 +2813,9 @@ class Rufo::Formatter
|
|
2750
2813
|
|
2751
2814
|
def find_closing_brace_token
|
2752
2815
|
count = 0
|
2753
|
-
i
|
2816
|
+
i = @tokens.size - 1
|
2754
2817
|
while i >= 0
|
2755
|
-
token
|
2818
|
+
token = @tokens[i]
|
2756
2819
|
(line, column), kind = token
|
2757
2820
|
case kind
|
2758
2821
|
when :on_lbrace, :on_tlambeg
|
@@ -2785,6 +2848,10 @@ class Rufo::Formatter
|
|
2785
2848
|
|
2786
2849
|
def next_token
|
2787
2850
|
@tokens.pop
|
2851
|
+
|
2852
|
+
if newline? && !@heredocs.empty?
|
2853
|
+
flush_heredocs
|
2854
|
+
end
|
2788
2855
|
end
|
2789
2856
|
|
2790
2857
|
def last?(i, array)
|
data/lib/rufo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ary Borenszweig
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|