rufo 0.0.34 → 0.0.35

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: d722fed549e79c6f97a6ec76c94574aa7b644520
4
- data.tar.gz: d4f1a92c881618644dd8c8b2aa71b6261b969014
3
+ metadata.gz: c8f842894649d7c49344b5fb5facbaf7be70325b
4
+ data.tar.gz: da16188e943e329386aa0c9ac945c7d71c15dc56
5
5
  SHA512:
6
- metadata.gz: c18dfd73b6ce027e04402131ec960ec4162b8d0a7b8ba91f70c7793ad5c2e17f1c847435d37ab5226580c358383a9c9eb507b6bbfa0551d4ce73a079e28fe004
7
- data.tar.gz: fda9a82998eea248c95efa63523ae67931b582acb12bcd9a3ef1023a8e58b8b36246a8a9235c50e08343ca27b5e455390ed540f7d158db46db910c6c9bc6554b
6
+ metadata.gz: 3f03c1fe682b3eb4a2e2b5c073bc18108854bea07781a06910eede1a1c7388ef2f2604c86b6b6721fa49d3574cf83a2d45d32b2eab981089da9e664609580190
7
+ data.tar.gz: cec6e4a31328301bfaaf61e304a523af85c2c8fdd79ae7939a3506320af836084bed546fdd33b82e953a9fb720c30a29abf7a2658bb3a03d848280dcbf39303c
data/README.md CHANGED
@@ -189,6 +189,7 @@ Allow spaces inside hash braces?
189
189
  - `:dynamic`: (default) if there's a space, keep it. Otherwise don't add it.
190
190
  - `:always`: always add a space
191
191
  - `:never`: never add a space
192
+ - `:match`: if there's a leading space, keep it (just one) and match the closing brace with one space before it
192
193
 
193
194
  With `:always`, hashes will look like this:
194
195
 
@@ -202,7 +203,14 @@ With `:never`, hashes will look like this:
202
203
  {:foo => 1, :bar => 2}
203
204
  ```
204
205
 
205
- With `:dynamic`, any of the above choices is fine.
206
+ With `:match`, hashes will look like any of these:
207
+
208
+ ```ruby
209
+ { :foo => 1, :bar => 2 }
210
+ {:foo => 1, :bar => 2}
211
+ ```
212
+
213
+ With `:dynamic`, any of the above choices is fine, and any amount of space (or zero) is preserved.
206
214
 
207
215
  ### spaces_inside_array_bracket
208
216
 
@@ -211,6 +219,7 @@ Allow spaces inside array brackets?
211
219
  - `:dynamic`: (default) if there's a space, keep it. Otherwise don't add it.
212
220
  - `:always`: always add a space
213
221
  - `:never`: never add a space
222
+ - `:match`: if there's a leading space, keep it (just one) and match the closing bracket with one space before it
214
223
 
215
224
  With `:always`, arrays will look like this:
216
225
 
@@ -224,6 +233,13 @@ With `:never`, arrays will look like this:
224
233
  [1, 2]
225
234
  ```
226
235
 
236
+ With `:match`, arrays will look like any of these:
237
+
238
+ ```ruby
239
+ [ 1, 2 ]
240
+ [1, 2]
241
+ ```
242
+
227
243
  With `:dynamic`, any of the above choices is fine.
228
244
 
229
245
  ### spaces_around_equal
data/lib/rufo/command.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "optionparser"
1
+ require "optparse"
2
2
 
3
3
  class Rufo::Command
4
4
  def self.run(argv)
@@ -144,6 +144,14 @@ class Rufo::Formatter
144
144
  # Case when positions
145
145
  @case_when_positions = []
146
146
 
147
+ # Methods that were writte in a single line, like:
148
+ #
149
+ # def foo; 1; end
150
+ #
151
+ # We want to track these because we allow consecutive inline defs
152
+ # to be together (without an empty line between them)
153
+ @one_line_defs = []
154
+
147
155
  # Settings
148
156
  indent_size options.fetch(:indent_size, 2)
149
157
  spaces_inside_hash_brace options.fetch(:spaces_inside_hash_brace, :dynamic)
@@ -178,11 +186,11 @@ class Rufo::Formatter
178
186
  end
179
187
 
180
188
  def spaces_inside_hash_brace(value)
181
- @spaces_inside_hash_brace = dynamic_always_never("spaces_inside_hash_brace", value)
189
+ @spaces_inside_hash_brace = dynamic_always_never_match("spaces_inside_hash_brace", value)
182
190
  end
