rufo 0.0.20 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b322ac4c05931d04475299c6b98e3ca70537941c
4
- data.tar.gz: 707eeb3653a5e309028fd3aea0f104654f9d7e4d
3
+ metadata.gz: c8ea364388703323772b0c25eafeb7bd9c5de665
4
+ data.tar.gz: b502a49f92eededb4eaba9cea8fd8acd16b6d5a5
5
5
  SHA512:
6
- metadata.gz: 14cbee46609b75b46199a5dcd1b54f6066efc8b1fda1ec1e0f6494191f41fe6f0d8148c632c46bcb6868cd7a8ade5421d9d0a04dfbaaa125c51300b65d1a273c
7
- data.tar.gz: 4b983949675d6764b73ecf1479ad26992aace76b197b1e96a348a8699416e83b2da645bf2392f066d5dca30a1d08e8c486ecb7a11c1ef5a9f2c98390f0373a59
6
+ metadata.gz: 1c9f13c1604467276a1e59fb05f8b1de3cb80986aa94df270c5cd40b994b1aa33a78ac045801ba7788f5edcded56f55fad201bd26d2000f983c9a6cdf036d198
7
+ data.tar.gz: 7764a8b0d8502712a0d484bf43fbf1f4c7413bd8749093c6c789a9f7e04ddea4cf4153bd26f9ff8e556e4100354ed498d72de228ffa2fcfb6c928d415defd4a2
data/.rufo CHANGED
@@ -1,4 +1,5 @@
1
1
  space_after_hash_brace :dynamic
2
+ preserve_whitespace true
2
3
  align_comments true
3
4
  align_assignments false
4
5
  align_hash_keys true
data/README.md CHANGED
@@ -77,6 +77,16 @@ align_hash_keys true
77
77
  # Whether to align successive case when (default: true)
78
78
  align_case_when true
79
79
 
80
+ # Preserve whitespace after assignments target and values,
81
+ # after calls that start with a space, hash arrows and commas (default: true).
82
+ #
83
+ # This allows for manual alignment of some code that would otherwise
84
+ # be impossible to automatically format or preserve "beautiful".
85
+ #
86
+ # If `align_assignments` is true, this doesn't apply to assignments.
87
+ # If `align_hash_keys` is true, this doesn't apply to hash keys.
88
+ preserve_whitespace true
89
+
80
90
  # The indent size (default: 2)
81
91
  indent_size 2
