-
Notifications
You must be signed in to change notification settings - Fork 3
/
cpp-align.c
282 lines (244 loc) · 5.7 KB
/
cpp-align.c
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
#include "cpp-align.h"
#include "config.h"
#include <ctype.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/buffer.h"
#include "doc.h"
#include "ruler.h"
#include "style.h"
#include "token.h"
#include "trace-types.h"
#include "trace.h"
#include "util.h"
#define cpp_trace(op, fmt, ...) trace(TRACE_CPP, (op), (fmt), __VA_ARGS__)
enum indent_type {
INDENT_TYPE_NONE,
INDENT_TYPE_SPACES,
INDENT_TYPE_TABS,
};
struct alignment {
const char *indent;
enum indent_type indent_type;
enum style_keyword mode;
unsigned int width;
unsigned int tabs:1,
skip_first_line:1;
};
static enum indent_type
classify_indent(const char *str)
{
if (str[0] == '\t')
return INDENT_TYPE_TABS;
if (str[0] == ' ')
return INDENT_TYPE_SPACES;
return INDENT_TYPE_NONE;
}
/*
* Returns a pointer to the end of current line assuming it's a line
* continuation.
*/
static const char *
nextline(const char *str, size_t len, const char **nx)
{
const char *p;
p = memchr(str, '\n', len);
if (p == NULL || p == str || p[-1] != '\\')
return NULL;
*nx = &p[1];
p--; /* consume '\\' */
while (p > str && isspace((unsigned char)p[-1]))
p--;
return p;
}
static int
is_not_aligned(const struct alignment *a)
{
return strncmp(a->indent, " \\", 2) == 0;
}
static int
all_identical(const struct alignment *a, unsigned int len)
{
unsigned int i;
for (i = 0; i < len - 1; i++) {
if (a[i].width != a[i + 1].width)
return 0;
if (a[i].indent_type == INDENT_TYPE_NONE ||
a[i + 1].indent_type == INDENT_TYPE_NONE)
continue;
if (a[i].indent_type != a[i + 1].indent_type)
return 0;
}
return 1;
}
static int
all_not_aligned(const struct alignment *a, unsigned int len)
{
unsigned int i;
for (i = 0; i < len; i++) {
if (!is_not_aligned(&a[i]))
return 0;
}
return 1;
}
static int
all_tabs(const struct alignment *a, unsigned int len)
{
unsigned int i;
for (i = 0; i < len; i++) {
if (a[i].indent_type != INDENT_TYPE_NONE &&
a[i].indent_type != INDENT_TYPE_TABS)
return 0;
}
return 1;
}
static int
sense_alignment(const char *str, size_t len, const struct style *st,
struct alignment *alignment)
{
struct alignment lines[3] = {0};
unsigned int maxcol = style(st, ColumnLimit);
unsigned int nlines = 0;
unsigned int i;
for (i = 0; i < sizeof(lines) / sizeof(lines[0]); i++) {
const char *indent, *nx;
size_t linelen;
unsigned int col;
indent = nextline(str, len, &nx);
if (indent == NULL)
break;
linelen = (size_t)(nx - str);
/* Require trailing '\\' '\n' characters. */
if (linelen < 2)
return 0;
col = colwidth(str, linelen - 1, 1);
if (col > maxcol)
return 0;
lines[i].indent = indent;
lines[i].indent_type = classify_indent(indent);
lines[i].width = col - 2;
len -= linelen;
str += linelen;
nlines++;
}
if (all_not_aligned(lines, nlines)) {
*alignment = (struct alignment){.mode = DontAlign};
return 1;
}
/* The first line is allowed to not be aligned. */
if (nlines >= 3 && all_identical(&lines[1], nlines - 1)) {
*alignment = (struct alignment){
.mode = Right,
.width = lines[nlines - 1].width,
.tabs = all_tabs(lines, nlines) ? 1 : 0,
.skip_first_line = is_not_aligned(&lines[0]) ? 1 : 0,
};
return 1;
}
return 0;
}
/*
* Align line continuations.
*/
struct buffer *
cpp_align(struct token *tk, const struct style *st, struct arena_scope *s,
struct arena *scratch, const struct options *op)
{
struct alignment alignment = {
.mode = style(st, AlignEscapedNewlines),
.width = style(st, ColumnLimit) - style(st, IndentWidth),
.tabs = style_use_tabs(st) ? 1 : 0,
};
struct ruler rl;
struct buffer *out = NULL;
struct buffer *bf;
struct doc *dc;
const char *nx, *str;
size_t len;
int nlines = 0;
str = tk->tk_str;
len = tk->tk_len;
if (nextline(str, len, &nx) == NULL)
return NULL;
if (sense_alignment(str, len, st, &alignment)) {
cpp_trace(op, "mode %s, width %u, tabs %d, skip %d",
style_keyword_str(alignment.mode), alignment.width,
alignment.tabs ? 1 : 0,
alignment.skip_first_line ? 1 : 0);
}
switch (alignment.mode) {
case DontAlign:
ruler_init(&rl, 1, RULER_ALIGN_FIXED);
break;
case Left:
ruler_init(&rl, 0,
alignment.tabs ? RULER_ALIGN_TABS : RULER_ALIGN_MIN);
break;
case Right:
ruler_init(&rl, alignment.width,
RULER_ALIGN_MAX | (alignment.tabs ? RULER_ALIGN_TABS : 0));
break;
default:
return NULL;
}
arena_scope(scratch, scratch_scope);
bf = arena_buffer_alloc(s, len);
dc = doc_root(&scratch_scope);
for (;;) {
struct doc *concat;
const char *ep, *sp;
size_t cpplen, linelen;
unsigned int w;
concat = doc_alloc(DOC_CONCAT, dc);
sp = str;
ep = nextline(sp, len, &nx);
if (ep == NULL)
break;
cpplen = (size_t)(ep - sp);
if (cpplen > 0) {
const char *literal;
literal = arena_strndup(&scratch_scope, sp, cpplen);
doc_literal(literal, concat);
}
buffer_reset(bf);
w = doc_width(&(struct doc_exec_arg){
.dc = concat,
.scratch = scratch,
.bf = bf,
.st = st,
.op = op,
});
if (nlines == 0 && alignment.skip_first_line)
doc_literal(" ", concat);
else
ruler_insert(&rl, tk, concat, 1, w, 0);
doc_literal("\\", concat);
doc_alloc(DOC_HARDLINE, concat);
linelen = (size_t)(nx - str);
len -= linelen;
str += linelen;
nlines++;
}
if (len > 0) {
const char *literal;
literal = arena_strndup(&scratch_scope, str, len);
doc_literal(literal, dc);
}
/* Alignment only wanted for multiple lines. */
if (nlines <= 1)
goto out;
ruler_exec(&rl);
buffer_reset(bf);
doc_exec(&(struct doc_exec_arg){
.dc = dc,
.scratch = scratch,
.bf = bf,
.st = st,
.op = op,
});
out = bf;
out:
ruler_free(&rl);
return out;
}