rufo 0.0.36 → 0.0.37

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.
@@ -0,0 +1,198 @@
1
+ class Rufo::Formatter
2
+ def init_settings(options)
3
+ indent_size options.fetch(:indent_size, 2)
4
+ spaces_inside_hash_brace options.fetch(:spaces_inside_hash_brace, :dynamic)
5
+ spaces_inside_array_bracket options.fetch(:spaces_inside_array_bracket, :dynamic)
6
+ spaces_around_equal options.fetch(:spaces_around_equal, :dynamic)
7
+ spaces_in_ternary options.fetch(:spaces_in_ternary, :dynamic)
8
+ spaces_in_suffix options.fetch(:spaces_in_suffix, :dynamic)
9
+ spaces_in_commands options.fetch(:spaces_in_commands, :dynamic)
10
+ spaces_around_block_brace options.fetch(:spaces_around_block_brace, :dynamic)
11
+ spaces_after_comma options.fetch(:spaces_after_comma, :dynamic)
12
+ spaces_around_hash_arrow options.fetch(:spaces_around_hash_arrow, :dynamic)
13
+ spaces_around_when options.fetch(:spaces_around_when, :dynamic)
14
+ spaces_around_dot options.fetch(:spaces_around_dot, :dynamic)
15
+ spaces_after_lambda_arrow options.fetch(:spaces_after_lambda_arrow, :dynamic)
16
+ spaces_around_unary options.fetch(:spaces_around_unary, :dynamic)
17
+ spaces_around_binary options.fetch(:spaces_around_binary, :dynamic)
18
+ spaces_after_method_name options.fetch(:spaces_after_method_name, :dynamic)
19
+ spaces_in_inline_expressions options.fetch(:spaces_in_inline_expressions, :dynamic)
20
+ parens_in_def options.fetch(:parens_in_def, :dynamic)
21
+ double_newline_inside_type options.fetch(:double_newline_inside_type, :dynamic)
22
+ visibility_indent options.fetch(:visibility_indent, :dynamic)
23
+ align_comments options.fetch(:align_comments, false)
24
+ align_assignments options.fetch(:align_assignments, false)
25
+ align_hash_keys options.fetch(:align_hash_keys, false)
26
+ align_case_when options.fetch(:align_case_when, false)
27
+ align_chained_calls options.fetch(:align_chained_calls, false)
28
+ trailing_commas options.fetch(:trailing_commas, :dynamic)
29
+ end
30
+
31
+ def indent_size(value)
32
+ @indent_size = value
33
+ end
34
+
35
+ def spaces_inside_hash_brace(value)
36
+ @spaces_inside_hash_brace = dynamic_always_never_match("spaces_inside_hash_brace", value)
37
+ end
38
+
39
+ def spaces_inside_array_bracket(value)
40
+ @spaces_inside_array_bracket = dynamic_always_never_match("spaces_inside_array_bracket", value)
41
+ end
42
+
43
+ def spaces_around_equal(value)
44
+ @spaces_around_equal = one_dynamic("spaces_around_equal", value)
45
+ end
46
+
47
+ def spaces_in_ternary(value)
48
+ @spaces_in_ternary = one_dynamic("spaces_in_ternary", value)
49
+ end
50
+
51
+ def spaces_in_suffix(value)
52
+ @spaces_in_suffix = one_dynamic("spaces_in_suffix", value)
53
+ end
54
+
55
+ def spaces_in_commands(value)
56
+ @spaces_in_commands = one_dynamic("spaces_in_commands", value)
57
+ end
58
+
59
+ def spaces_around_block_brace(value)
60
+ @spaces_around_block_brace = one_dynamic("spaces_around_block_brace", value)
61
+ end
62
+
63
+ def spaces_after_comma(value)
64
+ @spaces_after_comma = one_dynamic("spaces_after_comma", value)
65
+ end
66
+
67
+ def spaces_around_hash_arrow(value)
68
+ @spaces_around_hash_arrow = one_dynamic("spaces_around_hash_arrow", value)
69
+ end
70
+
71
+ def spaces_around_when(value)
72
+ @spaces_around_when = one_dynamic("spaces_around_when", value)
73
+ end
74
+
75
+ def spaces_around_dot(value)
76
+ @spaces_around_dot = no_dynamic("spaces_around_dot", value)
77
+ end
78
+
79
+ def spaces_after_lambda_arrow(value)
80
+ @spaces_after_lambda_arrow = one_no_dynamic("spaces_after_lambda_arrow", value)
81
+ end
82
+
83
+ def spaces_around_unary(value)
84
+ @spaces_around_unary = no_dynamic("spaces_around_unary", value)
85
+ end
86
+
87
+ def spaces_around_binary(value)
88
+ @spaces_around_binary = one_dynamic("spaces_around_binary", value)
89
+ end
90
+
91
+ def spaces_after_method_name(value)
92
+ @spaces_after_method_name = no_dynamic("spaces_after_method_name", value)
93
+ end
94
+
95
+ def spaces_in_inline_expressions(value)
96
+ @spaces_in_inline_expressions = one_dynamic("spaces_in_inline_expressions", value)
97
+ end
98
+
99
+ def parens_in_def(value)
100
+ @parens_in_def = yes_dynamic("parens_in_def", value)
101
+ end
102
+
103
+ def double_newline_inside_type(value)
104
+ @double_newline_inside_type = no_dynamic("double_newline_inside_type", value)
105
+ end
106
+
107
+ def trailing_commas(value)
108
+ case value
109
+ when :dynamic, :always, :never
110
+ @trailing_commas = value
111
+ else
112
+ raise ArgumentError.new("invalid value for trailing_commas: #{value}. Valid values are: :dynamic, :always, :never")
113
+ end
114
+ end
115
+
116
+ def visibility_indent(value)
117
+ case value
118
+ when :indent, :align, :dynamic #, :dedent
119
+ @visibility_indent = value
120
+ else
121
+ raise ArgumentError.new("invalid value for visibility_indent: #{value}. Valid values are: :indent, :align, :dynamic")
122
+ end
123
+ end
124
+
125
+ def align_comments(value)
126
+ @align_comments = value
127
+ end
128
+
129
+ def align_assignments(value)
130
+ @align_assignments = value
131
+ end
132
+
133
+ def align_hash_keys(value)
134
+ @align_hash_keys = value
135
+ end
136
+
137
+ def align_case_when(value)
138
+ @align_case_when = value
139
+ end
140
+
141
+ def align_chained_calls(value)
142
+ @align_chained_calls = value
143
+ end
144
+
145
+ def dynamic_always_never(name, value)
146
+ case value
147
+ when :dynamic, :always, :never
148
+ value
149
+ else
150
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :dynamic, :always, :never")
151
+ end
152
+ end
153
+
154
+ def dynamic_always_never_match(name, value)
155
+ case value
156
+ when :dynamic, :always, :never, :match
157
+ value
158
+ else
159
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :dynamic, :always, :never, :match")
160
+ end
161
+ end
162
+
163
+ def one_dynamic(name, value)
164
+ case value
165
+ when :one, :dynamic
166
+ value
167
+ else
168
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :one, :dynamic")
169
+ end
170
+ end
171
+
172
+ def no_dynamic(name, value)
173
+ case value
174
+ when :no, :dynamic
175
+ value
176
+ else
177
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :no, :dynamic")
178
+ end
179
+ end
180
+
181
+ def one_no_dynamic(name, value)
182
+ case value
183
+ when :no, :one, :dynamic
184
+ value
185
+ else
186
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :no, :one, :dynamic")
187
+ end
188
+ end
189
+
190
+ def yes_dynamic(name, value)
191
+ case value
192
+ when :yes, :dynamic
193
+ value
194
+ else
195
+ raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :yes, :dynamic")
196
+ end
197
+ end
198
+ end
@@ -156,198 +156,9 @@ class Rufo::Formatter
156
156
  # This is [[line, original_line], ...]