82
92
  ```
@@ -62,6 +62,7 @@ class Rufo::Formatter
62
62
  align_assignments options.fetch(:align_assignments, false)
63
63
  align_hash_keys options.fetch(:align_hash_keys, true)
64
64
  align_case_when options.fetch(:align_case_when, true)
65
+ preserve_whitespace options.fetch(:preserve_whitespace, true)
65
66
  end
66
67
 
67
68
  # The indent size (default: 2)
@@ -103,6 +104,18 @@ class Rufo::Formatter
103
104
  @align_case_when = value
104
105
  end
105
106
 
107
+ # Preserve whitespace after assignments target and values,
108
+ # after calls that start with a space, hash arrows and commas.
109
+ #
110
+ # This allows for manual alignment of some code that would otherwise
111
+ # be impossible to automatically format or preserve "beautiful".
112
+ #
113
+ # If `align_assignments` is true, this doesn't apply to assignments.
114
+ # If `align_hash_keys` is true, this doesn't apply to hash keys.
115
+ def preserve_whitespace(value)
116
+ @preserve_whitespace = value
117
+ end
118
+
106
119
  def format
107
120
  visit @sexp
108
121
  consume_end
@@ -418,11 +431,12 @@ class Rufo::Formatter
418
431
  visit exp
419
432
  end
420
433
 
421
- skip_space
434
+ is_last = last?(i, exps)
435
+
436
+ skip_space unless is_last
422
437
 
423
438
  line_before_endline = @line
424
439
 
425
- is_last = last?(i, exps)
426
440
  if with_lines
427
441
  exp_needs_two_lines = needs_two_lines?(exp)
428
442
 
@@ -511,7 +525,8 @@ class Rufo::Formatter
511
525
 
512
526
  visit string1
513
527
 
514
- if skip_space_backslash
528
+ has_backslash, first_space = skip_space_backslash
529
+ if has_backslash
515
530
  write " \\"
516
531
  write_line
517
532
  write_indent
@@ -597,7 +612,7 @@ class Rufo::Formatter
597
612
  _, target, value = node
598
613
 
599
614
  visit target
600
- consume_space
615
+ consume_space(!@align_assignments)
601
616
  track_assignment
602
617
  consume_op "="
603
618
  visit_assign_value value
@@ -609,7 +624,7 @@ class Rufo::Formatter
609
624
  # [:opassign, target, op, value]
610
625
  _, target, op, value = node
611
626
  visit target
612
- consume_space
627
+ consume_space(!@align_assignments)
613
628
 
614
629
  # [:@op, "+=", [1, 2]],
615
630
  check :on_op
@@ -635,16 +650,17 @@ class Rufo::Formatter
635
650
  # A trailing comma can come after the left hand side
636
651
  consume_token :on_comma if comma?
637
652
 
638
- consume_space
653
+ consume_space(!@align_assignments)
639
654
  track_assignment
640
655
  consume_op "="
641
656
  visit_assign_value right
642
657
  end
643
658
 
644
659
  def visit_assign_value(value)
660
+ first_space = current_token if space?
645
661
  skip_space
646
662
 
647
- indent_after_space value, indentable_value?(value)
663
+ indent_after_space value, indentable_value?(value), true, first_space
648
664
  end
649
665
 
650
666
  def indentable_value?(value)
@@ -703,25 +719,33 @@ class Rufo::Formatter
703
719
  _, cond, then_body, else_body = node
704
720
 
705
721
  visit cond
706
- consume_space
722
+ consume_space(true)
707
723
  consume_op "?"
708
724
 
725
+ first_space = current_token if space?
726
+
709
727
  skip_space
710
728
  if newline? || comment?
711
729
  consume_end_of_line
712
730
  write_indent(next_indent)
731
+ elsif first_space && @preserve_whitespace
732
+ write_space first_space[2]
713
733
  else
714
734
  consume_space
715
735
  end
716
736
 
717
737
  visit then_body
718
- consume_space
738
+ consume_space(true)
719
739
  consume_op ":"
720
740
 
741
+ first_space = current_token if space?
721
742
  skip_space
743
+
722
744
  if newline? || comment?
723
745
  consume_end_of_line
724
746
  write_indent(next_indent)
747
+ elsif first_space && @preserve_whitespace
748
+ write_space first_space[2]
725
749
  else
726
750
  consume_space
727
751
  end
@@ -742,9 +766,10 @@ class Rufo::Formatter
742
766
  end
743
767
 
744
768
  visit body
745
- consume_space
769
+
770
+ consume_space(true)
746
771
  consume_keyword(suffix)
747
- consume_space
772
+ consume_space(true)
748
773
  visit cond
749
774
  end
750
775
 
@@ -837,6 +862,7 @@ class Rufo::Formatter
837
862
 
838
863
  push_call(node) do
839
864
  visit args_node
865
+ skip_space
840
866
  end
841
867
 
842
868
  found_comma = comma?
@@ -885,10 +911,15 @@ class Rufo::Formatter
885
911
 
886
912
  push_call(node) do
887
913
  visit name
888
- if skip_space_backslash
914
+
915
+ has_backslash, first_space = skip_space_backslash
916
+ if has_backslash
889
917
  write " \\"
890
918
  write_line
891
919
  write_indent(next_indent)
920
+ elsif first_space && @preserve_whitespace
921
+ write_space first_space[2]
922
+ skip_space_or_newline
892
923
  else
893
924
  consume_space
894
925
  end
@@ -1117,6 +1148,7 @@ class Rufo::Formatter
1117
1148
  end
1118
1149
 
1119
1150
  if block_arg
1151
+ skip_space_or_newline
1120
1152
  write_params_comma if comma?
1121
1153
 
1122
1154
  consume_op "&"
@@ -1250,6 +1282,7 @@ class Rufo::Formatter
1250
1282
 
1251
1283
  indent(@column) do
1252
1284
  visit_comma_separated_list args
1285
+ skip_space_or_newline
1253
1286
  end
1254
1287
 
1255
1288
  check :on_rparen
@@ -1362,22 +1395,28 @@ class Rufo::Formatter
1362
1395
  visit exp
1363
1396
  end
1364
1397
  end
1398
+
1399
+ next if last?(i, nodes)
1400
+
1365
1401
  skip_space
1366
- unless last?(i, nodes)
1367
- check :on_comma
1368
- write ","
1369
- next_token
1370
- skip_space
1402
+ check :on_comma
1403
+ write ","
1404
+ next_token
1371
1405
 
1372
- if newline? || comment?
1373
- indent(base_column || @indent) do
1374
- consume_end_of_line(false, false, false)
1375
- write_indent
1376
- end
1377
- else
1378
- write_space
1379
- skip_space_or_newline
1406
+ first_space = current_token if space?
1407
+ skip_space
1408
+
1409
+ if newline? || comment?
1410
+ indent(base_column || @indent) do
1411
+ consume_end_of_line(false, false, false)
1412
+ write_indent
1380
1413
  end
1414
+ elsif first_space && @preserve_whitespace
1415
+ write_space first_space[2]
1416
+ skip_space_or_newline
1417
+ else
1418
+ write_space
1419
+ skip_space_or_newline
1381
1420
  end
1382
1421
  end
1383
1422
  end
@@ -1409,7 +1448,7 @@ class Rufo::Formatter
1409
1448
  consume_op_or_keyword op
1410
1449
 
1411
1450
  if op == :not
1412
- consume_space
1451
+ consume_space(true)
1413
1452
  else
1414
1453
  skip_space_or_newline
1415
1454
  end
@@ -1428,11 +1467,14 @@ class Rufo::Formatter
1428
1467
  needs_space = op != :* && op != :/ && op != :**
1429
1468
  end
1430
1469
 
1431
- if skip_space_backslash
1470
+ has_backslash, first_space = skip_space_backslash
1471
+ if has_backslash
1432
1472
  needs_space = true
1433
1473
  write " \\"
1434
1474
  write_line
1435
1475
  write_indent(next_indent)
1476
+ elsif @preserve_whitespace && first_space
1477
+ write_space first_space[2]
1436
1478
  else
1437
1479
  write_space if needs_space
1438
1480
  end
@@ -1711,11 +1753,16 @@ class Rufo::Formatter
1711
1753
  check :on_comma
1712
1754
  write ","
1713
1755
  next_token
1756
+
1757
+ first_space = current_token if space?
1714
1758
  skip_space
1715
1759
 
1716
1760
  if newline? || comment?
1717
1761
  consume_end_of_line
1718
1762
  write_indent
1763
+ elsif first_space && @preserve_whitespace
1764
+ write_space first_space[2]
1765
+ skip_space_or_newline
1719
1766
  else
1720
1767
  write_space
1721
1768
  skip_space_or_newline
@@ -1859,8 +1906,7 @@ class Rufo::Formatter
1859
1906
 
1860
1907
  visit key
1861
1908
 
1862
- skip_space_or_newline
1863
- consume_space
1909
+ consume_space(!@align_hash_keys)
1864
1910
 
1865
1911
  track_hash_key
1866
1912
 
@@ -1868,8 +1914,7 @@ class Rufo::Formatter
1868
1914
  # or `"label": value`
1869
1915
  if symbol || !(key[0] == :@label || key[0] == :dyna_symbol)
1870
1916
  consume_op "=>"
1871
- skip_space_or_newline
1872
- write_space
1917
+ consume_space(!@align_hash_keys)
1873
1918
  end
1874
1919
 
1875
1920
  visit value
@@ -2229,23 +2274,22 @@ class Rufo::Formatter
2229
2274
  end
2230
2275
  skip_space
2231
2276
 
2232
- if comma?
2233
- is_last = last?(i, elements)
2277
+ next unless comma?
2278
+ is_last = last?(i, elements)
2234
2279
 
2235
- write "," unless is_last
2236
- next_token
2237
- skip_space
2280
+ write "," unless is_last
2281
+ next_token
2282
+ skip_space
2238
2283
 
2239
- if newline? || comment?
2240
- if is_last
2241
- # Nothing
2242
- else
2243
- consume_end_of_line
2244
- write_indent(needed_indent)
2245
- end
2284
+ if newline? || comment?
2285
+ if is_last
2286
+ # Nothing
2246
2287
  else
2247
- write_space unless is_last
2288
+ consume_end_of_line
2289
+ write_indent(needed_indent)
2248
2290
  end
2291
+ else
2292
+ write_space unless is_last
2249
2293
  end
2250
2294
  end
2251
2295
 
@@ -2369,7 +2413,8 @@ class Rufo::Formatter
2369
2413
  write " do "
2370
2414
  end
2371
2415
  visit_exps body, false, false
2372
- consume_space
2416
+ skip_space_or_newline
2417
+ write_space if is_do
2373
2418
  end
2374
2419
  else
2375
2420
  indent_body body
@@ -2467,33 +2512,41 @@ class Rufo::Formatter
2467
2512
  end
2468
2513
  end
2469
2514
 
2470
- def consume_space
2471
- skip_space_or_newline
2472
- write_space unless @output[-1] == " "
2515
+ def consume_space(want_preserve_whitespace = false)
2516
+ first_space = current_token if space?
2517
+ skip_space
2518
+ if want_preserve_whitespace && @preserve_whitespace && !newline? && !comment? && first_space
2519
+ write_space first_space[2] unless @output[-1] == " "
2520
+ skip_space_or_newline
2521
+ else
2522
+ skip_space_or_newline
2523
+ write_space unless @output[-1] == " "
2524
+ end
2473
2525
  end
2474
2526
 
2475
2527
  def skip_space
2476
- while space?
2477
- next_token
2478
- end
2528
+ next_token while space?
2479
2529
  end
2480
2530
 
2481
2531
  def skip_space_backslash
2532
+ return [false, false] unless space?
2533
+
2534
+ first_space = current_token
2482
2535
  has_slash_newline = false
2483
2536
  while space?
2484
2537
  has_slash_newline ||= current_token_value == "\\\n"
2485
2538
  next_token
2486
2539
  end
2487
- has_slash_newline
2540
+ [has_slash_newline, first_space]
2488
2541
  end
2489
2542
 
2490
- def skip_space_or_newline(want_semicolon = false, write_first_semicolon = false)
2543
+ def skip_space_or_newline(_want_semicolon = false, write_first_semicolon = false)
2491
2544
  found_newline = false
2492
2545
  found_comment = false
2493
2546
  found_semicolon = false
2494
2547
  last = nil
2495
2548
 
2496
- while true
2549
+ loop do
2497
2550
  case current_token_kind
2498
2551
  when :on_sp
2499
2552
  next_token
@@ -2590,7 +2643,7 @@ class Rufo::Formatter
2590
2643
  last_comment_has_newline = false # Does the last comment has a newline?
2591
2644
  newline_count = 0 # Number of newlines we passed
2592
2645
 
2593
- while true
2646
+ loop do
2594
2647
  case current_token_kind
2595
2648
  when :on_sp
2596
2649
  # Ignore spaces
@@ -2598,7 +2651,7 @@ class Rufo::Formatter
2598
2651
  when :on_nl, :on_ignored_nl
2599
2652
  # I don't know why but sometimes a on_ignored_nl
2600
2653
  # can appear with nil as the "text", and that's wrong
2601
- if current_token[2] == nil
2654
+ if current_token[2].nil?
2602
2655
  next_token
2603
2656
  next
2604
2657
  end
@@ -2765,8 +2818,8 @@ class Rufo::Formatter
2765
2818
  @column += value.size
2766
2819
  end
2767
2820
 
2768
- def write_space
2769
- @output << " "
2821
+ def write_space(value = " ")
2822
+ @output << value
2770
2823
  @column += 1
2771
2824
  end
2772
2825
 
@@ -2783,7 +2836,9 @@ class Rufo::Formatter
2783
2836
  @last_was_newline = false
2784
2837
  end
2785
2838
 
2786
- def indent_after_space(node, sticky = false, want_space = true)
2839
+ def indent_after_space(node, sticky = false, want_space = true, first_space = nil)
2840
+ first_space = current_token if space?
2841
+
2787
2842
  skip_space
2788
2843
  case current_token_kind
2789
2844
  when :on_ignored_nl, :on_comment
@@ -2793,7 +2848,13 @@ class Rufo::Formatter
2793
2848
  visit node
2794
2849
  end
2795
2850
  else
2796
- write_space if want_space
2851
+ if want_space
2852
+ if first_space && @preserve_whitespace
2853
+ write_space first_space[2]
2854
+ else
2855
+ write_space
2856
+ end
2857
+ end
2797
2858
  if sticky
2798
2859
  indent(@column) do
2799
2860
  visit node
@@ -2966,14 +3027,7 @@ class Rufo::Formatter
2966
3027
  # or array literal that is formatted in separate lines.
2967
3028
  has_brace_newline = comments.any? do |(l, c)|
2968
3029
  line_end = lines[l][c..-1]
2969
- line_end.start_with?("=> {\n") ||
2970
- line_end.start_with?("=> [\n") ||
2971
- line_end.start_with?("=> [ #") ||
2972
- line_end.start_with?("=> { #") ||
2973
- line_end.start_with?("[\n") ||
2974
- line_end.start_with?("{\n") ||
2975
- line_end.start_with?("[ #") ||
2976
- line_end.start_with?("{ #")
3030
+ line_end.start_with?("=> {\n", "=> [\n", "=> [ #", "=> { #", "[\n", "{\n", "[ #", "{ #")
2977
3031
  end
2978
3032
  next if has_brace_newline
2979
3033
  end
data/lib/rufo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.20"
2
+ VERSION = "0.0.21"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.20
4
+ version: 0.0.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ary Borenszweig