rufo 0.0.30 → 0.0.31

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d993ab0deff7da557c3e35bac8271ad6c60d43c
4
- data.tar.gz: 669c9785a3ef39403dc0b6746ad0e5c8c7196e15
3
+ metadata.gz: 208020c1cfc4ae639637febda545a3c3752980fe
4
+ data.tar.gz: 17da5ed34f7765ff9e08daa305965e41c568a6ac
5
5
  SHA512:
6
- metadata.gz: e6bcf974f8c95767a9a70a1f24acad264d6facb5040e466709a3b15fcfc11f5ecf8c0de1f8cbf02d9b34e051f0487711ad7f79639b9908a6cff99207dc4f2e19
7
- data.tar.gz: 0e2f04958a659060263e4e1dfa15748e2b70bbce178f91db46a278aeec5cc8671ed207ec3b053aee609edbc5881bba78b313bc431925826c82e7081351e621c2
6
+ metadata.gz: 696a53c1d4d30504a01c677fe87f54db384c68c8e8e38f510184c7bd2fd1bb4e9b18a04171b75318f722356bdcca93eef28e96feafb21d403336b0432d2821cc
7
+ data.tar.gz: a5783f444cb5b20a2ef8d0b8eadd4a7e62502f4782e98bf5938ac484d6abbeda167384749a47b81ca10949c548e737a967706b420f64424225f94180bc753fb6
@@ -19,7 +19,7 @@ class Rufo::Formatter
19
19
  @indent = 0
20
20
  @line = 0
21
21
  @column = 0
22
- @last_was_newline = false
22
+ @last_was_newline = true
23
23
  @output = ""
24
24
 
25
25
  # The column of a `obj.method` call, so we can align
@@ -74,6 +74,12 @@ class Rufo::Formatter
74
74
  # Position of comments that occur at the end of a line
75
75
  @comments_positions = []
76
76
 
77
+ # Token for the last comment found
78
+ @last_comment = nil
79
+
80
+ # Actual column of the last comment written
81
+ @last_comment_column = nil
82
+
77
83
  # Associate lines to alignments
78
84
  # Associate a line to an index inside @comments_position
79
85
  # becuase when aligning something to the left of a comment
@@ -205,7 +211,8 @@ class Rufo::Formatter
205
211
  def format
206
212
  visit @sexp
207
213
  consume_end
208
- write_line unless @last_was_newline
214
+ write_line if !@last_was_newline || @output == ""
215
+ @output.chomp! if @output.end_with?("\n\n")
209
216
 
210
217
  dedent_calls
211
218
  do_align_assignments if @align_assignments
@@ -275,7 +282,7 @@ class Rufo::Formatter
275
282
 
276
283
  # For heredocs with tilde we sometimes need to align the contents
277
284
  if heredoc && tilde && @last_was_newline
278
- write_indent(next_indent)
285
+ write_indent(next_indent) unless current_token_value == "\n"
279
286
  check :on_tstring_content
280
287
  consume_token_value(current_token_value)
281
288
  next_token
@@ -797,9 +804,19 @@ class Rufo::Formatter
797
804
  end
798
805
  end
799
806
 
800
- def track_comment
807
+ def current_comment_aligned_to_previous_one?
808
+ @last_comment &&
809
+ @last_comment[0][0] + 1 == current_token[0][0] &&
810
+ @last_comment[0][1] == current_token[0][1]
811
+ end
812
+
813
+ def track_comment(id: nil, match_previous_id: false)
814
+ if match_previous_id && !@comments_positions.empty?
815
+ id = @comments_positions.last[3]
816
+ end
817
+
801
818
  @line_to_alignments_positions[@line] << [:comment, @column, @comments_positions, @comments_positions.size]
802
- @comments_positions << [@line, @column, 0, nil, 0]
819
+ @comments_positions << [@line, @column, 0, id, 0]
803
820
  end
804
821
 
805
822
  def track_assignment(offset = 0)
@@ -1056,18 +1073,7 @@ class Rufo::Formatter
1056
1073
 
1057
1074
  push_call(node) do
1058
1075
  visit name
1059
-
1060
- has_backslash, first_space = skip_space_backslash
1061
- if has_backslash
1062
- write " \\"
1063
- write_line
1064
- write_indent(next_indent)
1065
- elsif first_space && @preserve_whitespace
1066
- write_space first_space[2]
1067
- skip_space_or_newline
1068
- else
1069
- consume_space
1070
- end
1076
+ consume_space_after_command_name
1071
1077
  end
1072
1078
 
1073
1079
  visit_command_end(node, args)
@@ -1128,8 +1134,7 @@ class Rufo::Formatter
1128
1134
  end
1129
1135
 
1130
1136
  visit name
1131
- consume_space
1132
-
1137
+ consume_space_after_command_name
1133
1138
  visit_command_args(args)
1134
1139
 
1135
1140
  # Only set it after we visit the call after the dot,
@@ -1137,6 +1142,20 @@ class Rufo::Formatter
1137
1142
  @dot_column = dot_column
1138
1143
  end
1139
1144
 
1145
+ def consume_space_after_command_name
1146
+ has_backslash, first_space = skip_space_backslash
1147
+ if has_backslash
1148
+ write " \\"
1149
+ write_line
1150
+ write_indent(next_indent)
1151
+ elsif first_space && @preserve_whitespace
1152
+ write_space first_space[2]
1153
+ skip_space_or_newline
1154
+ else
1155
+ consume_space
1156
+ end
1157
+ end
1158
+
1140
1159
  def visit_command_args(args)
1141
1160
  needed_indent = @column
1142
1161
 
@@ -1481,7 +1500,16 @@ class Rufo::Formatter
1481
1500
  visit args
1482
1501
  end
1483
1502
 
1484
- consume_token :on_rparen if has_paren
1503
+ if has_paren
1504
+ # Ripper has a bug where parsing `|(w, *x, y), z|`,
1505
+ # the "y" isn't returned. In this case we just consume
1506
+ # all tokens until we find a `)`.
1507
+ while current_token_kind != :on_rparen
1508
+ consume_token current_token_kind
1509
+ end
1510
+
1511
+ consume_token :on_rparen
1512
+ end
1485
1513
  end
1486
1514
 
1487
1515
  def visit_mrhs_add_star(node)
@@ -1639,13 +1667,34 @@ class Rufo::Formatter
1639
1667
 
1640
1668
  consume_op_or_keyword op
1641
1669
 
1642
- if op == :not
1643
- consume_space(want_preserve_whitespace: true)
1670
+ has_space = space?
1671
+
1672
+ if has_space
1673
+ consume_space(want_preserve_whitespace: @preserve_whitespace)
1644
1674
  else
1645
1675
  skip_space_or_newline
1646
1676
  end
1647
1677
 
1648
- visit exp
1678
+ if op == :not
1679
+ has_paren = current_token_kind == :on_lparen
1680
+
1681
+ if has_paren && !has_space
1682
+ write "("
1683
+ next_token
1684
+ skip_space_or_newline
1685
+ end
1686
+
1687
+ visit exp
1688
+
1689
+ if has_paren && !has_space
1690
+ skip_space_or_newline
1691
+ check :on_rparen
1692
+ write ")"
1693
+ next_token
1694
+ end
1695
+ else
1696
+ visit exp
1697
+ end
1649
1698
  end
1650
1699
 
1651
1700
  def visit_binary(node)
@@ -2312,7 +2361,12 @@ class Rufo::Formatter
2312
2361
  check :on_tlambda
2313
2362
  write "->"
2314
2363
  next_token
2315
- skip_space_or_newline
2364
+
2365
+ if space? && @preserve_whitespace
2366
+ consume_space(want_preserve_whitespace: true)
2367
+ else
2368
+ skip_space_or_newline
2369
+ end
2316
2370
 
2317
2371
  if empty_params?(params)
2318
2372
  if current_token_kind == :on_lparen
@@ -2386,27 +2440,25 @@ class Rufo::Formatter
2386
2440
  _, exp = node
2387
2441
 
2388
2442
  consume_keyword "defined?"
2389
- skip_space_or_newline
2443
+ has_space = space?
2444
+
2445
+ if has_space
2446
+ consume_space(want_preserve_whitespace: @preserve_whitespace)
2447
+ else
2448
+ skip_space_or_newline
2449
+ end
2390
2450
 
2391
2451
  has_paren = current_token_kind == :on_lparen
2392
2452
 
2393
- if has_paren
2453
+ if has_paren && !has_space
2394
2454
  write "("
2395
2455
  next_token
2396
2456
  skip_space_or_newline