183
191
 
184
192
  def spaces_inside_array_bracket(value)
185
- @spaces_inside_array_bracket = dynamic_always_never("spaces_inside_array_bracket", value)
193
+ @spaces_inside_array_bracket = dynamic_always_never_match("spaces_inside_array_bracket", value)
186
194
  end
187
195
 
188
196
  def spaces_around_equal(value)
@@ -259,6 +267,15 @@ class Rufo::Formatter
259
267
  end
260
268
  end
261
269
 
270
+ def dynamic_always_never_match(name, value)
271
+ case value
272
+ when :dynamic, :always, :never, :match
273
+ value
274
+ else
275
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :dynamic, :always, :never, :match")
276
+ end
277
+ end
278
+
262
279
  def one_dynamic(name, value)
263
280
  case value
264
281
  when :one, :dynamic
@@ -334,6 +351,7 @@ class Rufo::Formatter
334
351
  do_align_hash_keys if @align_hash_keys
335
352
  do_align_case_when if @align_case_when
336
353
  do_align_comments if @align_comments
354
+ remove_lines_before_one_line_defs
337
355
  end
338
356
 
339
357
  def visit(node)
@@ -640,13 +658,17 @@ class Rufo::Formatter
640
658
  end
641
659
  end
642
660
 
661
+ line_before_exp = @line
662
+
643
663
  push_node(exp) do
644
664
  visit exp
645
665
  end
646
666
 
647
- is_last = last?(i, exps)
667
+ if def?(exp) && @line == line_before_exp
668
+ @one_line_defs << @line
669
+ end
648
670
 
649
- skip_space unless is_last
671
+ is_last = last?(i, exps)
650
672
 
651
673
  line_before_endline = @line
652
674
 
@@ -694,6 +716,10 @@ class Rufo::Formatter
694
716
  false
695
717
  end
696
718
 
719
+ def def?(exp)
720
+ exp[0] == :def
721
+ end
722
+
697
723
  def visit_string_literal(node)
698
724
  # [:string_literal, [:string_content, exps]]
699
725
  heredoc = current_token_kind == :on_heredoc_beg
@@ -1008,7 +1034,9 @@ class Rufo::Formatter
1008
1034
  end
1009
1035
 
1010
1036
  @line_to_alignments_positions[@line] << [key, @column, target, target.size]
1011
- target << [@line, @column, @indent, id, offset]
1037
+ info = [@line, @column, @indent, id, offset]
1038
+ target << info
1039
+ info
1012
1040
  end
1013
1041
 
1014
1042
  def visit_ternary_if(node)
@@ -1430,7 +1458,7 @@ class Rufo::Formatter
1430
1458
  if void_exps?(body)
1431
1459
  consume_token :on_lbrace
1432
1460
  consume_block_args args
1433
- consume_one_dynamic_space_no_more_than_one @spaces_around_block_brace
1461
+ consume_one_dynamic_space @spaces_around_block_brace
1434
1462
  consume_token :on_rbrace
1435
1463
  return
1436
1464
  end
@@ -1441,14 +1469,14 @@ class Rufo::Formatter
1441
1469
  if current_token_line == closing_brace_token[0][0]
1442
1470
  consume_token :on_lbrace
1443
1471
  consume_block_args args
1444
- consume_one_dynamic_space_no_more_than_one @spaces_around_block_brace
1472
+ consume_one_dynamic_space @spaces_around_block_brace
1445
1473
  visit_exps body, with_lines: false
1446
1474
 
1447
1475
  while semicolon?
1448
1476
  next_token
1449
1477
  end
1450
1478
 
1451
- consume_one_dynamic_space_no_more_than_one @spaces_around_block_brace
1479
+ consume_one_dynamic_space @spaces_around_block_brace
1452
1480
 
1453
1481
  consume_token :on_rbrace
1454
1482
  return
@@ -1492,7 +1520,7 @@ class Rufo::Formatter
1492
1520
 
1493
1521
  def consume_block_args(args)
1494
1522
  if args
1495
- consume_one_dynamic_space_no_more_than_one @spaces_around_block_brace
1523
+ consume_one_dynamic_space_or_newline @spaces_around_block_brace
1496
1524
  # + 1 because of |...|
1497
1525
  # ^
1498
1526
  indent(@column + 1) do
