rufo 0.0.35 → 0.0.36

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: c8f842894649d7c49344b5fb5facbaf7be70325b
4
- data.tar.gz: da16188e943e329386aa0c9ac945c7d71c15dc56
3
+ metadata.gz: 217daf75d51d00f8dc47f3b98c8e4403d10a4d99
4
+ data.tar.gz: c95e8308df7067fb8221f99e4a19804b3c82c09d
5
5
  SHA512:
6
- metadata.gz: 3f03c1fe682b3eb4a2e2b5c073bc18108854bea07781a06910eede1a1c7388ef2f2604c86b6b6721fa49d3574cf83a2d45d32b2eab981089da9e664609580190
7
- data.tar.gz: cec6e4a31328301bfaaf61e304a523af85c2c8fdd79ae7939a3506320af836084bed546fdd33b82e953a9fb720c30a29abf7a2658bb3a03d848280dcbf39303c
6
+ metadata.gz: 623c8db7e7e98ae87904f55cc6661beabc119adac24593fc533f82e03893deff64328a2a2ecb6db46d39e43e477c0b74da8e7d8f1556c394c7105a21792e3666
7
+ data.tar.gz: 229519b8ba228f3924b4b1340a48fdcbd1dd97bebaf1604702b57a1faf1bfa3ab29844d31e50a4d5f1f94cbaa46b3b2ed28e363d36a6fc486f4d3b6c90676f05
data/.travis.yml CHANGED
@@ -5,6 +5,7 @@ rvm:
5
5
  - 2.4.1
6
6
  - 2.3.4
7
7
  - 2.2.7
8
+ - ruby-head
8
9
  before_install: gem install bundler -v 1.15.1
9
10
  script:
10
11
  - bundle exec rake ci
data/README.md CHANGED
@@ -144,11 +144,12 @@ according to **rufo**, and will exit with exit code 1.
144
144
 
145
145
  ## Editor support
146
146
 
