-
Notifications
You must be signed in to change notification settings - Fork 70
/
align.jl
301 lines (251 loc) · 8.71 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
function align_fst!(fst::FST, opts::Options)
is_leaf(fst) && return
const_idxs = Int[]
assignment_idxs = Int[]
pair_arrow_idxs = 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_idxs, i)
elseif opts.align_pair_arrow && n.typ === Binary && op_kind(n) === Tokens.PAIR_ARROW
push!(pair_arrow_idxs, i)
end
end
align_binaryopcalls!(fst, assignment_idxs)
align_binaryopcalls!(fst, pair_arrow_idxs)
end
"""
AlignGroup
Group of FST node indices and required metadata to potentially align them.
- `node_idxs`. Indices of 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
node_idxs::Vector{Int}
line_offsets::Vector{Int}
lens::Vector{Int}
whitespaces::Vector{Int}
end
AlignGroup() = AlignGroup(Int[], Int[], Int[], Int[])
function Base.push!(g::AlignGroup, idx::Int, line_offset::Int, len::Int, ws::Int)
push!(g.node_idxs, idx)
push!(g.line_offsets, line_offset)
push!(g.lens, len)
push!(g.whitespaces, ws)
return
end
function align_to(g::AlignGroup)::Union{Nothing,Int}
length(g.lens) > 0 || return nothing
# determine whether alignment might be warranted
max_len, max_idx = findmax(g.lens)
max_idxs = findall(==(g.line_offsets[max_idx]), g.line_offsets)
length(max_idxs) > 1 || return nothing
# 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_idxs
g.whitespaces[i] > 1 && return max_len
end
return nothing
end
function align_binaryopcall!(fst::FST, diff::Int)
# insert whitespace before and after operator
fidx = findfirst(x -> x.typ === WHITESPACE, fst.nodes)
lidx = findlast(x -> x.typ === WHITESPACE, fst.nodes)
if fidx === nothing
insert!(fst, 2, Whitespace(diff))
else
fst[fidx] = Whitespace(diff)
end
if lidx === nothing
insert!(fst, 4, Whitespace(1))
end
end
"""
align_binaryopcalls!(fst::FST, op_idxs::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_idxs::Vector{Int})
length(op_idxs) > 1 || return
prev_endline = fst[op_idxs[1]].endline
groups = AlignGroup[]
g = AlignGroup()
for i in op_idxs
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 = length(n[1])
n, nlen, (n[3].line_offset - n.line_offset) - nlen
else
binop_idx = findfirst(nn -> nn.typ === Binary, n.nodes)
binop = n[binop_idx]
nlen = length(binop[1]) + sum(length.(n[1:binop_idx-1]))
binop, nlen, (binop[3].line_offset - n.line_offset) - nlen
end
push!(g, i, binop[3].line_offset, nlen, ws)
prev_endline = n.endline
end
push!(groups, g)
for g in groups
align_len = align_to(g)
align_len === nothing && continue
for (i, idx) in enumerate(g.node_idxs)
diff = align_len - g.lens[i] + 1
typ = fst[idx].typ
if typ === Binary || typ === Kw
align_binaryopcall!(fst[idx], diff)
else
binop_idx = findfirst(n -> n.typ === Binary, fst[idx].nodes)
align_binaryopcall!(fst[idx][binop_idx], diff)
end
fst[idx].nest_behavior = NeverNest
end
end
return
end
"""
align_struct!(fst::FST)
Aligns struct fields.
"""
function align_struct!(fst::FST)
idx = findfirst(n -> n.typ === Block, fst.nodes)
idx === nothing && return
length(fst[idx]) == 0 && return
block_fst = fst[idx]
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 = length(n[1])
idx = findfirst(x -> x.typ === OPERATOR, n.nodes)
ws = n[idx].line_offset - (n.line_offset + nlen)
push!(g, i, n[idx].line_offset, nlen, ws)
prev_endline = n.endline
end
end
push!(groups, g)
for g in groups
align_len = align_to(g)
align_len === nothing && continue
for (i, idx) in enumerate(g.node_idxs)
diff = align_len - g.lens[i] + 1
align_binaryopcall!(block_fst[idx], diff)
block_fst[idx].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 = length(nodes[i-2])
ws = n.line_offset - (nodes[i-2].line_offset + nlen)
push!(cond_group, i, n.line_offset, nlen, ws)
end
cond_prev_endline = n.endline
elseif n.typ === OPERATOR && n.val == ":"
if colon_prev_endline != n.endline
nlen = length(nodes[i-2])
ws = n.line_offset - (nodes[i-2].line_offset + nlen)
push!(colon_group, i, n.line_offset, nlen, ws)
end
colon_prev_endline = n.endline
end
end
length(cond_group.lens) > 1 || return
cond_len = align_to(cond_group)
colon_len = align_to(colon_group)
cond_len === nothing && colon_len === nothing && return
if cond_len !== nothing
for (i, idx) in enumerate(cond_group.node_idxs)
diff = cond_len - cond_group.lens[i] + 1
nodes[idx-1] = Whitespace(diff)
end
end
for (i, idx) in enumerate(colon_group.node_idxs)
# the placeholder would be i+1 if not for a possible inline comment
nidx = findnext(n -> n.typ === PLACEHOLDER, nodes, idx + 1)
if nodes[nidx+1].startline != nodes[nidx].startline
nodes[nidx] = Newline(nest_behavior = AlwaysNest)
end
if colon_len !== nothing
diff = colon_len - colon_group.lens[i] + 1
nodes[idx-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)
length(rows) == 0 && return
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
insert!(r.nodes, 1, Whitespace(diff))
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 - length(n1)
r[i] = Whitespace(diff)
end
end
end
return
end