@@ -1855,14 +1883,14 @@ class Rufo::Formatter
1855
1883
 
1856
1884
  if newline? || comment?
1857
1885
  indent(base_column || @indent) do
1858
- consume_end_of_line(want_multiline: false)
1886
+ consume_end_of_line(want_multiline: false, first_space: first_space)
1859
1887
  write_indent
1860
1888
  end
1861
1889
  elsif first_space && @spaces_after_comma == :dynamic
1862
1890
  write_space first_space[2]
1863
1891
  skip_space_or_newline
1864
1892
  else
1865
- write_space
1893
+ write_space if @spaces_after_comma == :one
1866
1894
  skip_space_or_newline
1867
1895
  end
1868
1896
  end
@@ -2195,11 +2223,9 @@ class Rufo::Formatter
2195
2223
  write_params_comma if needs_comma
2196
2224
  visit_comma_separated_list(args_with_default) do |arg, default|
2197
2225
  visit arg
2198
- skip_space
2199
- write_space
2226
+ consume_one_dynamic_space @spaces_around_equal
2200
2227
  consume_op "="
2201
- skip_space_or_newline
2202
- write_space
2228
+ consume_one_dynamic_space @spaces_around_equal
2203
2229
  visit default
2204
2230
  end
2205
2231
  needs_comma = true
@@ -2280,7 +2306,7 @@ class Rufo::Formatter
2280
2306
  write_space first_space[2]
2281
2307
  skip_space_or_newline
2282
2308
  else
2283
- write_space
2309
+ write_space if @spaces_after_comma == :one
2284
2310
  skip_space_or_newline
2285
2311
  end
2286
2312
  end
@@ -2511,6 +2537,7 @@ class Rufo::Formatter
2511
2537
 
2512
2538
  column = @column
2513
2539
 
2540
+ first_space = space? ? current_token : nil
2514
2541
  skip_space
2515
2542
 
2516
2543
  # Sometimes args comes with an array...
@@ -2526,6 +2553,9 @@ class Rufo::Formatter
2526
2553
  skip_space_or_newline
2527
2554
  end
2528
2555
  else
2556
+ if first_space && @spaces_inside_array_bracket == :dynamic
2557
+ write first_space[2]
2558
+ end
2529
2559
  needed_indent = column
2530
2560
  end
2531
2561
 
@@ -2536,7 +2566,13 @@ class Rufo::Formatter
2536
2566
  end
2537
2567
  end
2538
2568
 
2539
- skip_space_or_newline
2569
+ first_space = space? ? current_token : nil
2570
+ skip_space
2571
+ if newline? || comment?
2572
+ skip_space_or_newline
2573
+ elsif first_space && @spaces_inside_array_bracket == :dynamic
2574
+ write first_space[2]
2575
+ end
2540
2576
 
2541
2577
  check :on_rbracket
2542
2578
  write "]"
@@ -2789,6 +2825,7 @@ class Rufo::Formatter
2789
2825
  def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:)
2790
2826
  base_column = @column
2791
2827
  base_line = @line
2828
+ first_space = current_token if space?
2792
2829
  needs_final_space = (inside_hash || inside_array) && space?
2793
2830
  skip_space
2794
2831
 
@@ -2813,7 +2850,13 @@ class Rufo::Formatter
2813
2850
  if newline? || comment?
2814
2851
  needs_final_space = false
2815
2852
  elsif needs_final_space
2816
- consume_space
2853
+ if inside_hash && first_space && @spaces_inside_hash_brace == :dynamic
2854
+ write first_space[2]
2855
+ elsif inside_array && first_space && @spaces_inside_array_bracket == :dynamic
2856
+ write first_space[2]
2857
+ else
2858
+ consume_space
2859
+ end
2817
2860
  base_column = @column
2818
2861
  end
2819
2862
 
@@ -2835,6 +2878,7 @@ class Rufo::Formatter
2835
2878
 
2836
2879
  wrote_comma = false
2837
2880
  last_has_comma = false
2881
+ first_space = nil
2838
2882
 
2839
2883
  elements.each_with_index do |elem, i|
2840
2884
  is_last = last?(i, elements)
@@ -2847,11 +2891,24 @@ class Rufo::Formatter
2847
2891
  visit elem
2848
2892
  end
2849
2893
 
2894
+ first_space = space? ? current_token : nil
2895
+
2850
2896
  # We have to be careful not to aumatically write a heredoc on next_token,
