rufo 0.0.29 → 0.0.30
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rufo/formatter.rb +67 -24
- data/lib/rufo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d993ab0deff7da557c3e35bac8271ad6c60d43c
|
4
|
+
data.tar.gz: 669c9785a3ef39403dc0b6746ad0e5c8c7196e15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6bcf974f8c95767a9a70a1f24acad264d6facb5040e466709a3b15fcfc11f5ecf8c0de1f8cbf02d9b34e051f0487711ad7f79639b9908a6cff99207dc4f2e19
|
7
|
+
data.tar.gz: 0e2f04958a659060263e4e1dfa15748e2b70bbce178f91db46a278aeec5cc8671ed207ec3b053aee609edbc5881bba78b313bc431925826c82e7081351e621c2
|
data/lib/rufo/formatter.rb
CHANGED
@@ -26,6 +26,9 @@ class Rufo::Formatter
|
|
26
26
|
# calls to that dot
|
27
27
|
@dot_column = nil
|
28
28
|
|
29
|
+
# Did this line already set the `@dot_column` variable?
|
30
|
+
@line_has_dot_column = nil
|
31
|
+
|
29
32
|
# The column of a `obj.method` call, but only the name part,
|
30
33
|
# so we can also align arguments accordingly
|
31
34
|
@name_dot_column = nil
|
@@ -103,16 +106,16 @@ class Rufo::Formatter
|
|
103
106
|
@case_when_positions = []
|
104
107
|
|
105
108
|
# Settings
|
106
|
-
indent_size
|
107
|
-
space_after_hash_brace
|
109
|
+
indent_size options.fetch(:indent_size, 2)
|
110
|
+
space_after_hash_brace options.fetch(:space_after_hash_brace, :dynamic)
|
108
111
|
space_after_array_bracket options.fetch(:space_after_array_bracket, :never)
|
109
|
-
align_comments
|
110
|
-
align_assignments
|
111
|
-
align_hash_keys
|
112
|
-
align_case_when
|
113
|
-
align_chained_calls
|
114
|
-
preserve_whitespace
|
115
|
-
trailing_commas
|
112
|
+
align_comments options.fetch(:align_comments, true)
|
113
|
+
align_assignments options.fetch(:align_assignments, false)
|
114
|
+
align_hash_keys options.fetch(:align_hash_keys, true)
|
115
|
+
align_case_when options.fetch(:align_case_when, true)
|
116
|
+
align_chained_calls options.fetch(:align_chained_calls, true)
|
117
|
+
preserve_whitespace options.fetch(:preserve_whitespace, true)
|
118
|
+
trailing_commas options.fetch(:trailing_commas, :always)
|
116
119
|
end
|
117
120
|
|
118
121
|
# The indent size (default: 2)
|
@@ -906,8 +909,9 @@ class Rufo::Formatter
|
|
906
909
|
end
|
907
910
|
end
|
908
911
|
|
909
|
-
# Remember dot column
|
910
|
-
dot_column = @column
|
912
|
+
# Remember dot column, but only if there isn't one already set
|
913
|
+
dot_column = @column unless @dot_column
|
914
|
+
|
911
915
|
consume_call_dot
|
912
916
|
|
913
917
|
skip_space
|
@@ -927,7 +931,7 @@ class Rufo::Formatter
|
|
927
931
|
|
928
932
|
# Only set it after we visit the call after the dot,
|
929
933
|
# so we remember the outmost dot position
|
930
|
-
@dot_column = dot_column
|
934
|
+
@dot_column = dot_column if dot_column
|
931
935
|
end
|
932
936
|
|
933
937
|
def consume_call_dot
|
@@ -983,6 +987,18 @@ class Rufo::Formatter
|
|
983
987
|
call_info << true
|
984
988
|
end
|
985
989
|
|
990
|
+
want_trailing_comma = true
|
991
|
+
|
992
|
+
# Check if there's a block arg and if the call ends with hash key/values
|
993
|
+
if args_node[0] == :args_add_block
|
994
|
+
_, args, block_arg = args_node
|
995
|
+
want_trailing_comma = !block_arg
|
996
|
+
if args.is_a?(Array) && (last_arg = args.last) && last_arg.is_a?(Array) &&
|
997
|
+
last_arg[0].is_a?(Symbol) && last_arg[0] != :bare_assoc_hash
|
998
|
+
want_trailing_comma = false
|
999
|
+
end
|
1000
|
+
end
|
1001
|
+
|
986
1002
|
push_call(node) do
|
987
1003
|
visit args_node
|
988
1004
|
skip_space
|
@@ -992,7 +1008,7 @@ class Rufo::Formatter
|
|
992
1008
|
|
993
1009
|
if found_comma
|
994
1010
|
if needs_trailing_newline
|
995
|
-
write "," if @trailing_commas != :never
|
1011
|
+
write "," if @trailing_commas != :never && !block_arg
|
996
1012
|
|
997
1013
|
next_token
|
998
1014
|
indent(next_indent) do
|
@@ -1007,7 +1023,7 @@ class Rufo::Formatter
|
|
1007
1023
|
|
1008
1024
|
if newline? || comment?
|
1009
1025
|
if needs_trailing_newline
|
1010
|
-
write "," if @trailing_commas == :always
|
1026
|
+
write "," if @trailing_commas == :always && want_trailing_comma
|
1011
1027
|
|
1012
1028
|
indent(next_indent) do
|
1013
1029
|
consume_end_of_line
|
@@ -1018,7 +1034,7 @@ class Rufo::Formatter
|
|
1018
1034
|
end
|
1019
1035
|
else
|
1020
1036
|
if needs_trailing_newline && !found_comma
|
1021
|
-
write "," if @trailing_commas == :always
|
1037
|
+
write "," if @trailing_commas == :always && want_trailing_comma
|
1022
1038
|
consume_end_of_line
|
1023
1039
|
write_indent
|
1024
1040
|
end
|
@@ -1314,9 +1330,15 @@ class Rufo::Formatter
|
|
1314
1330
|
|
1315
1331
|
if block_arg
|
1316
1332
|
skip_space_or_newline
|
1317
|
-
|
1333
|
+
|
1334
|
+
if comma?
|
1335
|
+
indent(next_indent) do
|
1336
|
+
write_params_comma
|
1337
|
+
end
|
1338
|
+
end
|
1318
1339
|
|
1319
1340
|
consume_op "&"
|
1341
|
+
skip_space_or_newline
|
1320
1342
|
visit block_arg
|
1321
1343
|
end
|
1322
1344
|
end
|
@@ -1630,6 +1652,21 @@ class Rufo::Formatter
|
|
1630
1652
|
# [:binary, left, op, right]
|
1631
1653
|
_, left, op, right = node
|
1632
1654
|
|
1655
|
+
# If this binary is not at the beginning of a line, if there's
|
1656
|
+
# a newline following the op we want to align it with the left
|
1657
|
+
# value. So for example:
|
1658
|
+
#
|
1659
|
+
# var = left_exp ||
|
1660
|
+
# right_exp
|
1661
|
+
#
|
1662
|
+
# But:
|
1663
|
+
#
|
1664
|
+
# def foo
|
1665
|
+
# left_exp ||
|
1666
|
+
# right_exp
|
1667
|
+
# end
|
1668
|
+
needed_indent = @column == @indent ? next_indent : @column
|
1669
|
+
|
1633
1670
|
visit left
|
1634
1671
|
if space?
|
1635
1672
|
needs_space = true
|
@@ -1650,7 +1687,7 @@ class Rufo::Formatter
|
|
1650
1687
|
end
|
1651
1688
|
|
1652
1689
|
consume_op_or_keyword op
|
1653
|
-
indent_after_space right, want_space: needs_space
|
1690
|
+
indent_after_space right, want_space: needs_space, needed_indent: needed_indent
|
1654
1691
|
end
|
1655
1692
|
|
1656
1693
|
def consume_op_or_keyword(op)
|
@@ -1757,6 +1794,11 @@ class Rufo::Formatter
|
|
1757
1794
|
check :on_rparen
|
1758
1795
|
next_token
|
1759
1796
|
skip_space
|
1797
|
+
|
1798
|
+
# () needs to be preserved if some content follows
|
1799
|
+
unless newline? || comment?
|
1800
|
+
write "()"
|
1801
|
+
end
|
1760
1802
|
else
|
1761
1803
|
write "("
|
1762
1804
|
|
@@ -1976,6 +2018,7 @@ class Rufo::Formatter
|
|
1976
2018
|
|
1977
2019
|
if elements && !elements.empty?
|
1978
2020
|
write_space if has_space
|
2021
|
+
column = @column
|
1979
2022
|
|
1980
2023
|
elements.each_with_index do |elem, i|
|
1981
2024
|
if elem[0] == :@tstring_content
|
@@ -1991,7 +2034,7 @@ class Rufo::Formatter
|
|
1991
2034
|
if current_token_value.include?("\n")
|
1992
2035
|
next_token
|
1993
2036
|
write_line
|
1994
|
-
write_indent(
|
2037
|
+
write_indent(column)
|
1995
2038
|
else
|
1996
2039
|
next_token
|
1997
2040
|
write_space
|
@@ -2894,8 +2937,8 @@ class Rufo::Formatter
|
|
2894
2937
|
# # that continues here
|
2895
2938
|
# ```
|
2896
2939
|
if last_comment &&
|
2897
|
-
|
2898
|
-
|
2940
|
+
last_comment[0][0] + 1 == current_token[0][0] &&
|
2941
|
+
last_comment[0][1] == current_token[0][1]
|
2899
2942
|
write_indent(last_comment_column)
|
2900
2943
|
track_comment
|
2901
2944
|
else
|
@@ -2947,8 +2990,8 @@ class Rufo::Formatter
|
|
2947
2990
|
# or the last thing was a comment (from which we removed the newline)
|
2948
2991
|
# or we just passed multiple lines (but printed only one)
|
2949
2992
|
if (!found_newline && !at_prefix && !(want_semicolon && last == :semicolon)) ||
|
2950
|
-
|
2951
|
-
|
2993
|
+
last == :comment ||
|
2994
|
+
(multilple_lines && want_multiline)
|
2952
2995
|
write_line
|
2953
2996
|
end
|
2954
2997
|
end
|
@@ -3100,13 +3143,13 @@ class Rufo::Formatter
|
|
3100
3143
|
@last_was_newline = false
|
3101
3144
|
end
|
3102
3145
|
|
3103
|
-
def indent_after_space(node, sticky: false, want_space: true, first_space: nil)
|
3146
|
+
def indent_after_space(node, sticky: false, want_space: true, first_space: nil, needed_indent: next_indent)
|
3104
3147
|
first_space = current_token if space?
|
3105
3148
|
|
3106
3149
|
skip_space
|
3107
3150
|
case current_token_kind
|
3108
3151
|
when :on_ignored_nl, :on_comment
|
3109
|
-
indent do
|
3152
|
+
indent(needed_indent) do
|
3110
3153
|
consume_end_of_line
|
3111
3154
|
write_indent
|
3112
3155
|
visit node
|
data/lib/rufo/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.30
|
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-
|
11
|
+
date: 2017-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|