-
Notifications
You must be signed in to change notification settings - Fork 23
/
align.ts
109 lines (90 loc) · 2.62 KB
/
align.ts
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
import { Transform } from './index.js';
import { BlockMarkers, Block, Line, Markers } from '../primitives.js';
import { rewireSource } from '../util.js';
interface Width {
start: number;
tag: number;
type: number;
name: number;
}
const zeroWidth = {
start: 0,
tag: 0,
type: 0,
name: 0,
};
const getWidth =
(markers = Markers) =>
(w: Width, { tokens: t }: Line) => ({
start: t.delimiter === markers.start ? t.start.length : w.start,
tag: Math.max(w.tag, t.tag.length),
type: Math.max(w.type, t.type.length),
name: Math.max(w.name, t.name.length),
});
const space = (len: number) => ''.padStart(len, ' ');
export default function align(markers = Markers): Transform {
let intoTags = false;
let w: Width;
function update(line: Line): Line {
const tokens = { ...line.tokens };
if (tokens.tag !== '') intoTags = true;
const isEmpty =
tokens.tag === '' &&
tokens.name === '' &&
tokens.type === '' &&
tokens.description === '';
// dangling '*/'
if (tokens.end === markers.end && isEmpty) {
tokens.start = space(w.start + 1);
return { ...line, tokens };
}
switch (tokens.delimiter) {
case markers.start:
tokens.start = space(w.start);
break;
case markers.delim:
tokens.start = space(w.start + 1);
break;
default:
tokens.delimiter = '';
tokens.start = space(w.start + 2); // compensate delimiter
}
if (!intoTags) {
tokens.postDelimiter = tokens.description === '' ? '' : ' ';
return { ...line, tokens };
}
const nothingAfter = {
delim: false,
tag: false,
type: false,
name: false,
};
if (tokens.description === '') {
nothingAfter.name = true;
tokens.postName = '';
if (tokens.name === '') {
nothingAfter.type = true;
tokens.postType = '';
if (tokens.type === '') {
nothingAfter.tag = true;
tokens.postTag = '';
if (tokens.tag === '') {
nothingAfter.delim = true;
}
}
}
}
tokens.postDelimiter = nothingAfter.delim ? '' : ' ';
if (!nothingAfter.tag)
tokens.postTag = space(w.tag - tokens.tag.length + 1);
if (!nothingAfter.type)
tokens.postType = space(w.type - tokens.type.length + 1);
if (!nothingAfter.name)
tokens.postName = space(w.name - tokens.name.length + 1);
return { ...line, tokens };
}
return ({ source, ...fields }: Block): Block => {
w = source.reduce(getWidth(markers), { ...zeroWidth });
return rewireSource({ ...fields, source: source.map(update) });
};
}