-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathalign.jl
422 lines (361 loc) · 11.8 KB
/
align.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
function align_fst!(fst::FST, opts::Options)
if is_leaf(fst)
return
end
assignment_inds = Int[]
pair_arrow_inds = Int[]
for (i, n) in enumerate(fst.nodes)
if is_leaf(n)
continue
elseif opts.align_struct_field && (n.typ === Struct || n.typ === Mutable)
align_struct!(n)
elseif opts.align_conditional && n.typ === Conditional
align_conditional!(n)
elseif opts.align_matrix && (
n.typ === Vcat || n.typ === TypedVcat || n.typ === Ncat || n.typ === TypedNcat
)
align_matrix!(n)
else
align_fst!(n, opts)
end
if opts.align_assignment && (is_assignment(n) || n.typ === Kw)
# Gather all assignments within the current code block
# they will be aligned at the end
push!(assignment_inds, i)
elseif opts.align_pair_arrow && n.typ === Binary && op_kind(n) === K"=>"
push!(pair_arrow_inds, i)
end
end
align_binaryopcalls!(fst, assignment_inds)
align_binaryopcalls!(fst, pair_arrow_inds)
end
"""
AlignGroup
Group of FST node indices and required metadata to potentially align them.
- `node_inds`. Indices of FST nodes affected by alignment.
- `nodes`. FST nodes affected by alignment.
- `line_offsets`. Line offset of the character nodes may be aligned to
in the source file.
- `lens`. Length of the FST node prior to the alignment character. Used
to calculate extra whitespace padding.
- `whitespaces`. Number of whitespaces between the alignment character and
the prior FST node. If this is > 1 it signifies additional whitespace was
manually added by the user since the formatter would only use 0 or 1 whitespaces.
"""
struct AlignGroup
nodes::Vector{FST}
node_inds::Vector{Int}
line_offsets::Vector{Int}
lens::Vector{Int}
whitespaces::Vector{Int}
end
AlignGroup() = AlignGroup(FST[], Int[], Int[], Int[], Int[])
function Base.push!(g::AlignGroup, n::FST, ind::Int, line_offset::Int, len::Int, ws::Int)
push!(g.nodes, n)
push!(g.node_inds, ind)
push!(g.line_offsets, line_offset)
push!(g.lens, len)
push!(g.whitespaces, ws)
return
end
function Base.show(io::IO, g::AlignGroup)
println(io, "AlignGroup:")
println(io, " node_inds: ", g.node_inds)
println(io, " line_offsets: ", g.line_offsets)
println(io, " lens: ", g.lens)
println(io, " whitespaces: ", g.whitespaces)
end
function align_to(g::AlignGroup)::Union{Nothing,Int}
if !(length(g.lens) > 0)
return nothing
end
# determine whether alignment might be warranted
max_len, max_ind = findmax(g.lens)
max_inds = findall(==(g.line_offsets[max_ind]), g.line_offsets)
if !(length(max_inds) > 1)
return nothing
end
# Is there custom whitespace?
# Formatter would only add 0 or 1 whitespaces.
# > 2 implies a manual edit in the source file.
for i in max_inds
if g.whitespaces[i] > 1
return max_len
end
end
return nothing
end
function align_binaryopcall!(fst::FST, diff::Int)
# insert whitespace before and after operator
find = findfirst(x -> x.typ === WHITESPACE, fst.nodes)
lind = findlast(x -> x.typ === WHITESPACE, fst.nodes)
if find === nothing
insert!(fst, 2, Whitespace(diff))
else
fst[find] = Whitespace(diff)
end
if lind === nothing
insert!(fst, 4, Whitespace(1))
end
end
# we need to use ncodeunits
function node_align_length(n::FST)
if is_leaf(n)
return ncodeunits(n.val)
end
margin = 0
for nn in n.nodes
margin += node_align_length(nn)
end
return margin
end
function node_align_length(nodes::Vector{FST})
margin = 0
for nn in nodes
margin += node_align_length(nn)
end
return margin
end
"""
align_binaryopcalls!(fst::FST, op_inds::Vector{Int})
Aligns binary operator expressions.
Additionally handles the case where a keyword such as `const` is used
prior to the binary op call.
"""
function align_binaryopcalls!(fst::FST, op_inds::Vector{Int})
if !(length(op_inds) > 1)
return
end
prev_endline = fst[op_inds[1]].endline
groups = AlignGroup[]
g = AlignGroup()
for i in op_inds
n = fst[i]
if n.startline - prev_endline > 1
push!(groups, g)
g = AlignGroup()
end
binop, nlen, ws = if n.typ === Binary || n.typ === Kw
nlen = node_align_length(n[1])
n, nlen, (n[3].line_offset - n.line_offset) - nlen
else
binop::Union{FST,Nothing} = nothing
sn = n
nlen = 0
ws = 0
# the binary op may not be at the top level
# for instance, @call1 @call2 @call3 x = 10
while sn.nodes !== nothing
binop_ind = findfirst(nn -> nn.typ === Binary, sn.nodes)
nlen += node_align_length(sn[1:(end-1)])
if binop_ind === nothing
sn = (sn.nodes::Vector{FST})[end]
else
binop = (sn.nodes::Vector{FST})[binop_ind]
nlen += node_align_length(binop[1])
ws = (binop[3].line_offset - n.line_offset) - nlen
break
end
end
binop, nlen, ws
end
if binop === nothing
@warn "Binary operator not found in node" n.typ
continue
end
push!(g.nodes, binop)
push!(g.node_inds, i)
push!(g.line_offsets, binop[3].line_offset)
push!(g.lens, nlen)
push!(g.whitespaces, ws)
prev_endline = n.endline
end
push!(groups, g)
for g in groups
align_len = align_to(g)
if isnothing(align_len)
continue
end
for (i, n) in enumerate(g.nodes)
diff = align_len - g.lens[i] + 1
align_binaryopcall!(n, diff)
n.nest_behavior = NeverNest
end
end
return
end
"""
align_struct!(fst::FST)
Aligns struct fields.
"""
function align_struct!(fst::FST)
ind = findfirst(n -> n.typ === Block, fst.nodes)
if isnothing(ind) || length(fst[ind]) == 0
return
end
block_fst = fst[ind]
prev_endline = block_fst[1].endline
groups = AlignGroup[]
g = AlignGroup()
for (i, n) in enumerate(block_fst.nodes)
if n.typ === Binary
if n.startline - prev_endline > 1
push!(groups, g)
g = AlignGroup()
end
nlen = node_align_length(n[1])
ind = findfirst(x -> x.typ === OPERATOR, n.nodes)
# issue 757
# "foo
# """
# a::B
#
# This is parsed as a concatenated string of "foo" and ""
if ind === nothing
continue
end
ws = n[ind].line_offset - (n.line_offset + nlen)
push!(g.nodes, n)
push!(g.node_inds, i)
push!(g.line_offsets, n[ind].line_offset)
push!(g.lens, nlen)
push!(g.whitespaces, ws)
prev_endline = n.endline
elseif n.typ === Const && n[end].typ === Binary
if n.startline - prev_endline > 1
push!(groups, g)
g = AlignGroup()
end
nlen = node_align_length(n[1]) + node_align_length(n[2])
binop = n[end]
nlen += node_align_length(binop[1])
ind = findfirst(x -> x.typ === OPERATOR, binop.nodes)
if ind === nothing
continue
end
ws = binop[ind].line_offset - (n.line_offset + nlen)
push!(g.nodes, binop)
push!(g.node_inds, i)
push!(g.line_offsets, binop[ind].line_offset)
push!(g.lens, nlen)
push!(g.whitespaces, ws)
prev_endline = n.endline
end
end
push!(groups, g)
for g in groups
align_len = align_to(g)
if align_len === nothing
continue
end
for (i, n) in enumerate(g.nodes)
diff = align_len - g.lens[i] + 1
align_binaryopcall!(n, diff)
n.nest_behavior = NeverNest
end
end
end
"""
align_conditional!(fst::FST)
Aligns a conditional expression.
"""
function align_conditional!(fst::FST)
nodes = flatten_conditionalopcall(fst)
cond_group = AlignGroup()
cond_prev_endline = 0
colon_group = AlignGroup()
colon_prev_endline = 0
for (i, n) in enumerate(nodes)
if n.typ === OPERATOR && n.val == "?"
if cond_prev_endline != n.endline
nlen = node_align_length(nodes[i-2])
ws = n.line_offset - (nodes[i-2].line_offset + nlen)
push!(cond_group.nodes, n)
push!(cond_group.node_inds, i)
push!(cond_group.line_offsets, n.line_offset)
push!(cond_group.lens, nlen)
push!(cond_group.whitespaces, ws)
end
cond_prev_endline = n.endline
elseif n.typ === OPERATOR && n.val == ":"
if colon_prev_endline != n.endline
nlen = node_align_length(nodes[i-2])
ws = n.line_offset - (nodes[i-2].line_offset + nlen)
push!(colon_group.nodes, n)
push!(colon_group.node_inds, i)
push!(colon_group.line_offsets, n.line_offset)
push!(colon_group.lens, nlen)
push!(colon_group.whitespaces, ws)
end
colon_prev_endline = n.endline
end
end
if !(length(cond_group.lens) > 1)
return
end
cond_len = align_to(cond_group)
colon_len = align_to(colon_group)
if cond_len === nothing && colon_len === nothing
return
end
if cond_len !== nothing
for (i, ind) in enumerate(cond_group.node_inds)
diff = cond_len - cond_group.lens[i] + 1
nodes[ind-1] = Whitespace(diff)
end
end
for (i, ind) in enumerate(colon_group.node_inds)
# the placeholder would be i+1 if not for a possible inline comment
nind = findnext(n -> n.typ === PLACEHOLDER, nodes, ind + 1)::Int
if nodes[nind+1].startline != nodes[nind].startline
nodes[nind] = Newline(; nest_behavior = AlwaysNest)
end
if colon_len !== nothing
diff = colon_len - colon_group.lens[i] + 1
nodes[ind-1] = Whitespace(diff)
end
end
fst.nodes = nodes
fst.nest_behavior = NeverNest
fst.indent = fst.line_offset - 1
return
end
"""
Adjust whitespace in between matrix elements such that it's the same as the original source file.
"""
function align_matrix!(fst::FST)
rows = filter(n -> n.typ === Row, fst.nodes::Vector{FST})
if length(rows) == 0
return
end
min_offset = minimum(map(rows) do r
r[1].line_offset
end)
line = 0
# add whitespace prior to initial element if elements are aligned to the right and
# it's the first row on that line.
for r in rows
if r[1].line_offset > min_offset && line != r.startline
diff = r[1].line_offset - min_offset
if diff > 0
insert!(r.nodes::Vector{FST}, 1, Whitespace(diff))
end
end
line = r.startline
end
for r in rows
for (i, n) in enumerate(r.nodes)
# skip whitespace nodes appearing before initial element
if i > 1 && n.typ === WHITESPACE
n1 = r[i-1]
n2 = r[i+1]
diff = n2.line_offset - n1.line_offset - node_align_length(n1)
# fix #694 and #713
if diff > 0
r[i] = Whitespace(diff)
end
end
end
end
return
end