147
- - Sublime Text: [sublime-rufo](https://github.com/asterite/sublime-rufo)
148
- - Visual Studio Code: [rufo-vscode](https://marketplace.visualstudio.com/items?itemName=siliconsenthil.rufo-vscode)
149
- - Vim: [rufo-vim](https://github.com/splattael/rufo-vim) :construction:
150
147
  - Atom: [rufo-atom](https://github.com/bmulvihill/rufo-atom) :construction:
151
148
  - Emacs [emacs-rufo](https://github.com/aleandros/emacs-rufo) :construction: or [rufo-mode.el](https://github.com/danielma/rufo-mode.el) :construction:
149
+ - Sublime Text: [sublime-rufo](https://github.com/asterite/sublime-rufo)
150
+ - Vim: [rufo-vim](https://github.com/splattael/rufo-vim)
151
+ - Visual Studio Code: [rufo-vscode](https://marketplace.visualstudio.com/items?itemName=siliconsenthil.rufo-vscode)
152
+
152
153
 
153
154
  Did you write a plugin for your favorite editor? That's great! Let me know about it and
154
155
  I will list it here.
@@ -194,7 +195,7 @@ Allow spaces inside hash braces?
194
195
  With `:always`, hashes will look like this:
195
196
 
196
197
  ```ruby
197
- { :foo => 1, :bar => 2}
198
+ { :foo => 1, :bar => 2 }
198
199
  ```
199
200
 
200
201
  With `:never`, hashes will look like this:
@@ -268,7 +269,7 @@ With `:dynamic` it won't modify it.
268
269
  If `align_assignments` is `true`, then this setting has no effect and `:one`
269
270
  will be used when no other assignments are above/below an assignment.
270
271
 
271
- ### spaces_around_ternary
272
+ ### spaces_in_ternary
272
273
 
273
274
  How to format spaces around a ternary (`cond ? then : else`) operator?
274
275
 
@@ -545,7 +546,30 @@ before the operator, a space is added after it.
545
546
 
546
547
  With `:dynamic` it won't modify it.
547
548
 
548
- ### parens_in_defs
549
+ ### spaces_after_method_name
550
+
551
+ How to format spaces after a method name?
552
+
553
+ - `:dynamic`: (default) allow any number of spaces
554
+ - `:no`: no spaces after a method name
555
+
556
+ Given this code:
557
+
558
+ ```ruby
559
+ def plus_one (x) x + 1 end
560
+ def plus_twenty(x) x + 20 end
561
+ ```
562
+
563
+ With `:no` the formatter will change it to:
564
+
565
+ ```ruby
566
+ def plus_one(x) x + 1 end
567
+ def plus_twenty(x) x + 20 end
568
+ ```
569
+
570
+ With `:dynamic` it won't modify it.
571
+
572
+ ### parens_in_def
549
573
 
550
574
  Use parentheses in defs?
551
575
 
data/lib/rufo/backport.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rufo::Backport
2
4
  # Implement Enumerable#chunk_while
3
5
  # if it's not available in the current Ruby version
data/lib/rufo/command.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "optparse"
2
4
 
3
5
  class Rufo::Command
data/lib/rufo/dot_file.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Rufo::DotFile
2
4
  def initialize
3
5
  @cache = {}
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "ripper"
2
4
 
3
5
  class Rufo::Formatter
@@ -20,7 +22,7 @@ class Rufo::Formatter
20
22
  @line = 0
21
23
  @column = 0
22
24
  @last_was_newline = true
23
- @output = ""
25
+ @output = "".dup
24
26
 
25
27
  # The column of a `obj.method` call, so we can align
26
28
  # calls to that dot
@@ -144,13 +146,15 @@ class Rufo::Formatter
144
146
  # Case when positions
145
147
  @case_when_positions = []
146
148
 
147
- # Methods that were writte in a single line, like:
149
+ # Declarations that are written in a single line, like:
148
150
  #
149
151
  # def foo; 1; end
150
152
  #
151
153
  # We want to track these because we allow consecutive inline defs
152
154
  # to be together (without an empty line between them)
153
- @one_line_defs = []
155
+ #
156
+ # This is [[line, original_line], ...]
157
+ @inline_declarations = []
154
158
 
155
159
  # Settings
156
160
  indent_size options.fetch(:indent_size, 2)
@@ -168,6 +172,7 @@ class Rufo::Formatter
168
172
  spaces_after_lambda_arrow options.fetch(:spaces_after_lambda_arrow, :dynamic)
169
173
  spaces_around_unary options.fetch(:spaces_around_unary, :dynamic)
170
174
  spaces_around_binary options.fetch(:spaces_around_binary, :dynamic)
175
+ spaces_after_method_name options.fetch(:spaces_after_method_name, :dynamic)
171
176
  parens_in_def options.fetch(:parens_in_def, :dynamic)
172
177
  double_newline_inside_type options.fetch(:double_newline_inside_type, :dynamic)
173
178
  visibility_indent options.fetch(:visibility_indent, :dynamic)
@@ -241,6 +246,10 @@ class Rufo::Formatter
241
246
  @spaces_around_binary = one_dynamic("spaces_around_binary", value)
242
247
  end
243
248
 
249
+ def spaces_after_method_name(value)
250
+ @spaces_after_method_name = no_dynamic("spaces_after_method_name", value)
251
+ end
252
+
244
253
  def parens_in_def(value)
245
254
  @parens_in_def = yes_dynamic("parens_in_def", value)
246
255
  end
@@ -351,7 +360,7 @@ class Rufo::Formatter
351
360
  do_align_hash_keys if @align_hash_keys
352
361
  do_align_case_when if @align_case_when
353
362
  do_align_comments if @align_comments
354
- remove_lines_before_one_line_defs
363
+ remove_lines_before_inline_declarations
355
364
  end
356
365
 
357
366
  def visit(node)
@@ -416,9 +425,12 @@ class Rufo::Formatter
416
425
  # For heredocs with tilde we sometimes need to align the contents
417
426
  if heredoc && tilde && @last_was_newline
418
427
  write_indent(next_indent) unless current_token_value == "\n"
419
- check :on_tstring_content
420
- consume_token_value(current_token_value)
421
- next_token
428
+ skip_ignored_space
429
+ if current_token_kind == :on_tstring_content
430
+ check :on_tstring_content
431
+ consume_token_value(current_token_value)
432
+ next_token
433
+ end
422
434
  else
423
435
  consume_token :on_tstring_content
424
436
  end
@@ -553,6 +565,8 @@ class Rufo::Formatter
553
565
  visit_mrhs_new_from_args(node)
554
566
  when :mlhs_paren
555
567
  visit_mlhs_paren(node)
568
+ when :mlhs
569
+ visit_mlhs(node)
556
570
  when :mrhs_add_star
557
571
  visit_mrhs_add_star(node)
558
572
  when :def
@@ -620,6 +634,8 @@ class Rufo::Formatter
620
634
  visit_undef(node)
621
635
  when :mlhs_add_star
622
636
  visit_mlhs_add_star(node)
637
+ when :rest_param
638
+ visit_rest_param(node)
623
639
  when :retry
624
640
  # [:retry]
625
641
  consume_keyword "retry"
@@ -659,13 +675,14 @@ class Rufo::Formatter
659
675
  end
660
676
 
661
677
  line_before_exp = @line
678
+ original_line = current_token_line
662
679
 
663
680
  push_node(exp) do
664
681
  visit exp
665
682
  end
666
683
 
667
- if def?(exp) && @line == line_before_exp
668
- @one_line_defs << @line
684
+ if declaration?(exp) && @line == line_before_exp
685
+ @inline_declarations << [@line, original_line]
669
686
  end
670
687
 
671
688
  is_last = last?(i, exps)
@@ -716,8 +733,13 @@ class Rufo::Formatter
716
733
  false
717
734
  end
718
735
 
719
- def def?(exp)
720
- exp[0] == :def
736
+ def declaration?(exp)
737
+ case exp[0]
738
+ when :def, :class, :module
739
+ true
740
+ else
741
+ false
742
+ end
721
743
  end
722
744
 
723
745
  def visit_string_literal(node)
@@ -1396,11 +1418,7 @@ class Rufo::Formatter
1396
1418
 
1397
1419
  # We align call parameters to the first paramter
1398
1420
  indent(needed_indent) do
1399
- if args[0].is_a?(Symbol)
1400
- visit args
1401
- else
1402
- visit_exps args, with_lines: false
1403
- end
1421
+ visit_exps to_ary(args), with_lines: false
1404
1422
  end
1405
1423
 
1406
1424
  if call_info && call_info.size > 2
@@ -1512,10 +1530,13 @@ class Rufo::Formatter
1512
1530
 
1513
1531
  consume_block_args args
1514
1532
 
1515
- indent_body body
1516
-
1517
- write_indent if @line != line
1518
- consume_keyword "end"
1533
+ if body.first == :bodystmt
1534
+ visit_bodystmt body
1535
+ else
1536
+ indent_body body
1537
+ write_indent unless @line == line
1538
+ consume_keyword "end"
1539
+ end
1519
1540
  end
1520
1541
 
1521
1542
  def consume_block_args(args)
@@ -1705,11 +1726,7 @@ class Rufo::Formatter
1705
1726
  end
1706
1727
 
1707
1728
  def visit_rescue_types(node)
1708
- if node[0].is_a?(Symbol)
1709
- visit node
1710
- else
1711
- visit_exps node, with_lines: false
1712
- end
1729
+ visit_exps to_ary(node), with_lines: false
1713
1730
  end
1714
1731
 
1715
1732
  def visit_mrhs_new_from_args(node)
@@ -1721,10 +1738,8 @@ class Rufo::Formatter
1721
1738
  visit_comma_separated_list exps
1722
1739
  write_params_comma
1723
1740
  visit final_exp
1724
- elsif exps[0].is_a?(Symbol)
1725
- visit exps
1726
1741
  else
1727
- visit_comma_separated_list exps
1742
+ visit_comma_separated_list to_ary(exps)
1728
1743
  end
1729
1744
  end
1730
1745
 
@@ -1734,8 +1749,18 @@ class Rufo::Formatter
1734
1749
  # ]
1735
1750
  _, args = node
1736
1751
 
1737
- # For :mlsh_paren, sometimes a paren comes,
1738
- # some times not, so act accordingly.
1752
+ visit_mlhs_or_mlhs_paren(args)
1753
+ end
1754
+
1755
+ def visit_mlhs(node)
1756
+ # [:mlsh, *args]
1757
+ _, *args = node
1758
+
1759
+ visit_mlhs_or_mlhs_paren(args)
1760
+ end
1761
+
1762
+ def visit_mlhs_or_mlhs_paren(args)
1763
+ # Sometimes a paren comes, some times not, so act accordingly.
1739
1764
  has_paren = current_token_kind == :on_lparen
1740
1765
  if has_paren
1741
1766
  consume_token :on_lparen
@@ -1792,11 +1817,7 @@ class Rufo::Formatter
1792
1817
  consume_keyword "for"
1793
1818
  consume_space
1794
1819
 
1795
- if var[0].is_a?(Symbol)
1796
- visit var
1797
- else
1798
- visit_comma_separated_list var
1799
- end
1820
+ visit_comma_separated_list to_ary(var)
1800
1821
 
1801
1822
  consume_space
1802
1823
  consume_keyword "in"
@@ -1844,13 +1865,6 @@ class Rufo::Formatter
1844
1865
  end
1845
1866
 
1846
1867
  def visit_comma_separated_list(nodes)
1847
- # When there's *x inside a left hand side assignment
1848
- # or a case when, it comes as [:op, ...]
1849
- if nodes[0].is_a?(Symbol)
1850
- visit nodes
1851
- return
1852
- end
1853
-
1854
1868
  needs_indent = false
1855
1869
 
1856
1870
  if newline? || comment?
@@ -1862,6 +1876,7 @@ class Rufo::Formatter
1862
1876
  base_column = @column
1863
1877
  end
1864
1878
 
1879
+ nodes = to_ary(nodes)
1865
1880
  nodes.each_with_index do |exp, i|
1866
1881
  maybe_indent(needs_indent, base_column) do
1867
1882
  if block_given?
@@ -1907,11 +1922,7 @@ class Rufo::Formatter
1907
1922
  if current_token_kind == :on_op && current_token_value == "*"
1908
1923
  before, star, after = nil, before, after
1909
1924
  else
1910
- if before[0].is_a?(Symbol)
1911
- visit before
1912
- else
1913
- visit_comma_separated_list before
1914
- end
1925
+ visit_comma_separated_list to_ary(before)
1915
1926
  write_params_comma
1916
1927
  end
1917
1928
  end
@@ -1929,6 +1940,19 @@ class Rufo::Formatter
1929
1940
  end
1930
1941
  end
1931
1942
 
1943
+ def visit_rest_param(node)
1944
+ # [:rest_param, name]
1945
+
1946
+ _, name = node
1947
+
1948
+ consume_op "*"
1949
+
1950
+ if name
1951
+ skip_space_or_newline
1952
+ visit name
1953
+ end
1954
+ end
1955
+
1932
1956
  def visit_unary(node)
1933
1957
  # [:unary, :-@, [:vcall, [:@ident, "x", [1, 2]]]]
1934
1958
  _, op, exp = node
@@ -2128,7 +2152,9 @@ class Rufo::Formatter
2128
2152
  params = params[1]
2129
2153
  end
2130
2154
 
2155
+ first_space = space? ? current_token : nil
2131
2156
  skip_space
2157
+
2132
2158
  if current_token_kind == :on_lparen
2133
2159
  next_token
2134
2160
  skip_space
@@ -2142,9 +2168,17 @@ class Rufo::Formatter
2142
2168
 
2143
2169
  # () needs to be preserved if some content follows
2144
2170
  unless newline? || comment?
2171
+ if first_space && @spaces_after_method_name == :dynamic
2172
+ write_space first_space[2]
2173
+ end
2174
+
2145
2175
  write "()"
2146
2176
  end
2147
2177
  else
2178
+ if first_space && @spaces_after_method_name == :dynamic
2179
+ write_space first_space[2]
2180
+ end
2181
+
2148
2182
  write "("
2149
2183
 
2150
2184
  if newline? || comment?
@@ -2195,11 +2229,7 @@ class Rufo::Formatter
2195
2229
  skip_space_or_newline
2196
2230
 
2197
2231
  if exps
2198
- if exps[0].is_a?(Symbol)
2199
- visit exps
2200
- else
2201
- visit_exps exps, with_lines: false
2202
- end
2232
+ visit_exps to_ary(exps), with_lines: false
2203
2233
  end
2204
2234
 
2205
2235
  skip_space_or_newline
@@ -2330,13 +2360,7 @@ class Rufo::Formatter
2330
2360
  next_token
2331
2361
 
2332
2362
  if elements
2333
- if elements[0].is_a?(Symbol)
2334
- skip_space_or_newline
2335
- visit elements
2336
- skip_space_or_newline
2337
- else
2338
- visit_literal_elements elements, inside_array: true, token_column: token_column
2339
- end
2363
+ visit_literal_elements to_ary(elements), inside_array: true, token_column: token_column
2340
2364
  else
2341
2365
  skip_space_or_newline
2342
2366
  end
@@ -2678,12 +2702,7 @@ class Rufo::Formatter
2678
2702
  consume_space if space?
2679
2703
 
2680
2704
  indent(@column) do
2681
- # For `return a b` there comes many nodes, not just one... (see #8)
2682
- if node[1][0].is_a?(Symbol)
2683
- visit node[1]
2684
- else
2685
- visit_exps node[1], with_lines: false
2686
- end
2705
+ visit_exps to_ary(node[1]), with_lines: false
2687
2706
  end
2688
2707
  end
2689
2708
  end
@@ -3183,6 +3202,9 @@ class Rufo::Formatter
3183
3202
  skip_space
3184
3203
 
3185
3204
  if newline? || semicolon? || comment?
3205
+ # Cancel tracking of `else` on a nelwine.
3206
+ @case_when_positions.pop
3207
+
3186
3208
  indent_body next_exp[1]
3187
3209
  else
3188
3210
  if @spaces_around_when == :one || @align_case_when || !first_space
@@ -3245,6 +3267,10 @@ class Rufo::Formatter
3245
3267
  next_token while space?
3246
3268
  end
3247
3269
 
3270
+ def skip_ignored_space
3271
+ next_token while current_token_kind == :on_ignored_sp
3272
+ end
3273
+
3248
3274
  def skip_space_no_heredoc_check
3249
3275
  while space?
3250
3276
  next_token_no_heredoc_check
@@ -3539,15 +3565,15 @@ class Rufo::Formatter
3539
3565
  end
3540
3566
 
3541
3567
  def consume_embedded_comment
3542
- write current_token_value
3568
+ consume_token_value current_token_value
3543
3569
  next_token
3544
3570
 
3545
3571
  while current_token_kind != :on_embdoc_end
3546
- write current_token_value
3572
+ consume_token_value current_token_value
3547
3573
  next_token
3548
3574
  end
3549
3575
 
3550
- write current_token_value.rstrip
3576
+ consume_token_value current_token_value.rstrip
3551
3577
  next_token
3552
3578
  end
3553
3579
 
@@ -3821,7 +3847,7 @@ class Rufo::Formatter
3821
3847
  if last_newline_index
3822
3848
  # Remove extra indent if we are indenting inside private/protected/public
3823
3849
  # and we just found another one.
3824
- @output = "#{@output[0..last_newline_index]}#{@output[last_newline_index + 1 + @indent_size..-1]}"
3850
+ @output = "#{@output[0..last_newline_index]}#{@output[last_newline_index + 1 + @indent_size..-1]}".dup
3825
3851
  @indent -= @indent_size
3826
3852
  @visibility_indent_in_action.delete @current_type
3827
3853
  end
@@ -3946,6 +3972,10 @@ class Rufo::Formatter
3946
3972
  @current_type = old_type
3947
3973
  end
3948
3974
 
3975
+ def to_ary(node)
3976
+ node[0].is_a?(Symbol) ? [node] : node
3977
+ end
3978
+
3949
3979
  def dedent_calls
3950
3980
  return if @line_to_call_info.empty?
3951
3981
 
@@ -4094,13 +4124,13 @@ class Rufo::Formatter
4094
4124
  end
4095
4125
  end
4096
4126
 
4097
- def remove_lines_before_one_line_defs
4098
- return if @one_line_defs.empty?
4127
+ def remove_lines_before_inline_declarations
4128
+ return if @inline_declarations.empty?
4099
4129
 
4100
4130
  lines = @output.lines
4101
4131
 
4102
- @one_line_defs.reverse.each_cons(2) do |after, before|
4103
- if before + 2 == after && lines[before + 1].strip.empty?
4132
+ @inline_declarations.reverse.each_cons(2) do |(after, after_original), (before, before_original)|
4133
+ if before + 2 == after && before_original + 1 == after_original && lines[before + 1].strip.empty?
4104
4134
  lines.delete_at(before + 1)
4105
4135
  end
4106
4136
  end
data/lib/rufo/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rufo
2
- VERSION = "0.0.35"
4
+ VERSION = "0.0.36"
3
5
  end
data/lib/rufo.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Rufo
2
4
  class Bug < StandardError; end
3
5
 
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.35
4
+ version: 0.0.36
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-05 00:00:00.000000000 Z
11
+ date: 2017-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  version: '0'
101
101
  requirements: []
102
102
  rubyforge_project:
103
- rubygems_version: 2.6.11
103
+ rubygems_version: 2.6.12
104
104
  signing_key:
105
105
  specification_version: 4
106
106
  summary: Ruby code formatter