2851
2897
  # because we miss the chance to write a comma to separate elements
2852
2898
  skip_space_no_heredoc_check
2853
2899
  wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
2854
2900
 
2901
+ if is_last && !comma? && !wrote_comma && !needs_trailing_comma && !comment?
2902
+ if (inside_hash && @spaces_inside_hash_brace == :dynamic) ||
2903
+ (inside_array && @spaces_inside_array_bracket == :dynamic)
2904
+ if first_space
2905
+ write first_space[2]
2906
+ else
2907
+ needs_final_space = false
2908
+ end
2909
+ end
2910
+ end
2911
+
2855
2912
  next unless comma?
2856
2913
 
2857
2914
  last_has_comma = true
@@ -2865,7 +2922,7 @@ class Rufo::Formatter
2865
2922
  # because we miss the chance to write a comma to separate elements
2866
2923
  next_token_no_heredoc_check
2867
2924
 
2868
- first_space = current_token if space?
2925
+ first_space = space? ? current_token : nil
2869
2926
 
2870
2927
  skip_space_no_heredoc_check
2871
2928
  wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
@@ -2874,12 +2931,14 @@ class Rufo::Formatter
2874
2931
  if is_last
2875
2932
  # Nothing
2876
2933
  else
2877
- consume_end_of_line
2878
- write_indent(needed_indent)
2934
+ indent(needed_indent) do
2935
+ consume_end_of_line(first_space: first_space)
2936
+ write_indent
2937
+ end
2879
2938
  end
2880
2939
  elsif !is_last && first_space && @spaces_after_comma == :dynamic
2881
2940
  write_space first_space[2]
2882
- else
2941
+ elsif @spaces_after_comma == :one
2883
2942
  write_space unless is_last
2884
2943
  end
2885
2944
  end
@@ -2894,10 +2953,10 @@ class Rufo::Formatter
2894
2953
  write "," if last_has_comma && !wrote_comma
2895
2954
  end
2896
2955
 
2897
- consume_end_of_line
2956
+ consume_end_of_line(first_space: first_space)
2898
2957
  write_indent
2899
2958
  elsif comment?
2900
- consume_end_of_line
2959
+ consume_end_of_line(first_space: first_space)
2901
2960
  else
2902
2961
  if needs_final_space
2903
2962
  consume_space
@@ -3053,10 +3112,10 @@ class Rufo::Formatter
3053
3112
  space_after_then = current_token if space? && preserve_whitespace
3054
3113
  skip_space
3055
3114
 
3056
- track_case_when
3115
+ info = track_case_when
3057
3116
  skip_semicolons
3058
3117
 
3059
- if newline? || comment?
3118
+ if newline?
3060
3119
  inline = false
3061
3120
 
3062
3121
  # Cancel tracking of `case when ... then` on a nelwine.
@@ -3070,6 +3129,23 @@ class Rufo::Formatter
3070
3129
 
3071
3130
  write "then"
3072
3131
 
3132
+ # We adjust the column and offset from:
3133
+ #
3134
+ # when 1 then 2
3135
+ # ^ (with offset 0)
3136
+ #
3137
+ # to:
3138
+ #
3139
+ # when 1 then 2
3140
+ # ^ (with offset 5)
3141
+ #
3142
+ # In that way we can align this with an `else` clause.
3143
+ if info
3144
+ offset = @column - info[1]
3145
+ info[1] = @column
3146
+ info[-1] = offset
3147
+ end
3148
+
3073
3149
  if space_after_then
3074
3150
  write_space space_after_then[2]
3075
3151
  else
@@ -3102,12 +3178,18 @@ class Rufo::Formatter
3102
3178
  if next_exp[0] == :else
3103
3179
  # [:else, body]
3104
3180
  consume_keyword "else"
3181
+ first_space = current_token if space?
3182
+ track_case_when
3105
3183
  skip_space
3106
3184
 
3107
3185
  if newline? || semicolon? || comment?
3108
3186
  indent_body next_exp[1]
3109
3187
  else
3110
- write_space
3188
+ if @spaces_around_when == :one || @align_case_when || !first_space
3189
+ write_space
3190
+ else
3191
+ write_space first_space[2]
3192
+ end
3111
3193
  visit_exps next_exp[1]
3112
3194
  end
3113
3195
  else
@@ -3146,14 +3228,6 @@ class Rufo::Formatter
3146
3228
  end
3147
3229
  end
3148
3230
 
