rufo 0.0.28 → 0.0.29
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rufo/formatter.rb +136 -95
- data/lib/rufo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5d988295293486aedb59809e856de442d3e7643
|
4
|
+
data.tar.gz: 8429b9a9e9c0261736946f31564aed9f85618c91
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fe7de7ca53140f7b02fda1d5bdc9ad8cf448f448f60c969479f9e8427b96585a7dc7094f816fe3450f21f591bdea88f2c162670a906f0a13eda2b38f9388327
|
7
|
+
data.tar.gz: 1dc130a6f8708862eb9a4ded52459607579a1f431e77758f2e1d2266a50d6563e8e3fe52b0b8110c4e8508014107b7624ab49cb24a1fa8861d937abc03ff461f
|
data/lib/rufo/formatter.rb
CHANGED
@@ -532,8 +532,18 @@ class Rufo::Formatter
|
|
532
532
|
if !is_last && (exp_needs_two_lines || needs_two_lines?(exps[i + 1])) && @line <= line_before_endline + 1
|
533
533
|
write_line
|
534
534
|
end
|
535
|
-
|
536
|
-
|
535
|
+
elsif !is_last
|
536
|
+
skip_space
|
537
|
+
|
538
|
+
has_semicolon = semicolon?
|
539
|
+
skip_semicolons
|
540
|
+
if newline?
|
541
|
+
write_line
|
542
|
+
write_indent(next_indent)
|
543
|
+
elsif has_semicolon
|
544
|
+
write "; "
|
545
|
+
end
|
546
|
+
skip_space_or_newline
|
537
547
|
end
|
538
548
|
end
|
539
549
|
end
|
@@ -1170,7 +1180,11 @@ class Rufo::Formatter
|
|
1170
1180
|
if void_exps?(body)
|
1171
1181
|
consume_token :on_lbrace
|
1172
1182
|
consume_block_args args
|
1173
|
-
|
1183
|
+
if space?
|
1184
|
+
consume_space
|
1185
|
+
else
|
1186
|
+
skip_space_or_newline
|
1187
|
+
end
|
1174
1188
|
consume_token :on_rbrace
|
1175
1189
|
return
|
1176
1190
|
end
|
@@ -1199,7 +1213,7 @@ class Rufo::Formatter
|
|
1199
1213
|
call_info << true
|
1200
1214
|
end
|
1201
1215
|
|
1202
|
-
indent_body body
|
1216
|
+
indent_body body, force_multiline: true
|
1203
1217
|
write_indent
|
1204
1218
|
|
1205
1219
|
call_info << @line if call_info
|
@@ -1211,13 +1225,15 @@ class Rufo::Formatter
|
|
1211
1225
|
# [:brace_block, args, body]
|
1212
1226
|
_, args, body = node
|
1213
1227
|
|
1228
|
+
line = @line
|
1229
|
+
|
1214
1230
|
consume_keyword "do"
|
1215
1231
|
|
1216
1232
|
consume_block_args args
|
1217
1233
|
|
1218
1234
|
indent_body body
|
1219
1235
|
|
1220
|
-
write_indent
|
1236
|
+
write_indent if @line != line
|
1221
1237
|
consume_keyword "end"
|
1222
1238
|
end
|
1223
1239
|
|
@@ -1343,6 +1359,9 @@ class Rufo::Formatter
|
|
1343
1359
|
def visit_bodystmt(node)
|
1344
1360
|
# [:bodystmt, body, rescue_body, else_body, ensure_body]
|
1345
1361
|
_, body, rescue_body, else_body, ensure_body = node
|
1362
|
+
|
1363
|
+
line = @line
|
1364
|
+
|
1346
1365
|
indent_body body
|
1347
1366
|
|
1348
1367
|
while rescue_body
|
@@ -1385,7 +1404,7 @@ class Rufo::Formatter
|
|
1385
1404
|
indent_body ensure_body[1]
|
1386
1405
|
end
|
1387
1406
|
|
1388
|
-
write_indent
|
1407
|
+
write_indent if @line != line
|
1389
1408
|
consume_keyword "end"
|
1390
1409
|
end
|
1391
1410
|
|
@@ -1463,6 +1482,8 @@ class Rufo::Formatter
|
|
1463
1482
|
#[:for, var, collection, body]
|
1464
1483
|
_, var, collection, body = node
|
1465
1484
|
|
1485
|
+
line = @line
|
1486
|
+
|
1466
1487
|
consume_keyword "for"
|
1467
1488
|
consume_space
|
1468
1489
|
|
@@ -1478,10 +1499,9 @@ class Rufo::Formatter
|
|
1478
1499
|
visit collection
|
1479
1500
|
skip_space
|
1480
1501
|
|
1481
|
-
next_token if keyword?("do")
|
1482
|
-
|
1483
1502
|
indent_body body
|
1484
|
-
|
1503
|
+
|
1504
|
+
write_indent if @line != line
|
1485
1505
|
consume_keyword "end"
|
1486
1506
|
end
|
1487
1507
|
|
@@ -1664,7 +1684,7 @@ class Rufo::Formatter
|
|
1664
1684
|
visit superclass
|
1665
1685
|
end
|
1666
1686
|
|
1667
|
-
|
1687
|
+
visit body
|
1668
1688
|
end
|
1669
1689
|
|
1670
1690
|
def visit_module(node)
|
@@ -1677,25 +1697,7 @@ class Rufo::Formatter
|
|
1677
1697
|
skip_space_or_newline
|
1678
1698
|
write_space
|
1679
1699
|
visit name
|
1680
|
-
|
1681
|
-
end
|
1682
|
-
|
1683
|
-
def maybe_inline_body(body)
|
1684
|
-
skip_space
|
1685
|
-
if semicolon? && empty_body?(body)
|
1686
|
-
next_token
|
1687
|
-
skip_space
|
1688
|
-
if newline?
|
1689
|
-
skip_space_or_newline
|
1690
|
-
visit body
|
1691
|
-
else
|
1692
|
-
write "; "
|
1693
|
-
skip_space_or_newline
|
1694
|
-
consume_keyword "end"
|
1695
|
-
end
|
1696
|
-
else
|
1697
|
-
visit body
|
1698
|
-
end
|
1700
|
+
visit body
|
1699
1701
|
end
|
1700
1702
|
|
1701
1703
|
def visit_def(node)
|
@@ -1754,7 +1756,7 @@ class Rufo::Formatter
|
|
1754
1756
|
skip_space_or_newline
|
1755
1757
|
check :on_rparen
|
1756
1758
|
next_token
|
1757
|
-
|
1759
|
+
skip_space
|
1758
1760
|
else
|
1759
1761
|
write "("
|
1760
1762
|
|
@@ -1780,7 +1782,7 @@ class Rufo::Formatter
|
|
1780
1782
|
write "("
|
1781
1783
|
visit params
|
1782
1784
|
write ")"
|
1783
|
-
|
1785
|
+
skip_space
|
1784
1786
|
end
|
1785
1787
|
|
1786
1788
|
visit body
|
@@ -2058,16 +2060,17 @@ class Rufo::Formatter
|
|
2058
2060
|
# `:foo => 1` or `:"foo" => 1` and a `=>`
|
2059
2061
|
# always follows
|
2060
2062
|
symbol = current_token_kind == :on_symbeg
|
2063
|
+
arrow = symbol || !(key[0] == :@label || key[0] == :dyna_symbol)
|
2061
2064
|
|
2062
2065
|
visit key
|
2063
2066
|
|
2064
|
-
consume_space(want_preserve_whitespace:
|
2067
|
+
consume_space(want_preserve_whitespace: @preserve_whitespace)
|
2065
2068
|
|
2066
2069
|
track_hash_key
|
2067
2070
|
|
2068
2071
|
# Don't output `=>` for keys that are `label: value`
|
2069
2072
|
# or `"label": value`
|
2070
|
-
if
|
2073
|
+
if arrow
|
2071
2074
|
consume_op "=>"
|
2072
2075
|
consume_space(want_preserve_whitespace: !@align_hash_keys)
|
2073
2076
|
end
|
@@ -2310,7 +2313,7 @@ class Rufo::Formatter
|
|
2310
2313
|
consume_keyword "do"
|
2311
2314
|
end
|
2312
2315
|
|
2313
|
-
indent_body body
|
2316
|
+
indent_body body, force_multiline: true
|
2314
2317
|
|
2315
2318
|
write_indent
|
2316
2319
|
|
@@ -2465,6 +2468,9 @@ class Rufo::Formatter
|
|
2465
2468
|
# We have to be careful not to aumatically write a heredoc on next_token,
|
2466
2469
|
# because we miss the chance to write a comma to separate elements
|
2467
2470
|
next_token_no_heredoc_check
|
2471
|
+
|
2472
|
+
first_space = current_token if space?
|
2473
|
+
|
2468
2474
|
skip_space_no_heredoc_check
|
2469
2475
|
wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
|
2470
2476
|
|
@@ -2475,6 +2481,8 @@ class Rufo::Formatter
|
|
2475
2481
|
consume_end_of_line
|
2476
2482
|
write_indent(needed_indent)
|
2477
2483
|
end
|
2484
|
+
elsif !is_last && first_space && @preserve_whitespace
|
2485
|
+
write_space first_space[2]
|
2478
2486
|
else
|
2479
2487
|
write_space unless is_last
|
2480
2488
|
end
|
@@ -2533,17 +2541,13 @@ class Rufo::Formatter
|
|
2533
2541
|
# end
|
2534
2542
|
#
|
2535
2543
|
# [:if, cond, then, else]
|
2544
|
+
line = @line
|
2545
|
+
|
2536
2546
|
consume_keyword(keyword)
|
2537
2547
|
consume_space
|
2538
2548
|
visit node[1]
|
2539
2549
|
skip_space
|
2540
2550
|
|
2541
|
-
# Remove "then"
|
2542
|
-
if keyword?("then")
|
2543
|
-
next_token
|
2544
|
-
skip_space
|
2545
|
-
end
|
2546
|
-
|
2547
2551
|
indent_body node[2]
|
2548
2552
|
if else_body = node[3]
|
2549
2553
|
# [:else, else_contents]
|
@@ -2562,7 +2566,7 @@ class Rufo::Formatter
|
|
2562
2566
|
end
|
2563
2567
|
|
2564
2568
|
if check_end
|
2565
|
-
write_indent
|
2569
|
+
write_indent if @line != line
|
2566
2570
|
consume_keyword "end"
|
2567
2571
|
end
|
2568
2572
|
end
|
@@ -2580,6 +2584,8 @@ class Rufo::Formatter
|
|
2580
2584
|
def visit_while_or_until(node, keyword)
|
2581
2585
|
_, cond, body = node
|
2582
2586
|
|
2587
|
+
line = @line
|
2588
|
+
|
2583
2589
|
consume_keyword keyword
|
2584
2590
|
consume_space
|
2585
2591
|
|
@@ -2587,49 +2593,9 @@ class Rufo::Formatter
|
|
2587
2593
|
|
2588
2594
|
skip_space
|
2589
2595
|
|
2590
|
-
|
2591
|
-
semicolon = semicolon?
|
2592
|
-
is_do = keyword?("do")
|
2593
|
-
|
2594
|
-
if (semicolon || is_do) && void_exps?(body)
|
2595
|
-
next_token
|
2596
|
-
skip_space
|
2597
|
-
|
2598
|
-
if keyword?("end")
|
2599
|
-
if is_do
|
2600
|
-
write " do end"
|
2601
|
-
else
|
2602
|
-
write "; end"
|
2603
|
-
end
|
2604
|
-
next_token
|
2605
|
-
return
|
2606
|
-
end
|
2607
|
-
end
|
2608
|
-
|
2609
|
-
if semicolon || is_do
|
2610
|
-
next_token
|
2611
|
-
skip_space
|
2612
|
-
skip_semicolons
|
2613
|
-
|
2614
|
-
if newline? || comment?
|
2615
|
-
indent_body body
|
2616
|
-
write_indent
|
2617
|
-
else
|
2618
|
-
skip_space_or_newline
|
2619
|
-
if semicolon
|
2620
|
-
write "; "
|
2621
|
-
else
|
2622
|
-
write " do "
|
2623
|
-
end
|
2624
|
-
visit_exps body, with_lines: false
|
2625
|
-
skip_space_or_newline
|
2626
|
-
write_space if is_do
|
2627
|
-
end
|
2628
|
-
else
|
2629
|
-
indent_body body
|
2630
|
-
write_indent
|
2631
|
-
end
|
2596
|
+
indent_body body
|
2632
2597
|
|
2598
|
+
write_indent if @line != line
|
2633
2599
|
consume_keyword "end"
|
2634
2600
|
end
|
2635
2601
|
|
@@ -2857,6 +2823,8 @@ class Rufo::Formatter
|
|
2857
2823
|
multilple_lines = false # Did we pass through more than one newline?
|
2858
2824
|
last_comment_has_newline = false # Does the last comment has a newline?
|
2859
2825
|
newline_count = 0 # Number of newlines we passed
|
2826
|
+
last_comment = nil # Token for the last comment found
|
2827
|
+
last_comment_column = nil # Actual column of the last comment written
|
2860
2828
|
|
2861
2829
|
loop do
|
2862
2830
|
case current_token_kind
|
@@ -2909,7 +2877,30 @@ class Rufo::Formatter
|
|
2909
2877
|
# Since we remove newlines from comments, we must add the last
|
2910
2878
|
# one if it was a comment
|
2911
2879
|
write_line
|
2912
|
-
|
2880
|
+
|
2881
|
+
# If the last comment is in the previous line and it was already
|
2882
|
+
# aligned to this comment, keep it aligned. This is useful for
|
2883
|
+
# this:
|
2884
|
+
#
|
2885
|
+
# ```
|
2886
|
+
# a = 1 # some comment
|
2887
|
+
# # that continues here
|
2888
|
+
# ```
|
2889
|
+
#
|
2890
|
+
# We want to preserve it like that and not change it to:
|
2891
|
+
#
|
2892
|
+
# ```
|
2893
|
+
# a = 1 # some comment
|
2894
|
+
# # that continues here
|
2895
|
+
# ```
|
2896
|
+
if last_comment &&
|
2897
|
+
last_comment[0][0] + 1 == current_token[0][0] &&
|
2898
|
+
last_comment[0][1] == current_token[0][1]
|
2899
|
+
write_indent(last_comment_column)
|
2900
|
+
track_comment
|
2901
|
+
else
|
2902
|
+
write_indent
|
2903
|
+
end
|
2913
2904
|
else
|
2914
2905
|
if found_newline
|
2915
2906
|
if newline_count == 1 && needs_two_lines_on_comment
|
@@ -2932,11 +2923,14 @@ class Rufo::Formatter
|
|
2932
2923
|
track_comment
|
2933
2924
|
end
|
2934
2925
|
end
|
2926
|
+
last_comment = current_token
|
2927
|
+
last_comment_column = @column
|
2935
2928
|
last_comment_has_newline = current_token_value.end_with?("\n")
|
2936
|
-
write current_token_value.rstrip
|
2937
|
-
next_token
|
2938
2929
|
last = :comment
|
2939
2930
|
multilple_lines = false
|
2931
|
+
|
2932
|
+
write current_token_value.rstrip
|
2933
|
+
next_token
|
2940
2934
|
when :on_embdoc_beg
|
2941
2935
|
write_line if multilple_lines
|
2942
2936
|
|
@@ -3000,14 +2994,61 @@ class Rufo::Formatter
|
|
3000
2994
|
end
|
3001
2995
|
end
|
3002
2996
|
|
3003
|
-
def indent_body(exps)
|
3004
|
-
|
3005
|
-
|
2997
|
+
def indent_body(exps, force_multiline: false)
|
2998
|
+
skip_space
|
2999
|
+
|
3000
|
+
has_semicolon = semicolon?
|
3001
|
+
|
3002
|
+
if has_semicolon
|
3003
|
+
next_token
|
3004
|
+
skip_semicolons
|
3005
|
+
end
|
3006
|
+
|
3007
|
+
# If an end follows there's nothing to do
|
3008
|
+
if keyword?("end")
|
3009
|
+
if has_semicolon
|
3010
|
+
write "; "
|
3011
|
+
else
|
3012
|
+
write " "
|
3013
|
+
end
|
3014
|
+
return
|
3006
3015
|
end
|
3007
3016
|
|
3008
3017
|
# A then keyword can appear after a newline after an `if`, `unless`, etc.
|
3009
3018
|
# Since that's a super weird formatting for if, probably way too obsolete
|
3010
3019
|
# by now, we just remove it.
|
3020
|
+
has_then = keyword?("then")
|
3021
|
+
if has_then
|
3022
|
+
next_token
|
3023
|
+
skip_space
|
3024
|
+
end
|
3025
|
+
|
3026
|
+
has_do = keyword?("do")
|
3027
|
+
if has_do
|
3028
|
+
next_token
|
3029
|
+
skip_space
|
3030
|
+
end
|
3031
|
+
|
3032
|
+
# If no newline or comment follows, we format it inline.
|
3033
|
+
if !force_multiline && !(newline? || comment?)
|
3034
|
+
if has_then
|
3035
|
+
write " then "
|
3036
|
+
elsif has_do
|
3037
|
+
write " do "
|
3038
|
+
elsif has_semicolon
|
3039
|
+
write "; "
|
3040
|
+
else
|
3041
|
+
write " "
|
3042
|
+
end
|
3043
|
+
visit_exps exps, with_indent: false, with_lines: false
|
3044
|
+
consume_space
|
3045
|
+
return
|
3046
|
+
end
|
3047
|
+
|
3048
|
+
indent do
|
3049
|
+
consume_end_of_line(want_multiline: false)
|
3050
|
+
end
|
3051
|
+
|
3011
3052
|
if keyword?("then")
|
3012
3053
|
next_token
|
3013
3054
|
skip_space_or_newline
|
@@ -3273,27 +3314,27 @@ class Rufo::Formatter
|
|
3273
3314
|
def do_align(elements, scope)
|
3274
3315
|
lines = @output.lines
|
3275
3316
|
|
3276
|
-
# Chunk
|
3317
|
+
# Chunk elements that are in consecutive lines
|
3277
3318
|
chunks = chunk_while(elements) do |(l1, c1, i1, id1), (l2, c2, i2, id2)|
|
3278
3319
|
l1 + 1 == l2 && i1 == i2 && id1 == id2
|
3279
3320
|
end
|
3280
3321
|
|
3281
|
-
chunks.each do |
|
3282
|
-
next if
|
3322
|
+
chunks.each do |elements|
|
3323
|
+
next if elements.size == 1
|
3283
3324
|
|
3284
3325
|
if scope == :hash_key
|
3285
3326
|
# Don't indent successive hash keys if any of them is in turn a hash
|
3286
3327
|
# or array literal that is formatted in separate lines.
|
3287
|
-
has_brace_newline =
|
3328
|
+
has_brace_newline = elements.any? do |(l, c)|
|
3288
3329
|
line_end = lines[l][c..-1]
|
3289
3330
|
line_end.start_with?("=> {\n", "=> [\n", "=> [ #", "=> { #", "[\n", "{\n", "[ #", "{ #")
|
3290
3331
|
end
|
3291
3332
|
next if has_brace_newline
|
3292
3333
|
end
|
3293
3334
|
|
3294
|
-
max_column =
|
3335
|
+
max_column = elements.map { |l, c| c }.max
|
3295
3336
|
|
3296
|
-
|
3337
|
+
elements.each do |(line, column, _, _, offset)|
|
3297
3338
|
next if column == max_column
|
3298
3339
|
|
3299
3340
|
split_index = column
|
data/lib/rufo/version.rb
CHANGED