157
157
  @inline_declarations = []
158
158
 
159
- # Settings
160
- indent_size options.fetch(:indent_size, 2)
161
- spaces_inside_hash_brace options.fetch(:spaces_inside_hash_brace, :dynamic)
162
- spaces_inside_array_bracket options.fetch(:spaces_inside_array_bracket, :dynamic)
163
- spaces_around_equal options.fetch(:spaces_around_equal, :dynamic)
164
- spaces_in_ternary options.fetch(:spaces_in_ternary, :dynamic)
165
- spaces_in_suffix options.fetch(:spaces_in_suffix, :dynamic)
166
- spaces_in_commands options.fetch(:spaces_in_commands, :dynamic)
167
- spaces_around_block_brace options.fetch(:spaces_around_block_brace, :dynamic)
168
- spaces_after_comma options.fetch(:spaces_after_comma, :dynamic)
169
- spaces_around_hash_arrow options.fetch(:spaces_around_hash_arrow, :dynamic)
170
- spaces_around_when options.fetch(:spaces_around_when, :dynamic)
171
- spaces_around_dot options.fetch(:spaces_around_dot, :dynamic)
172
- spaces_after_lambda_arrow options.fetch(:spaces_after_lambda_arrow, :dynamic)
173
- spaces_around_unary options.fetch(:spaces_around_unary, :dynamic)
174
- spaces_around_binary options.fetch(:spaces_around_binary, :dynamic)
175
- spaces_after_method_name options.fetch(:spaces_after_method_name, :dynamic)
176
- parens_in_def options.fetch(:parens_in_def, :dynamic)
177
- double_newline_inside_type options.fetch(:double_newline_inside_type, :dynamic)
178
- visibility_indent options.fetch(:visibility_indent, :dynamic)
179
- align_comments options.fetch(:align_comments, false)
180
- align_assignments options.fetch(:align_assignments, false)
181
- align_hash_keys options.fetch(:align_hash_keys, false)
182
- align_case_when options.fetch(:align_case_when, false)
183
- align_chained_calls options.fetch(:align_chained_calls, false)
184
- trailing_commas options.fetch(:trailing_commas, :dynamic)
159
+ init_settings(options)
185
160
  end
186
161
 
187
- ### Settings
188
-
189
- def indent_size(value)
190
- @indent_size = value
191
- end
192
-
193
- def spaces_inside_hash_brace(value)
194
- @spaces_inside_hash_brace = dynamic_always_never_match("spaces_inside_hash_brace", value)
195
- end
196
-
197
- def spaces_inside_array_bracket(value)
198
- @spaces_inside_array_bracket = dynamic_always_never_match("spaces_inside_array_bracket", value)
199
- end
200
-
201
- def spaces_around_equal(value)
202
- @spaces_around_equal = one_dynamic("spaces_around_equal", value)
203
- end
204
-
205
- def spaces_in_ternary(value)
206
- @spaces_in_ternary = one_dynamic("spaces_in_ternary", value)
207
- end
208
-
209
- def spaces_in_suffix(value)
210
- @spaces_in_suffix = one_dynamic("spaces_in_suffix", value)
211
- end
212
-
213
- def spaces_in_commands(value)
214
- @spaces_in_commands = one_dynamic("spaces_in_commands", value)
215
- end
216
-
217
- def spaces_around_block_brace(value)
218
- @spaces_around_block_brace = one_dynamic("spaces_around_block_brace", value)
219
- end
220
-
221
- def spaces_after_comma(value)
222
- @spaces_after_comma = one_dynamic("spaces_after_comma", value)
223
- end
224
-
225
- def spaces_around_hash_arrow(value)
226
- @spaces_around_hash_arrow = one_dynamic("spaces_around_hash_arrow", value)
227
- end
228
-
229
- def spaces_around_when(value)
230
- @spaces_around_when = one_dynamic("spaces_around_when", value)
231
- end
232
-
233
- def spaces_around_dot(value)
234
- @spaces_around_dot = no_dynamic("spaces_around_dot", value)
235
- end
236
-
237
- def spaces_after_lambda_arrow(value)
238
- @spaces_after_lambda_arrow = no_dynamic("spaces_after_lambda_arrow", value)
239
- end
240
-
241
- def spaces_around_unary(value)
242
- @spaces_around_unary = no_dynamic("spaces_around_unary", value)
243
- end
244
-
245
- def spaces_around_binary(value)
246
- @spaces_around_binary = one_dynamic("spaces_around_binary", value)
247
- end
248
-
249
- def spaces_after_method_name(value)
250
- @spaces_after_method_name = no_dynamic("spaces_after_method_name", value)
251
- end
252
-
253
- def parens_in_def(value)
254
- @parens_in_def = yes_dynamic("parens_in_def", value)
255
- end
256
-
257
- def double_newline_inside_type(value)
258
- @double_newline_inside_type = no_dynamic("double_newline_inside_type", value)
259
- end
260
-
261
- def visibility_indent(value)
262
- case value
263
- when :indent, :align, :dynamic #, :dedent
264
- @visibility_indent = value
265
- else
266
- raise ArgumentError.new("invalid value for visibility_indent: #{value}. Valid values are: :indent, :align, :dynamic")
267
- end
268
- end
269
-
270
- def dynamic_always_never(name, value)
271
- case value
272
- when :dynamic, :always, :never
273
- value
274
- else
275
- raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :dynamic, :always, :never")
276
- end
277
- end
278
-
279
- def dynamic_always_never_match(name, value)
280
- case value
281
- when :dynamic, :always, :never, :match
282
- value
283
- else
284
- raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :dynamic, :always, :never, :match")
285
- end
286
- end
287
-
288
- def one_dynamic(name, value)
289
- case value
290
- when :one, :dynamic
291
- value
292
- else
293
- raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :one, :dynamic")
294
- end
295
- end
296
-
297
- def no_dynamic(name, value)
298
- case value
299
- when :no, :dynamic
300
- value
301
- else
302
- raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :no, :dynamic")
303
- end
304
- end
305
-
306
- def yes_dynamic(name, value)
307
- case value
308
- when :yes, :dynamic
309
- value
310
- else
311
- raise ArgumentError.new("invalid value for #{name}: #{value}. Valid values are: :yes, :dynamic")
312
- end
313
- end
314
-
315
- def align_comments(value)
316
- @align_comments = value
317
- end
318
-
319
- def align_assignments(value)
320
- @align_assignments = value
321
- end
322
-
323
- def align_hash_keys(value)
324
- @align_hash_keys = value
325
- end
326
-
327
- def align_case_when(value)
328
- @align_case_when = value
329
- end
330
-
331
- def align_chained_calls(value)
332
- @align_chained_calls = value
333
- end
334
-
335
- # Whether to place commas at the end of a multi-line list
336
- #
337
- # * :dynamic: if there's a comma, keep it. If not, don't add it (default)
338
- # * :always: always put a comma
339
- # * :never: never put a comma
340
- def trailing_commas(value)
341
- case value
342
- when :dynamic, :always, :never
343
- @trailing_commas = value
344
- else
345
- raise ArgumentError.new("invalid value for trailing_commas: #{value}. Valid values are: :dynamic, :always, :never")
346
- end
347
- end
348
-
349
- ### Formatter implementation
350
-
351
162
  def format