3149
- def consume_one_dynamic_space_no_more_than_one(setting)
3150
- if setting == :one
3151
- consume_space
3152
- else
3153
- consume_space if space?
3154
- end
3155
- end
3156
-
3157
3231
  def consume_one_dynamic_space_or_newline(setting)
3158
3232
  first_space = current_token if space?
3159
3233
  skip_space
@@ -3285,18 +3359,20 @@ class Rufo::Formatter
3285
3359
  # - at_prefix: are we at a point before an expression? (if so, we don't need a space before the first comment)
3286
3360
  # - want_semicolon: do we want do print a semicolon to separate expressions?
3287
3361
  # - want_multiline: do we want multiple lines to appear, or at most one?
3288
- def consume_end_of_line(at_prefix: false, want_semicolon: false, want_multiline: true, needs_two_lines_on_comment: false)
3362
+ def consume_end_of_line(at_prefix: false, want_semicolon: false, want_multiline: true, needs_two_lines_on_comment: false, first_space: nil)
3289
3363
  found_newline = false # Did we find any newline during this method?
3290
3364
  found_comment_after_newline = false # Did we find a comment after some newline?
3291
3365
  last = nil # Last token kind found
3292
3366
  multilple_lines = false # Did we pass through more than one newline?
3293
3367
  last_comment_has_newline = false # Does the last comment has a newline?
3294
3368
  newline_count = 0 # Number of newlines we passed
3369
+ last_space = first_space # Last found space
3295
3370
 
3296
3371
  loop do
3297
3372
  case current_token_kind
3298
3373
  when :on_sp
3299
3374
  # Ignore spaces
3375
+ last_space = current_token
3300
3376
  next_token
3301
3377
  when :on_nl, :on_ignored_nl
3302
3378
  # I don't know why but sometimes a on_ignored_nl
@@ -3384,7 +3460,14 @@ class Rufo::Formatter
3384
3460
  else
3385
3461
  # If we didn't find any newline yet, this is the first comment,
3386
3462
  # so append a space if needed (for example after an expression)
3387
- write_space unless at_prefix
3463
+ unless at_prefix
3464
+ # Preserve whitespace before comment unless we need to align them
3465
+ if last_space && !@align_comments
3466
+ write last_space[2]
3467
+ else
3468
+ write_space
3469
+ end
3470
+ end
3388
3471
 
3389
3472
  # First we check if the comment was aligned to the previous comment
3390
3473
  # in the previous line, in order to keep them like that.
@@ -3870,6 +3953,8 @@ class Rufo::Formatter
3870
3953
 
3871
3954
  while line_to_call_info = @line_to_call_info.shift
3872
3955
  first_line, call_info = line_to_call_info
3956
+ next unless call_info.size == 5
3957
+
3873
3958
  indent, first_param_indent, needs_dedent, first_paren_end_line, last_line = call_info
3874
3959
  next unless needs_dedent
3875
3960
  next unless first_paren_end_line == last_line
@@ -4009,6 +4094,20 @@ class Rufo::Formatter
4009
4094
  end
4010
4095
  end
4011
4096
 
4097
+ def remove_lines_before_one_line_defs
4098
+ return if @one_line_defs.empty?
4099
+
4100
+ lines = @output.lines
4101
+
4102
+ @one_line_defs.reverse.each_cons(2) do |after, before|
4103
+ if before + 2 == after && lines[before + 1].strip.empty?
4104
+ lines.delete_at(before + 1)
4105
+ end
4106
+ end
4107
+
4108
+ @output = lines.join
4109
+ end
4110
+
4012
4111
  def result
4013
4112
  @output
4014
4113
  end
data/lib/rufo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.34"
2
+ VERSION = "0.0.35"
3
3
  end
data/rufo.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["asterite@gmail.com"]
11
11
 
12
12
  spec.summary = %q{Ruby code formatter}
13
- spec.description = %q{Ruby code formatter}
13
+ spec.description = %q{Fast and unobtrusive Ruby code formatter}
14
14
  spec.homepage = "https://github.com/asterite/rufo"
15
15
  spec.license = "MIT"
16
16
 
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.34
4
+ version: 0.0.35
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-07-03 00:00:00.000000000 Z
11
+ date: 2017-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- description: Ruby code formatter
55
+ description: Fast and unobtrusive Ruby code formatter
56
56
  email:
57
57
  - asterite@gmail.com
58
58
  executables: