rufo 0.0.18 → 0.0.19
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 +4 -4
- data/.rufo +5 -4
- data/README.md +12 -5
- data/lib/rufo/backport.rb +2 -2
- data/lib/rufo/command.rb +31 -30
- data/lib/rufo/dot_file.rb +24 -0
- data/lib/rufo/formatter.rb +124 -77
- data/lib/rufo/version.rb +1 -1
- data/lib/rufo.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37581d331f99c0a339a02883d5eee479441a51c7
|
4
|
+
data.tar.gz: 5f65749a69f3d7542630b8509bb75b8925be48d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 987ae28a14a0c618bed6d037607d698931b71ce3b2757f59078ec68c58b0f239d66077931d9b53a65aa94c75309961fe5834f6dde425889a24165d55b47fb79f
|
7
|
+
data.tar.gz: 7070cc3d2d44f9fc55d1029458cf7145d36b7bf97c438ac2cda077f8c670521d5516d3b817e8cc32d483e3799766650327223c7431eca65a3d96a4dd8b3e9608
|
data/.rufo
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
space_after_hash_brace :dynamic
|
2
|
+
align_comments true
|
3
|
+
align_assignments false
|
4
|
+
align_hash_keys true
|
5
|
+
indent_size 2
|
data/README.md
CHANGED
@@ -58,17 +58,24 @@ The `.rufo` file is a Ruby file that is evaluated in the context of the formatte
|
|
58
58
|
available configurations:
|
59
59
|
|
60
60
|
```ruby
|
61
|
+
# Whether to put a space after a hash brace. Valid values are:
|
62
|
+
#
|
63
|
+
# * :dynamic: if there's a space, keep it. If not, don't keep it (default)
|
64
|
+
# * :always: always put a space after a hash brace
|
65
|
+
# * :never: never put a space after a hash brace
|
66
|
+
space_after_hash_brace :dynamic
|
67
|
+
|
61
68
|
# Whether to align successive comments (default: true)
|
62
|
-
align_comments
|
69
|
+
align_comments true
|
63
70
|
|
64
|
-
# Whether to align successive assignments (default:
|
65
|
-
align_assignments
|
71
|
+
# Whether to align successive assignments (default: false)
|
72
|
+
align_assignments false
|
66
73
|
|
67
74
|
# Whether to align successive hash keys (default: true)
|
68
|
-
align_hash_keys
|
75
|
+
align_hash_keys true
|
69
76
|
|
70
77
|
# The indent size (default: 2)
|
71
|
-
indent_size
|
78
|
+
indent_size 2
|
72
79
|
```
|
73
80
|
|
74
81
|
As time passes there might be more configurations available. Please open an
|
data/lib/rufo/backport.rb
CHANGED
data/lib/rufo/command.rb
CHANGED
@@ -1,21 +1,30 @@
|
|
1
1
|
require "optionparser"
|
2
2
|
|
3
|
-
|
3
|
+
class Rufo::Command
|
4
4
|
def self.run(argv)
|
5
|
-
want_check = parse_options(argv)
|
5
|
+
want_check, filename_for_dot_rufo = parse_options(argv)
|
6
|
+
new(want_check, filename_for_dot_rufo).run(argv)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(want_check, filename_for_dot_rufo)
|
10
|
+
@want_check = want_check
|
11
|
+
@filename_for_dot_rufo = filename_for_dot_rufo
|
12
|
+
@dot_file = Rufo::DotFile.new
|
13
|
+
end
|
6
14
|
|
15
|
+
def run(argv)
|
7
16
|
if argv.empty?
|
8
|
-
format_stdin
|
17
|
+
format_stdin
|
9
18
|
else
|
10
|
-
format_args argv
|
19
|
+
format_args argv
|
11
20
|
end
|
12
21
|
end
|
13
22
|
|
14
|
-
def
|
15
|
-
code
|
16
|
-
result = format(code, Dir.getwd)
|
23
|
+
def format_stdin
|
24
|
+
code = STDIN.read
|
25
|
+
result = format(code, @filename_for_dot_rufo || Dir.getwd)
|
17
26
|
|
18
|
-
if want_check
|
27
|
+
if @want_check
|
19
28
|
exit 1 if result != code
|
20
29
|
else
|
21
30
|
print result
|
@@ -30,12 +39,12 @@ module Rufo::Command
|
|
30
39
|
raise ex
|
31
40
|
end
|
32
41
|
|
33
|
-
def
|
42
|
+
def format_args(args)
|
34
43
|
files = []
|
35
44
|
|
36
45
|
args.each do |arg|
|
37
46
|
if Dir.exist?(arg)
|
38
|
-
files.concat Dir["#{arg}/**/*.rb"]
|
47
|
+
files.concat Dir["#{arg}/**/*.rb"].select(&File.method(:file?))
|
39
48
|
elsif File.exist?(arg)
|
40
49
|
files << arg
|
41
50
|
else
|
@@ -46,14 +55,14 @@ module Rufo::Command
|
|
46
55
|
changed = false
|
47
56
|
|
48
57
|
files.each do |file|
|
49
|
-
success
|
58
|
+
success = format_file file
|
50
59
|
changed ||= success
|
51
60
|
end
|
52
61
|
|
53
62
|
exit 1 if changed
|
54
63
|
end
|
55
64
|
|
56
|
-
def
|
65
|
+
def format_file(filename)
|
57
66
|
code = File.read(filename)
|
58
67
|
|
59
68
|
begin
|
@@ -66,7 +75,7 @@ module Rufo::Command
|
|
66
75
|
end
|
67
76
|
|
68
77
|
if code != result
|
69
|
-
if want_check
|
78
|
+
if @want_check
|
70
79
|
STDERR.puts "Error: formatting #{filename} produced changes"
|
71
80
|
else
|
72
81
|
File.write(filename, result)
|
@@ -88,13 +97,13 @@ module Rufo::Command
|
|
88
97
|
raise ex
|
89
98
|
end
|
90
99
|
|
91
|
-
def
|
100
|
+
def format(code, dir)
|
92
101
|
formatter = Rufo::Formatter.new(code)
|
93
102
|
|
94
|
-
dot_rufo =
|
103
|
+
dot_rufo = @dot_file.find_in(dir)
|
95
104
|
if dot_rufo
|
96
105
|
begin
|
97
|
-
formatter.instance_eval(
|
106
|
+
formatter.instance_eval(dot_rufo)
|
98
107
|
rescue => ex
|
99
108
|
STDERR.puts "Error evaluating #{dot_rufo}"
|
100
109
|
raise ex
|
@@ -105,21 +114,9 @@ module Rufo::Command
|
|
105
114
|
formatter.result
|
106
115
|
end
|
107
116
|
|
108
|
-
def self.find_dot_rufo(dir)
|
109
|
-
dir = File.expand_path(dir)
|
110
|
-
file = File.join(dir, ".rufo")
|
111
|
-
if File.exist?(file)
|
112
|
-
return file
|
113
|
-
end
|
114
|
-
|
115
|
-
parent_dir = File.dirname(dir)
|
116
|
-
return if parent_dir == dir
|
117
|
-
|
118
|
-
find_dot_rufo(parent_dir)
|
119
|
-
end
|
120
|
-
|
121
117
|
def self.parse_options(argv)
|
122
118
|
want_check = false
|
119
|
+
filename_for_dot_rufo = nil
|
123
120
|
|
124
121
|
OptionParser.new do |opts|
|
125
122
|
opts.banner = "Usage: rufo files or dirs [options]"
|
@@ -128,12 +125,16 @@ module Rufo::Command
|
|
128
125
|
want_check = true
|
129
126
|
end
|
130
127
|
|
128
|
+
opts.on("--filename=value", "Filename to use to lookup .rufo (useful for STDIN formatting)") do |value|
|
129
|
+
filename_for_dot_rufo = value
|
130
|
+
end
|
131
|
+
|
131
132
|
opts.on("-h", "--help", "Show this help") do
|
132
133
|
puts opts
|
133
134
|
exit
|
134
135
|
end
|
135
136
|
end.parse!(argv)
|
136
137
|
|
137
|
-
want_check
|
138
|
+
[want_check, filename_for_dot_rufo]
|
138
139
|
end
|
139
140
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Rufo::DotFile
|
2
|
+
def initialize
|
3
|
+
@cache = {}
|
4
|
+
end
|
5
|
+
|
6
|
+
def find_in(dir)
|
7
|
+
@cache.fetch(dir) do
|
8
|
+
@cache[dir] = internal_find_in(dir)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def internal_find_in(dir)
|
13
|
+
dir = File.expand_path(dir)
|
14
|
+
file = File.join(dir, ".rufo")
|
15
|
+
if File.exist?(file)
|
16
|
+
return File.read(file)
|
17
|
+
end
|
18
|
+
|
19
|
+
parent_dir = File.dirname(dir)
|
20
|
+
return if parent_dir == dir
|
21
|
+
|
22
|
+
find_in(parent_dir)
|
23
|
+
end
|
24
|
+
end
|
data/lib/rufo/formatter.rb
CHANGED
@@ -8,19 +8,19 @@ class Rufo::Formatter
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def initialize(code, **options)
|
11
|
-
@code
|
11
|
+
@code = code
|
12
12
|
@tokens = Ripper.lex(code).reverse!
|
13
|
-
@sexp
|
13
|
+
@sexp = Ripper.sexp(code)
|
14
14
|
|
15
15
|
unless @sexp
|
16
16
|
raise ::Rufo::SyntaxError.new
|
17
17
|
end
|
18
18
|
|
19
|
-
@indent
|
20
|
-
@line
|
21
|
-
@column
|
19
|
+
@indent = 0
|
20
|
+
@line = 0
|
21
|
+
@column = 0
|
22
22
|
@last_was_newline = false
|
23
|
-
@output
|
23
|
+
@output = ""
|
24
24
|
|
25
25
|
# The column of a `obj.method` call, so we can align
|
26
26
|
# calls to that dot
|
@@ -53,10 +53,11 @@ class Rufo::Formatter
|
|
53
53
|
@hash_keys_positions = []
|
54
54
|
|
55
55
|
# Settings
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
56
|
+
indent_size options.fetch(:indent_size, 2)
|
57
|
+
space_after_hash_brace options.fetch(:space_after_hash_brace, :dynamic)
|
58
|
+
align_comments options.fetch(:align_comments, true)
|
59
|
+
align_assignments options.fetch(:align_assignments, false)
|
60
|
+
align_hash_keys options.fetch(:align_hash_keys, true)
|
60
61
|
end
|
61
62
|
|
62
63
|
# The indent size (default: 2)
|
@@ -64,12 +65,26 @@ class Rufo::Formatter
|
|
64
65
|
@indent_size = value
|
65
66
|
end
|
66
67
|
|
68
|
+
# Whether to put a space after a hash brace. Valid values are:
|
69
|
+
#
|
70
|
+
# * :dynamic: if there's a space, keep it. If not, don't keep it (default)
|
71
|
+
# * :always: always put a space after a hash brace
|
72
|
+
# * :never: never put a space after a hash brace
|
73
|
+
def space_after_hash_brace(value)
|
74
|
+
case value
|
75
|
+
when :dynamic, :always, :never
|
76
|
+
@space_after_hash_brace = value
|
77
|
+
else
|
78
|
+
raise ArgumentError.new("invalid value for #{space_after_hash_brace}: #{value}. Valid values are: :dynamic, :always, :never")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
67
82
|
# Whether to align successive comments (default: true)
|
68
83
|
def align_comments(value)
|
69
84
|
@align_comments = value
|
70
85
|
end
|
71
86
|
|
72
|
-
# Whether to align successive assignments (default:
|
87
|
+
# Whether to align successive assignments (default: false)
|
73
88
|
def align_assignments(value)
|
74
89
|
@align_assignments = value
|
75
90
|
end
|
@@ -146,7 +161,7 @@ class Rufo::Formatter
|
|
146
161
|
when :@tstring_content
|
147
162
|
# [:@tstring_content, "hello ", [1, 1]]
|
148
163
|
heredoc, tilde = @current_heredoc
|
149
|
-
column
|
164
|
+
column = node[2][0]
|
150
165
|
|
151
166
|
# For heredocs with tilde we sometimes need to align the contents
|
152
167
|
if heredoc && tilde && @last_was_newline
|
@@ -435,7 +450,7 @@ class Rufo::Formatter
|
|
435
450
|
def visit_string_literal(node)
|
436
451
|
# [:string_literal, [:string_content, exps]]
|
437
452
|
heredoc = current_token_kind == :on_heredoc_beg
|
438
|
-
tilde
|
453
|
+
tilde = current_token_value.include?("~")
|
439
454
|
|
440
455
|
if heredoc
|
441
456
|
write current_token_value.rstrip
|
@@ -590,7 +605,7 @@ class Rufo::Formatter
|
|
590
605
|
check :on_op
|
591
606
|
|
592
607
|
before = op[1][0...-1]
|
593
|
-
after
|
608
|
+
after = op[1][-1]
|
594
609
|
|
595
610
|
write before
|
596
611
|
track_assignment before.size
|
@@ -618,15 +633,25 @@ class Rufo::Formatter
|
|
618
633
|
|
619
634
|
def visit_assign_value(value)
|
620
635
|
skip_space
|
621
|
-
|
636
|
+
|
637
|
+
indent_after_space value, indentable_value?(value)
|
622
638
|
end
|
623
639
|
|
624
|
-
def
|
640
|
+
def indentable_value?(value)
|
625
641
|
return unless current_token_kind == :on_kw
|
626
642
|
|
627
643
|
case current_token_value
|
628
644
|
when "if", "unless", "case"
|
629
645
|
true
|
646
|
+
when "begin"
|
647
|
+
# Only indent if it's begin/rescue
|
648
|
+
return false unless value[0] == :begin
|
649
|
+
|
650
|
+
body = value[1]
|
651
|
+
return false unless body[0] == :bodystmt
|
652
|
+
|
653
|
+
_, body, rescue_body, else_body, ensure_body = body
|
654
|
+
rescue_body || else_body || ensure_body
|
630
655
|
else
|
631
656
|
false
|
632
657
|
end
|
@@ -874,7 +899,7 @@ class Rufo::Formatter
|
|
874
899
|
@current_heredoc = [heredoc, tilde]
|
875
900
|
visit_string_literal_end(heredoc)
|
876
901
|
@current_heredoc = nil
|
877
|
-
printed
|
902
|
+
printed = true
|
878
903
|
end
|
879
904
|
end
|
880
905
|
|
@@ -1132,7 +1157,7 @@ class Rufo::Formatter
|
|
1132
1157
|
consume_keyword "rescue"
|
1133
1158
|
if type
|
1134
1159
|
skip_space
|
1135
|
-
write_space
|
1160
|
+
write_space
|
1136
1161
|
indent(@column) do
|
1137
1162
|
visit_rescue_types(type)
|
1138
1163
|
end
|
@@ -1140,10 +1165,10 @@ class Rufo::Formatter
|
|
1140
1165
|
|
1141
1166
|
if name
|
1142
1167
|
skip_space
|
1143
|
-
write_space
|
1168
|
+
write_space
|
1144
1169
|
consume_op "=>"
|
1145
1170
|
skip_space
|
1146
|
-
write_space
|
1171
|
+
write_space
|
1147
1172
|
visit name
|
1148
1173
|
end
|
1149
1174
|
|
@@ -1308,7 +1333,7 @@ class Rufo::Formatter
|
|
1308
1333
|
|
1309
1334
|
if newline? || comment?
|
1310
1335
|
needs_indent = true
|
1311
|
-
base_column
|
1336
|
+
base_column = next_indent
|
1312
1337
|
consume_end_of_line
|
1313
1338
|
write_indent(base_column)
|
1314
1339
|
else
|
@@ -1336,7 +1361,7 @@ class Rufo::Formatter
|
|
1336
1361
|
write_indent
|
1337
1362
|
end
|
1338
1363
|
else
|
1339
|
-
write_space
|
1364
|
+
write_space
|
1340
1365
|
skip_space_or_newline
|
1341
1366
|
end
|
1342
1367
|
end
|
@@ -1395,7 +1420,7 @@ class Rufo::Formatter
|
|
1395
1420
|
write_line
|
1396
1421
|
write_indent(next_indent)
|
1397
1422
|
else
|
1398
|
-
write_space
|
1423
|
+
write_space if needs_space
|
1399
1424
|
end
|
1400
1425
|
|
1401
1426
|
consume_op_or_keyword op
|
@@ -1421,15 +1446,15 @@ class Rufo::Formatter
|
|
1421
1446
|
|
1422
1447
|
consume_keyword "class"
|
1423
1448
|
skip_space_or_newline
|
1424
|
-
write_space
|
1449
|
+
write_space
|
1425
1450
|
visit name
|
1426
1451
|
|
1427
1452
|
if superclass
|
1428
1453
|
skip_space_or_newline
|
1429
|
-
write_space
|
1454
|
+
write_space
|
1430
1455
|
consume_op "<"
|
1431
1456
|
skip_space_or_newline
|
1432
|
-
write_space
|
1457
|
+
write_space
|
1433
1458
|
visit superclass
|
1434
1459
|
end
|
1435
1460
|
|
@@ -1444,7 +1469,7 @@ class Rufo::Formatter
|
|
1444
1469
|
|
1445
1470
|
consume_keyword "module"
|
1446
1471
|
skip_space_or_newline
|
1447
|
-
write_space
|
1472
|
+
write_space
|
1448
1473
|
visit name
|
1449
1474
|
maybe_inline_body body
|
1450
1475
|
end
|
@@ -1599,10 +1624,10 @@ class Rufo::Formatter
|
|
1599
1624
|
visit_comma_separated_list(args_with_default) do |arg, default|
|
1600
1625
|
visit arg
|
1601
1626
|
skip_space
|
1602
|
-
write_space
|
1627
|
+
write_space
|
1603
1628
|
consume_op "="
|
1604
1629
|
skip_space_or_newline
|
1605
|
-
write_space
|
1630
|
+
write_space
|
1606
1631
|
visit default
|
1607
1632
|
end
|
1608
1633
|
needs_comma = true
|
@@ -1678,7 +1703,7 @@ class Rufo::Formatter
|
|
1678
1703
|
consume_end_of_line
|
1679
1704
|
write_indent
|
1680
1705
|
else
|
1681
|
-
write_space
|
1706
|
+
write_space
|
1682
1707
|
skip_space_or_newline
|
1683
1708
|
end
|
1684
1709
|
end
|
@@ -1753,14 +1778,14 @@ class Rufo::Formatter
|
|
1753
1778
|
write_indent(next_indent)
|
1754
1779
|
else
|
1755
1780
|
next_token
|
1756
|
-
write_space
|
1781
|
+
write_space
|
1757
1782
|
end
|
1758
1783
|
end
|
1759
1784
|
end
|
1760
1785
|
end
|
1761
1786
|
|
1762
1787
|
has_newline = false
|
1763
|
-
last_token
|
1788
|
+
last_token = nil
|
1764
1789
|
|
1765
1790
|
while current_token_kind == :on_words_sep
|
1766
1791
|
has_newline ||= current_token_value.include?("\n")
|
@@ -1830,7 +1855,7 @@ class Rufo::Formatter
|
|
1830
1855
|
if symbol || !(key[0] == :@label || key[0] == :dyna_symbol)
|
1831
1856
|
consume_op "=>"
|
1832
1857
|
skip_space_or_newline
|
1833
|
-
write_space
|
1858
|
+
write_space
|
1834
1859
|
end
|
1835
1860
|
|
1836
1861
|
visit value
|
@@ -2152,10 +2177,17 @@ class Rufo::Formatter
|
|
2152
2177
|
end
|
2153
2178
|
|
2154
2179
|
def visit_literal_elements(elements, inside_hash = false)
|
2155
|
-
base_column
|
2180
|
+
base_column = @column
|
2156
2181
|
needs_final_space = inside_hash && space?
|
2157
2182
|
skip_space
|
2158
2183
|
|
2184
|
+
case @space_after_hash_brace
|
2185
|
+
when :never
|
2186
|
+
needs_final_space = false
|
2187
|
+
when :always
|
2188
|
+
needs_final_space = true
|
2189
|
+
end
|
2190
|
+
|
2159
2191
|
if newline? || comment?
|
2160
2192
|
needs_final_space = false
|
2161
2193
|
elsif needs_final_space
|
@@ -2198,7 +2230,7 @@ class Rufo::Formatter
|
|
2198
2230
|
write_indent(needed_indent)
|
2199
2231
|
end
|
2200
2232
|
else
|
2201
|
-
write_space
|
2233
|
+
write_space unless is_last
|
2202
2234
|
end
|
2203
2235
|
end
|
2204
2236
|
end
|
@@ -2290,7 +2322,7 @@ class Rufo::Formatter
|
|
2290
2322
|
|
2291
2323
|
# Keep `while cond; end` as is
|
2292
2324
|
semicolon = semicolon?
|
2293
|
-
is_do
|
2325
|
+
is_do = keyword?("do")
|
2294
2326
|
|
2295
2327
|
if (semicolon || is_do) && void_exps?(body)
|
2296
2328
|
next_token
|
@@ -2366,7 +2398,7 @@ class Rufo::Formatter
|
|
2366
2398
|
end
|
2367
2399
|
|
2368
2400
|
then_keyword = keyword?("then")
|
2369
|
-
inline
|
2401
|
+
inline = then_keyword || semicolon?
|
2370
2402
|
if then_keyword
|
2371
2403
|
next_token
|
2372
2404
|
skip_space
|
@@ -2406,7 +2438,7 @@ class Rufo::Formatter
|
|
2406
2438
|
if newline? || semicolon? || comment?
|
2407
2439
|
indent_body next_exp[1]
|
2408
2440
|
else
|
2409
|
-
write_space
|
2441
|
+
write_space
|
2410
2442
|
visit_exps next_exp[1]
|
2411
2443
|
end
|
2412
2444
|
else
|
@@ -2417,7 +2449,7 @@ class Rufo::Formatter
|
|
2417
2449
|
|
2418
2450
|
def consume_space
|
2419
2451
|
skip_space_or_newline
|
2420
|
-
write_space
|
2452
|
+
write_space unless @output[-1] == " "
|
2421
2453
|
end
|
2422
2454
|
|
2423
2455
|
def skip_space
|
@@ -2436,10 +2468,10 @@ class Rufo::Formatter
|
|
2436
2468
|
end
|
2437
2469
|
|
2438
2470
|
def skip_space_or_newline(want_semicolon = false, write_first_semicolon = false)
|
2439
|
-
found_newline
|
2440
|
-
found_comment
|
2471
|
+
found_newline = false
|
2472
|
+
found_comment = false
|
2441
2473
|
found_semicolon = false
|
2442
|
-
last
|
2474
|
+
last = nil
|
2443
2475
|
|
2444
2476
|
while true
|
2445
2477
|
case current_token_kind
|
@@ -2447,14 +2479,14 @@ class Rufo::Formatter
|
|
2447
2479
|
next_token
|
2448
2480
|
when :on_nl, :on_ignored_nl
|
2449
2481
|
next_token
|
2450
|
-
last
|
2482
|
+
last = :newline
|
2451
2483
|
found_newline = true
|
2452
2484
|
when :on_semicolon
|
2453
2485
|
if (!found_newline && !found_comment) || (!found_semicolon && write_first_semicolon)
|
2454
2486
|
write "; "
|
2455
2487
|
end
|
2456
2488
|
next_token
|
2457
|
-
last
|
2489
|
+
last = :semicolon
|
2458
2490
|
found_semicolon = true
|
2459
2491
|
when :on_comment
|
2460
2492
|
write_line if last == :newline
|
@@ -2468,7 +2500,7 @@ class Rufo::Formatter
|
|
2468
2500
|
end
|
2469
2501
|
next_token
|
2470
2502
|
found_comment = true
|
2471
|
-
last
|
2503
|
+
last = :comment
|
2472
2504
|
else
|
2473
2505
|
break
|
2474
2506
|
end
|
@@ -2501,9 +2533,9 @@ class Rufo::Formatter
|
|
2501
2533
|
# If the value has newlines, we need to adjust line and column
|
2502
2534
|
number_of_lines = value.count("\n")
|
2503
2535
|
if number_of_lines > 0
|
2504
|
-
@line
|
2505
|
-
last_line_index
|
2506
|
-
@column
|
2536
|
+
@line += number_of_lines
|
2537
|
+
last_line_index = value.rindex("\n")
|
2538
|
+
@column = value.size - (last_line_index + 1)
|
2507
2539
|
@last_was_newline = @column == 0
|
2508
2540
|
end
|
2509
2541
|
end
|
@@ -2532,11 +2564,11 @@ class Rufo::Formatter
|
|
2532
2564
|
# - want_semicolon: do we want do print a semicolon to separate expressions?
|
2533
2565
|
# - want_multiline: do we want multiple lines to appear, or at most one?
|
2534
2566
|
def consume_end_of_line(at_prefix = false, want_semicolon = false, want_multiline = true, needs_two_lines_on_comment = false)
|
2535
|
-
found_newline
|
2536
|
-
last
|
2537
|
-
multilple_lines
|
2567
|
+
found_newline = false # Did we find any newline during this method?
|
2568
|
+
last = nil # Last token kind found
|
2569
|
+
multilple_lines = false # Did we pass through more than one newline?
|
2538
2570
|
last_comment_has_newline = false # Does the last comment has a newline?
|
2539
|
-
newline_count
|
2571
|
+
newline_count = 0 # Number of newlines we passed
|
2540
2572
|
|
2541
2573
|
while true
|
2542
2574
|
case current_token_kind
|
@@ -2568,7 +2600,7 @@ class Rufo::Formatter
|
|
2568
2600
|
end
|
2569
2601
|
found_newline = true
|
2570
2602
|
next_token
|
2571
|
-
last
|
2603
|
+
last = :newline
|
2572
2604
|
newline_count += 1
|
2573
2605
|
when :on_semicolon
|
2574
2606
|
next_token
|
@@ -2608,20 +2640,20 @@ class Rufo::Formatter
|
|
2608
2640
|
else
|
2609
2641
|
# If we didn't find any newline yet, this is the first comment,
|
2610
2642
|
# so append a space if needed (for example after an expression)
|
2611
|
-
write_space
|
2643
|
+
write_space unless at_prefix
|
2612
2644
|
track_comment
|
2613
2645
|
end
|
2614
2646
|
end
|
2615
2647
|
last_comment_has_newline = current_token_value.end_with?("\n")
|
2616
2648
|
write current_token_value.rstrip
|
2617
2649
|
next_token
|
2618
|
-
last
|
2650
|
+
last = :comment
|
2619
2651
|
multilple_lines = false
|
2620
2652
|
when :on_embdoc_beg
|
2621
2653
|
write_line if multilple_lines
|
2622
2654
|
|
2623
2655
|
consume_embedded_comment
|
2624
|
-
last
|
2656
|
+
last = :comment
|
2625
2657
|
last_comment_has_newline = true
|
2626
2658
|
else
|
2627
2659
|
break
|
@@ -2670,7 +2702,7 @@ class Rufo::Formatter
|
|
2670
2702
|
def indent(value = nil)
|
2671
2703
|
if value
|
2672
2704
|
old_indent = @indent
|
2673
|
-
@indent
|
2705
|
+
@indent = value
|
2674
2706
|
yield
|
2675
2707
|
@indent = old_indent
|
2676
2708
|
else
|
@@ -2710,26 +2742,24 @@ class Rufo::Formatter
|
|
2710
2742
|
def write(value)
|
2711
2743
|
@output << value
|
2712
2744
|
@last_was_newline = false
|
2713
|
-
@column
|
2745
|
+
@column += value.size
|
2714
2746
|
end
|
2715
2747
|
|
2716
|
-
def write_space
|
2717
|
-
@output <<
|
2718
|
-
@column +=
|
2748
|
+
def write_space
|
2749
|
+
@output << " "
|
2750
|
+
@column += 1
|
2719
2751
|
end
|
2720
2752
|
|
2721
2753
|
def write_line
|
2722
2754
|
@output << "\n"
|
2723
2755
|
@last_was_newline = true
|
2724
|
-
@column
|
2725
|
-
@line
|
2756
|
+
@column = 0
|
2757
|
+
@line += 1
|
2726
2758
|
end
|
2727
2759
|
|
2728
2760
|
def write_indent(indent = @indent)
|
2729
|
-
indent
|
2730
|
-
|
2731
|
-
end
|
2732
|
-
@column += indent
|
2761
|
+
@output << " " * indent
|
2762
|
+
@column += indent
|
2733
2763
|
@last_was_newline = false
|
2734
2764
|
end
|
2735
2765
|
|
@@ -2743,7 +2773,7 @@ class Rufo::Formatter
|
|
2743
2773
|
visit node
|
2744
2774
|
end
|
2745
2775
|
else
|
2746
|
-
write_space
|
2776
|
+
write_space if want_space
|
2747
2777
|
if sticky
|
2748
2778
|
indent(@column) do
|
2749
2779
|
visit node
|
@@ -2813,9 +2843,9 @@ class Rufo::Formatter
|
|
2813
2843
|
|
2814
2844
|
def find_closing_brace_token
|
2815
2845
|
count = 0
|
2816
|
-
i
|
2846
|
+
i = @tokens.size - 1
|
2817
2847
|
while i >= 0
|
2818
|
-
token
|
2848
|
+
token = @tokens[i]
|
2819
2849
|
(line, column), kind = token
|
2820
2850
|
case kind
|
2821
2851
|
when :on_lbrace, :on_tlambeg
|
@@ -2869,7 +2899,7 @@ class Rufo::Formatter
|
|
2869
2899
|
end
|
2870
2900
|
|
2871
2901
|
def push_node(node)
|
2872
|
-
old_node
|
2902
|
+
old_node = @current_node
|
2873
2903
|
@current_node = node
|
2874
2904
|
|
2875
2905
|
yield
|
@@ -2878,7 +2908,7 @@ class Rufo::Formatter
|
|
2878
2908
|
end
|
2879
2909
|
|
2880
2910
|
def push_hash(node)
|
2881
|
-
old_hash
|
2911
|
+
old_hash = @current_hash
|
2882
2912
|
@current_hash = node
|
2883
2913
|
yield
|
2884
2914
|
@current_hash = old_hash
|
@@ -2893,10 +2923,10 @@ class Rufo::Formatter
|
|
2893
2923
|
end
|
2894
2924
|
|
2895
2925
|
def do_align_hash_keys
|
2896
|
-
do_align @hash_keys_positions
|
2926
|
+
do_align @hash_keys_positions, true, true
|
2897
2927
|
end
|
2898
2928
|
|
2899
|
-
def do_align(elements, adjust_comments = true)
|
2929
|
+
def do_align(elements, adjust_comments = true, hash_keys = false)
|
2900
2930
|
lines = @output.lines
|
2901
2931
|
|
2902
2932
|
elements.reject! { |l, c, indent, id, off, ignore| ignore == :ignore }
|
@@ -2909,21 +2939,38 @@ class Rufo::Formatter
|
|
2909
2939
|
chunks.each do |comments|
|
2910
2940
|
next if comments.size == 1
|
2911
2941
|
|
2942
|
+
if hash_keys
|
2943
|
+
# Don't indent successive hash keys if any of them is in turn a hash
|
2944
|
+
# or array literal that is formatted in separate lines.
|
2945
|
+
has_brace_newline = comments.any? do |(l, c)|
|
2946
|
+
line_end = lines[l][c..-1]
|
2947
|
+
line_end.start_with?("=> {\n") ||
|
2948
|
+
line_end.start_with?("=> [\n") ||
|
2949
|
+
line_end.start_with?("=> [ #") ||
|
2950
|
+
line_end.start_with?("=> { #") ||
|
2951
|
+
line_end.start_with?("[\n") ||
|
2952
|
+
line_end.start_with?("{\n") ||
|
2953
|
+
line_end.start_with?("[ #") ||
|
2954
|
+
line_end.start_with?("{ #")
|
2955
|
+
end
|
2956
|
+
next if has_brace_newline
|
2957
|
+
end
|
2958
|
+
|
2912
2959
|
max_column = comments.map { |l, c| c }.max
|
2913
2960
|
|
2914
2961
|
comments.each do |(line, column, _, _, offset)|
|
2915
2962
|
next if column == max_column
|
2916
2963
|
|
2917
|
-
split_index
|
2964
|
+
split_index = column
|
2918
2965
|
split_index -= offset if offset
|
2919
2966
|
|
2920
2967
|
target_line = lines[line]
|
2921
2968
|
|
2922
2969
|
before = target_line[0...split_index]
|
2923
|
-
after
|
2970
|
+
after = target_line[split_index..-1]
|
2924
2971
|
|
2925
2972
|
filler_size = max_column - column
|
2926
|
-
filler
|
2973
|
+
filler = " " * filler_size
|
2927
2974
|
|
2928
2975
|
if adjust_comments && (index = @line_to_comments_position_index[line])
|
2929
2976
|
@comments_positions[index][1] += filler_size
|
data/lib/rufo/version.rb
CHANGED
data/lib/rufo.rb
CHANGED
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.
|
4
|
+
version: 0.0.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ary Borenszweig
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/rufo.rb
|
75
75
|
- lib/rufo/backport.rb
|
76
76
|
- lib/rufo/command.rb
|
77
|
+
- lib/rufo/dot_file.rb
|
77
78
|
- lib/rufo/formatter.rb
|
78
79
|
- lib/rufo/version.rb
|
79
80
|
- rakelib/ci.rake
|