352
163
  visit @sexp
353
164
  consume_end
@@ -953,20 +764,18 @@ class Rufo::Formatter
953
764
 
954
765
  visit_comma_separated_list lefts
955
766
 
956
- first_space = current_token if space?
957
- skip_space
767
+ first_space = skip_space
958
768
 
959
769
  # A trailing comma can come after the left hand side
960
770
  if comma?
961
771
  consume_token :on_comma
962
- first_space = current_token if space?
963
- skip_space
772
+ first_space = skip_space
964
773
  end
965
774
 
966
- if @spaces_around_equal == :one || @align_assignments
775
+ if @align_assignments
967
776
  write_space
968
- elsif first_space
969
- write_space first_space[2]
777
+ else
778
+ write_space_using_setting(first_space, @spaces_around_equal)
970
779
  end
971
780
 
972
781
  track_assignment
@@ -977,8 +786,7 @@ class Rufo::Formatter
977
786
  def visit_assign_value(value)
978
787
  base_column = @column
979
788
 
980
- first_space = current_token if space?
981
- has_slash_newline, _ = skip_space_backslash
789
+ has_slash_newline, first_space = skip_space_backslash
982
790
 
983
791
  sticky = indentable_value?(value)
984
792
 
@@ -1104,8 +912,7 @@ class Rufo::Formatter
1104
912
  @dot_column = nil
1105
913
  visit obj
1106
914
 
1107
- first_space = current_token if space? && @spaces_around_dot == :dynamic
1108
- skip_space
915
+ first_space = skip_space
1109
916
 
1110
917
  if newline? || comment?
1111
918
  consume_end_of_line
@@ -1120,8 +927,8 @@ class Rufo::Formatter
1120
927
  @name_dot_column = next_indent
1121
928
  write_indent(next_indent)
1122
929
  end
1123
- elsif first_space
1124
- write_space first_space[2]
930
+ else
931
+ write_space_using_setting(first_space, @spaces_around_dot)
1125
932
  end
1126
933
 
1127
934
  # Remember dot column, but only if there isn't one already set
@@ -1132,18 +939,13 @@ class Rufo::Formatter
1132
939
 
1133
940
  consume_call_dot
1134
941
 
1135
- first_space = nil
1136
- first_space = current_token if space? && @spaces_around_dot == :dynamic
1137
- skip_space
942
+ first_space = skip_space
1138
943
 
1139
944
  if newline? || comment?
1140
945
  consume_end_of_line
1141
946
  write_indent(next_indent)
1142
947
  else
1143
- skip_space_or_newline
1144
- if first_space
1145
- write_space first_space[2]
1146
- end
948
+ write_space_using_setting(first_space, @spaces_around_dot)
1147
949
  end
1148
950
 
1149
951
  if name == :call
@@ -1337,8 +1139,6 @@ class Rufo::Formatter
1337
1139
 
1338
1140
  visit receiver
1339
1141
 
1340
- first_space = current_token if space? && @spaces_around_dot == :dynamic
1341
-
1342
1142
  line = @line
1343
1143
  skip_space_or_newline
1344
1144
 
@@ -1373,11 +1173,8 @@ class Rufo::Formatter
1373
1173
  write " \\"
1374
1174
  write_line
1375
1175
  write_indent(next_indent)
1376
- elsif first_space && @spaces_in_commands == :dynamic
1377
- write_space first_space[2]
1378
- skip_space_or_newline
1379
1176
  else
1380
- consume_space if @spaces_in_commands == :one
1177
+ write_space_using_setting(first_space, @spaces_in_commands)
1381
1178
  end
1382
1179
  end
1383
1180
 
@@ -1465,7 +1262,13 @@ class Rufo::Formatter
1465
1262
  consume_space
1466
1263
  end
1467
1264
 
1265
+ old_dot_column = @dot_column
1266
+ old_original_dot_column = @original_dot_column
1267
+
1468
1268
  visit block
1269
+
1270
+ @dot_column = old_dot_column
1271
+ @original_dot_column = old_original_dot_column
1469
1272
  end
1470
1273
 
1471
1274
  def visit_brace_block(node)
@@ -1893,20 +1696,15 @@ class Rufo::Formatter
1893
1696
  write ","
1894
1697
  next_token
1895
1698
 
1896
- first_space = current_token if space?
1897
- skip_space
1699
+ first_space = skip_space
1898
1700
 
1899
1701
  if newline? || comment?
1900
1702
  indent(base_column || @indent) do
1901
1703
  consume_end_of_line(want_multiline: false, first_space: first_space)
1902
1704
  write_indent
1903
1705
  end
1904
- elsif first_space && @spaces_after_comma == :dynamic
1905
- write_space first_space[2]
1906
- skip_space_or_newline
1907
1706
  else
1908
- write_space if @spaces_after_comma == :one
1909
- skip_space_or_newline
1707
+ write_space_using_setting(first_space, @spaces_after_comma)
1910
1708
  end
1911
1709
  end
1912
1710
  end
@@ -2031,9 +1829,7 @@ class Rufo::Formatter
2031
1829
 
2032
1830
  consume_op_or_keyword op
2033
1831
 
2034
- first_space = nil
2035
- first_space = current_token if space?
2036
- skip_space
1832
+ first_space = skip_space
2037
1833
 
2038
1834
  if newline? || comment?
2039
1835
  indent_after_space right,
@@ -2148,12 +1944,9 @@ class Rufo::Formatter
2148
1944
  def visit_def_from_name(name, params, body)
2149
1945
  visit name
2150
1946
 
2151
- if params[0] == :paren
2152
- params = params[1]
2153
- end
1947
+ params = params[1] if params[0] == :paren
2154
1948
 
2155
- first_space = space? ? current_token : nil
2156
- skip_space
1949
+ first_space = skip_space
2157
1950
 
2158
1951
  if current_token_kind == :on_lparen
2159
1952
  next_token
@@ -2164,20 +1957,10 @@ class Rufo::Formatter
2164
1957
  skip_space_or_newline
2165
1958
  check :on_rparen
2166
1959
  next_token
2167
- skip_space
2168
-
2169
- # () needs to be preserved if some content follows
2170
- unless newline? || comment?
2171
- if first_space && @spaces_after_method_name == :dynamic
2172
- write_space first_space[2]
2173
- end
2174
-
2175
- write "()"
2176
- end
1960
+ write_space_using_setting(first_space, @spaces_after_method_name)
1961
+ write "()"
2177
1962
  else
2178
- if first_space && @spaces_after_method_name == :dynamic
2179
- write_space first_space[2]
2180
- end
1963
+ write_space_using_setting(first_space, @spaces_after_method_name)
2181
1964
 
2182
1965
  write "("
2183
1966
 
@@ -2326,18 +2109,13 @@ class Rufo::Formatter
2326
2109
  write ","
2327
2110
  next_token
2328
2111
 
2329
- first_space = current_token if space?
2330
- skip_space
2112
+ first_space = skip_space
2331
2113
 
2332
2114
  if newline? || comment?
2333
2115
  consume_end_of_line
2334
2116
  write_indent
2335
- elsif first_space && @spaces_after_comma == :dynamic
2336
- write_space first_space[2]
2337
- skip_space_or_newline
2338
2117
  else
2339
- write_space if @spaces_after_comma == :one
2340
- skip_space_or_newline
2118
+ write_space_using_setting(first_space, @spaces_after_comma)
2341
2119
  end
2342
2120
  end
2343
2121
 
@@ -2561,8 +2339,7 @@ class Rufo::Formatter
2561
2339
 
2562
2340
  column = @column
2563
2341
 
2564
- first_space = space? ? current_token : nil
2565
- skip_space
2342
+ first_space = skip_space
2566
2343
 
2567
2344
  # Sometimes args comes with an array...
2568
2345
  if args && args[0].is_a?(Array)
@@ -2577,9 +2354,7 @@ class Rufo::Formatter
2577
2354
  skip_space_or_newline
2578
2355
  end
2579
2356
  else
2580
- if first_space && @spaces_inside_array_bracket == :dynamic
2581
- write first_space[2]
2582
- end
2357
+ write_space_using_setting(first_space, @spaces_inside_array_bracket)
2583
2358
  needed_indent = column
2584
2359
  end
2585
2360
 
@@ -2590,12 +2365,11 @@ class Rufo::Formatter
2590
2365
  end
2591
2366
  end
2592
2367
 
2593
- first_space = space? ? current_token : nil
2594
- skip_space
2368
+ first_space = skip_space
2595
2369
  if newline? || comment?
2596
2370
  skip_space_or_newline
2597
- elsif first_space && @spaces_inside_array_bracket == :dynamic
2598
- write first_space[2]
2371
+ else
2372
+ write_space_using_setting(first_space, @spaces_inside_array_bracket)
2599
2373
  end
2600
2374
 
2601
2375
  check :on_rbracket
@@ -2633,16 +2407,14 @@ class Rufo::Formatter
2633
2407
 
2634
2408
  visit receiver
2635
2409
 
2636
- first_space = current_token if space? && @spaces_around_dot == :dynamic
2637
-
2638
- skip_space
2410
+ first_space = skip_space
2639
2411
 
2640
2412
  if newline? || comment?
2641
2413
  consume_end_of_line
2642
2414
 
2643
2415
  write_indent(@dot_column || next_indent)
2644
- elsif first_space
2645
- write_space first_space[2]
2416
+ else
2417
+ write_space_using_setting(first_space, @spaces_around_dot)
2646
2418
  end
2647
2419
 
2648
2420
  # Remember dot column
@@ -2651,18 +2423,14 @@ class Rufo::Formatter
2651
2423
 
2652
2424
  consume_call_dot
2653
2425
 
2654
- first_space = nil
2655
- first_space = current_token if space? && @spaces_around_dot == :dynamic
2656
- skip_space
2426
+ first_space = skip_space
2657
2427
 
2658
2428
  if newline? || comment?
2659
2429
  consume_end_of_line
2660
2430
  write_indent(next_indent)
2661
2431
  else
2662
2432
  skip_space_or_newline
2663
- if first_space
2664
- write_space first_space[2]
2665
- end
2433
+ write_space_using_setting(first_space, @spaces_around_dot)
2666
2434
  end
2667
2435
 
2668
2436
  visit name
@@ -2717,6 +2485,9 @@ class Rufo::Formatter
2717
2485
 