2397
- else
2398
- consume_space
2399
- end
2400
-
2401
- # exp can be [:paren, exp] if there's a parentheses,
2402
- # though not always (only if there's a space after `defined?`)
2403
- if exp[0] == :paren
2404
- exp = exp[1]
2405
2457
  end
2406
2458
 
2407
2459
  visit exp
2408
2460
 
2409
- if has_paren
2461
+ if has_paren && !has_space
2410
2462
  skip_space_or_newline
2411
2463
  check :on_rparen
2412
2464
  write ")"
@@ -2866,8 +2918,6 @@ class Rufo::Formatter
2866
2918
  multilple_lines = false # Did we pass through more than one newline?
2867
2919
  last_comment_has_newline = false # Does the last comment has a newline?
2868
2920
  newline_count = 0 # Number of newlines we passed
2869
- last_comment = nil # Token for the last comment found
2870
- last_comment_column = nil # Actual column of the last comment written
2871
2921
 
2872
2922
  loop do
2873
2923
  case current_token_kind
@@ -2936,11 +2986,9 @@ class Rufo::Formatter
2936
2986
  # a = 1 # some comment
2937
2987
  # # that continues here
2938
2988
  # ```
2939
- if last_comment &&
2940
- last_comment[0][0] + 1 == current_token[0][0] &&
2941
- last_comment[0][1] == current_token[0][1]
2942
- write_indent(last_comment_column)
2943
- track_comment
2989
+ if current_comment_aligned_to_previous_one?
2990
+ write_indent(@last_comment_column)
2991
+ track_comment(match_previous_id: true)
2944
2992
  else
2945
2993
  write_indent
2946
2994
  end
@@ -2959,15 +3007,50 @@ class Rufo::Formatter
2959
3007
  # Write line or second line if needed
2960
3008
  write_line if last != :newline || multilple_lines
2961
3009
  write_indent
3010
+ track_comment(id: @last_was_newline ? true : nil)
2962
3011
  else
2963
3012
  # If we didn't find any newline yet, this is the first comment,
2964
3013
  # so append a space if needed (for example after an expression)
2965
3014
  write_space unless at_prefix
2966
- track_comment
3015
+
3016
+ # First we check if the comment was aligned to the previous comment
3017
+ # in the previous line, in order to keep them like that.
3018
+ if current_comment_aligned_to_previous_one?
3019
+ track_comment(match_previous_id: true)
3020
+ else
3021
+ # We want to distinguish comments that appear at the beginning
3022
+ # of a line (which means the line has only a comment) and comments
3023
+ # that appear after some expression. We don't want to align these
3024
+ # and consider them separate entities. So, we use `@last_was_newline`
3025
+ # as an id to distinguish that.
3026
+ #
3027
+ # For example, this:
3028
+ #
3029
+ # # comment 1
3030
+ # # comment 2
3031
+ # call # comment 3
3032
+ #
3033
+ # Should format to:
3034
+ #
3035
+ # # comment 1
3036
+ # # comment 2
3037
+ # call # comment 3
3038
+ #
3039
+ # Instead of:
3040
+ #
3041
+ # # comment 1
3042
+ # # comment 2
3043
+ # call # comment 3
3044
+ #
3045
+ # We still want to track the first two comments to align to the
3046
+ # beginning of the line according to indentation in case they
3047
+ # are not already there.
3048
+ track_comment(id: @last_was_newline ? true : nil)
3049
+ end
2967
3050
  end
2968
3051
  end
2969
- last_comment = current_token
2970
- last_comment_column = @column
3052
+ @last_comment = current_token
3053
+ @last_comment_column = @column
2971
3054
  last_comment_has_newline = current_token_value.end_with?("\n")
2972
3055
  last = :comment
2973
3056
  multilple_lines = false
@@ -3140,7 +3223,6 @@ class Rufo::Formatter
3140
3223
  def write_indent(indent = @indent)
3141
3224
  @output << " " * indent
3142
3225
  @column += indent
3143
- @last_was_newline = false
3144
3226
  end
3145
3227
 
3146
3228
  def indent_after_space(node, sticky: false, want_space: true, first_space: nil, needed_indent: next_indent)
data/lib/rufo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rufo
2
- VERSION = "0.0.30"
2
+ VERSION = "0.0.31"
3
3
  end
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.30
4
+ version: 0.0.31
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-29 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler