-
Notifications
You must be signed in to change notification settings - Fork 71
/
nest.jl
259 lines (239 loc) · 7.26 KB
/
nest.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
for f in [
:n_import!,
:n_using!,
:n_export!,
:n_public!,
:n_vcat!,
:n_ncat!,
:n_typedvcat!,
:n_typedncat!,
:n_row!,
:n_nrow!,
:n_hcat!,
:n_comprehension!,
:n_typedcomprehension!,
:n_generator!,
:n_filter!,
]
@eval function $f(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
$f(YASStyle(getstyle(ss)), fst, s, lineage)
end
end
# function n_binaryopcall!(ss::SciMLStyle, fst::FST, s::State, lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}}; indent::Int = -1)
# style = getstyle(ss)
# line_margin = s.line_offset + length(fst) + fst.extra_margin
# if line_margin > s.opts.margin && !isnothing(fst.metadata) && fst.metadata.is_short_form_function
# transformed = short_to_long_function_def!(fst, s)
# transformed && nest!(style, fst, s, lineage)
# return
# end
#
# if findfirst(n -> n.typ === PLACEHOLDER, fst.nodes) !== nothing
# n_binaryopcall!(DefaultStyle(style), fst, s, lineage; indent = indent)
# return
# end
#
# start_line_offset = s.line_offset
# walk(increment_line_offset!, (fst.nodes::Vector)[1:end-1], s, fst.indent)
# nest!(style, fst[end], s, lineage)
# end
function n_functiondef!(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
style = getstyle(ss)
nested = false
if s.opts.yas_style_nesting
nested |= nest!(
YASStyle(style),
fst.nodes::Vector,
s,
fst.indent,
lineage;
extra_margin = fst.extra_margin,
)
else
base_indent = fst.indent
add_indent!(fst[3], s, s.opts.indent)
nested |= nest!(
ss,
fst.nodes::Vector,
s,
fst.indent,
lineage;
extra_margin = fst.extra_margin,
)
f =
(fst::FST, s::State) -> begin
if is_closer(fst) && fst.indent == base_indent + s.opts.indent
fst.indent -= s.opts.indent
end
end
lo = s.line_offset
walk(f, fst[3], s)
s.line_offset = lo
end
return nested
end
function n_macro!(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
n_functiondef!(ss, fst, s, lineage)
end
function _n_tuple!(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
style = getstyle(ss)
line_margin = s.line_offset + length(fst) + fst.extra_margin
nodes = fst.nodes::Vector
has_closer = is_closer(fst[end])
start_line_offset = s.line_offset
if has_closer
fst[end].indent = fst.indent
end
if !(fst.typ in (TupleN, CartesianIterator, Parameters)) || has_closer
fst.indent += s.opts.indent
end
# "foo(a, b, c)" is true if "foo" and "c" are on different lines
src_diff_line = if s.opts.join_lines_based_on_source
last_arg_idx = findlast(is_iterable_arg, nodes)
last_arg = last_arg_idx === nothing ? fst[end] : fst[last_arg_idx]
fst[1].endline != last_arg.startline
else
false
end
nested = false
optimal_placeholders = find_optimal_nest_placeholders(fst, fst.indent, s.opts.margin)
if length(optimal_placeholders) > 0
nested = true
end
for i in optimal_placeholders
fst[i] = Newline(; length = fst[i].len)
end
placeholder_inds = findall(n -> n.typ === PLACEHOLDER, fst.nodes)
for (i, ph) in enumerate(placeholder_inds)
if i == 1 ||
i == length(placeholder_inds) ||
(ph < length(fst) && is_comment(fst[ph+1])) ||
(ph > 1 && is_comment(fst[ph-1]))
continue
end
fst[ph] = Whitespace(fst[ph].len)
end
# macrocall doesn't have a placeholder before the closing parenthesis
if fst.typ !== MacroCall && has_closer && length(placeholder_inds) > 0
fst[placeholder_inds[end]] = Whitespace(0)
end
idx = findlast(n -> n.typ === PLACEHOLDER, nodes)
if idx !== nothing && (line_margin > s.opts.margin || must_nest(fst) || src_diff_line)
for (i, n) in enumerate(nodes)
if n.typ === NEWLINE
s.line_offset = fst.indent
elseif n.typ === PLACEHOLDER
si = findnext(n -> n.typ === PLACEHOLDER || n.typ === NEWLINE, nodes, i + 1)
nested2 = nest_if_over_margin!(style, fst, s, i, lineage; stop_idx = si)
nested |= nested2
if has_closer && !nested2 && n.startline == fst[end].startline
# trailing types are automatically converted, undo this if
# there is no nest and the closer is on the same in the
# original source.
if fst[i-1].typ === TRAILINGCOMMA
fst[i-1].val = ""
fst[i-1].len = 0
end
end
elseif n.typ === TRAILINGCOMMA
n.val = ","
n.len = 1
nested |= nest!(style, n, s, lineage)
elseif has_closer && (i == 1 || i == length(nodes))
nested |= nest!(style, n, s, lineage)
else
diff = fst.indent - fst[i].indent
add_indent!(n, s, diff)
n.extra_margin = 1
nested |= nest!(style, n, s, lineage)
end
end
if has_closer
s.line_offset = fst[end].indent + 1
end
else
extra_margin = fst.extra_margin
if has_closer
(extra_margin += 1)
else
false
end
nested |= nest!(style, nodes, s, fst.indent, lineage; extra_margin = extra_margin)
end
s.line_offset = start_line_offset
walk(unnest!(style; dedent = false), fst, s)
s.line_offset = start_line_offset
walk(increment_line_offset!, fst, s)
return nested
end
for f in [
:n_tuple!,
:n_call!,
:n_curly!,
:n_macrocall!,
:n_ref!,
:n_braces!,
:n_parameters!,
:n_invisbrackets!,
:n_bracescat!,
]
@eval function $f(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
if s.opts.yas_style_nesting
$f(YASStyle(getstyle(ss)), fst, s, lineage)
else
_n_tuple!(getstyle(ss), fst, s, lineage)
end
end
end
function n_vect!(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
if s.opts.yas_style_nesting
# Allow a line break after the opening brackets without aligning
n_vect!(DefaultStyle(getstyle(ss)), fst, s, lineage)
else
_n_tuple!(getstyle(ss), fst, s, lineage)
end
end
for f in [:n_chainopcall!, :n_comparison!, :n_for!]
@eval function $f(
ss::SciMLStyle,
fst::FST,
s::State,
lineage::Vector{Tuple{FNode,Union{Nothing,Metadata}}},
)
if s.opts.yas_style_nesting
$f(YASStyle(getstyle(ss)), fst, s, lineage)
else
$f(DefaultStyle(getstyle(ss)), fst, s, lineage)
end
end
end