2718
2486
  if space? && @spaces_after_lambda_arrow == :dynamic
2719
2487
  consume_space(want_preserve_whitespace: true)
2488
+ elsif @spaces_after_lambda_arrow == :one
2489
+ skip_space
2490
+ write_space
2720
2491
  else
2721
2492
  skip_space_or_newline
2722
2493
  end
@@ -2844,9 +2615,8 @@ class Rufo::Formatter
2844
2615
  def visit_literal_elements(elements, inside_hash: false, inside_array: false, token_column:)
2845
2616
  base_column = @column
2846
2617
  base_line = @line
2847
- first_space = current_token if space?
2848
2618
  needs_final_space = (inside_hash || inside_array) && space?
2849
- skip_space
2619
+ first_space = skip_space
2850
2620
 
2851
2621
  if inside_hash
2852
2622
  case @spaces_inside_hash_brace
@@ -2910,11 +2680,9 @@ class Rufo::Formatter
2910
2680
  visit elem
2911
2681
  end
2912
2682
 
2913
- first_space = space? ? current_token : nil
2914
-
2915
2683
  # We have to be careful not to aumatically write a heredoc on next_token,
2916
2684
  # because we miss the chance to write a comma to separate elements
2917
- skip_space_no_heredoc_check
2685
+ first_space = skip_space_no_heredoc_check
2918
2686
  wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
2919
2687
 
2920
2688
  if is_last && !comma? && !wrote_comma && !needs_trailing_comma && !comment?
@@ -2941,9 +2709,7 @@ class Rufo::Formatter
2941
2709
  # because we miss the chance to write a comma to separate elements
2942
2710
  next_token_no_heredoc_check
2943
2711
 
2944
- first_space = space? ? current_token : nil
2945
-
2946
- skip_space_no_heredoc_check
2712
+ first_space = skip_space_no_heredoc_check
2947
2713
  wrote_comma = check_heredocs_in_literal_elements(is_last, needs_trailing_comma, wrote_comma)
2948
2714
 
2949
2715
  if newline? || comment?
@@ -3078,8 +2844,6 @@ class Rufo::Formatter
3078
2844
 
3079
2845
  visit cond
3080
2846
 
3081
- skip_space
3082
-
3083
2847
  indent_body body
3084
2848
 
3085
2849
  write_indent if @line != line
@@ -3197,9 +2961,8 @@ class Rufo::Formatter
3197
2961
  if next_exp[0] == :else
3198
2962
  # [:else, body]
3199
2963
  consume_keyword "else"
3200
- first_space = current_token if space?
3201
2964
  track_case_when
3202
- skip_space
2965
+ first_space = skip_space
3203
2966
 
3204
2967
  if newline? || semicolon? || comment?
3205
2968
  # Cancel tracking of `else` on a nelwine.
@@ -3207,10 +2970,10 @@ class Rufo::Formatter
3207
2970
 
3208
2971
  indent_body next_exp[1]
3209
2972
  else
3210
- if @spaces_around_when == :one || @align_case_when || !first_space
2973
+ if @align_case_when
3211
2974
  write_space
3212
2975
  else
3213
- write_space first_space[2]
2976
+ write_space_using_setting(first_space, @spaces_around_when)
3214
2977
  end
3215
2978
  visit_exps next_exp[1]
3216
2979
  end
@@ -3221,8 +2984,7 @@ class Rufo::Formatter
3221
2984
  end
3222
2985
 
3223
2986
  def consume_space(want_preserve_whitespace: false)
3224
- first_space = current_token if space?
3225
- skip_space
2987
+ first_space = skip_space
3226
2988
  if want_preserve_whitespace && !newline? && !comment? && first_space
3227
2989
  write_space first_space[2] unless @output[-1] == " "
3228
2990
  skip_space_or_newline
@@ -3251,8 +3013,7 @@ class Rufo::Formatter
3251
3013
  end
3252
3014
 
3253
3015
  def consume_one_dynamic_space_or_newline(setting)
3254
- first_space = current_token if space?
3255
- skip_space
3016
+ first_space = skip_space
3256
3017
  if newline? || comment?
3257
3018
  consume_end_of_line
3258
3019
  write_indent(next_indent)
@@ -3264,7 +3025,9 @@ class Rufo::Formatter
3264
3025
  end
3265
3026
 
3266
3027
  def skip_space
3028
+ first_space = space? ? current_token : nil
3267
3029
  next_token while space?
3030
+ first_space
3268
3031
  end
3269
3032
 
3270
3033
  def skip_ignored_space
@@ -3272,9 +3035,11 @@ class Rufo::Formatter
3272
3035
  end
3273
3036
 
3274
3037
  def skip_space_no_heredoc_check
3038
+ first_space = space? ? current_token : nil
3275
3039
  while space?
3276
3040
  next_token_no_heredoc_check
3277
3041
  end
3042
+ first_space
3278
3043
  end
3279
3044
 
3280
3045
  def skip_space_backslash
@@ -3606,13 +3371,14 @@ class Rufo::Formatter
3606
3371
  end
3607
3372
 
3608
3373
  def indent_body(exps, force_multiline: false, want_multiline: false)
3609
- skip_space
3374
+ first_space = skip_space
3610
3375
 
3611
3376
  has_semicolon = semicolon?
3612
3377
 
3613
3378
  if has_semicolon
3614
3379
  next_token
3615
3380
  skip_semicolons
3381
+ first_space = nil
3616
3382
  end
3617
3383
 
3618
3384
  # If an end follows there's nothing to do
@@ -3620,7 +3386,7 @@ class Rufo::Formatter
3620
3386
  if has_semicolon
3621
3387
  write "; "
3622
3388
  else
3623
- write " "
3389
+ write_space_using_setting(first_space, @spaces_in_inline_expressions)
3624
3390
  end
3625
3391
  return
3626
3392
  end
@@ -3631,13 +3397,13 @@ class Rufo::Formatter
3631
3397
  has_then = keyword?("then")
3632
3398
  if has_then
3633
3399
  next_token
3634
- skip_space
3400
+ second_space = skip_space
3635
3401
  end
3636
3402
 
3637
3403
  has_do = keyword?("do")
3638
3404
  if has_do
3639
3405
  next_token
3640
- skip_space
3406
+ second_space = skip_space
3641
3407
  end
3642
3408
 
3643
3409
  # If no newline or comment follows, we format it inline.
@@ -3645,14 +3411,23 @@ class Rufo::Formatter
3645
3411
  if has_then
3646
3412
  write " then "
3647
3413
  elsif has_do
3648
- write " do "
3414
+ write_space_using_setting(first_space, @spaces_in_inline_expressions, at_least_one: true)
3415
+ write "do"
3416
+ write_space_using_setting(second_space, @spaces_in_inline_expressions, at_least_one: true)
3649
3417
  elsif has_semicolon
3650
3418
  write "; "
3651
3419
  else
3652
- write " "
3420
+ write_space_using_setting(first_space, @spaces_in_inline_expressions, at_least_one: true)
3653
3421
  end
3654
3422
  visit_exps exps, with_indent: false, with_lines: false
3655
- consume_space
3423
+
3424
+ first_space = skip_space
3425
+ if first_space && @spaces_in_inline_expressions == :dynamic &&
3426
+ !(semicolon? || newline? || comment?)
3427
+ write_space first_space[2]
3428
+ else
3429
+ consume_space
3430
+ end
3656
3431
  return
3657
3432
  end
3658
3433
 
@@ -3698,6 +3473,14 @@ class Rufo::Formatter
3698
3473
  @column += value.size
3699
3474
  end
3700
3475
 
3476
+ def write_space_using_setting(first_space, setting, at_least_one: false)
3477
+ if first_space && setting == :dynamic
3478
+ write_space first_space[2]
3479
+ elsif setting == :one || at_least_one
3480
+ write_space
3481
+ end
3482
+ end
3483
+
3701
3484
  def write_line
3702
3485
  @output << "\n"
3703
3486
  @last_was_newline = true
@@ -3712,8 +3495,8 @@ class Rufo::Formatter
3712
3495
 
3713
3496
  def indent_after_space(node, sticky: false, want_space: true, first_space: nil, needed_indent: next_indent, token_column: nil, base_column: nil, preserve_whitespace:)
3714
3497
  first_space = current_token if space?
3715
-
3716
3498
  skip_space
3499
+
3717
3500
  case current_token_kind
3718
3501
  when :on_ignored_nl, :on_comment
3719
3502
  indent(needed_indent) do