/
PrinterHelper.java
311 lines (274 loc) · 7.01 KB
/
PrinterHelper.java
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
/*
* SPDX-License-Identifier: (MIT OR CECILL-C)
*
* Copyright (C) 2006-2023 INRIA and contributors
*
* Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
*/
package spoon.reflect.visitor;
import spoon.compiler.Environment;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtCompilationUnit;
import spoon.reflect.declaration.CtElement;
import spoon.support.reflect.cu.position.PartialSourcePositionImpl;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Supports configurable printing of text with indentations and line and column counting
*/
public class PrinterHelper {
/**
* Line separator which is used by the printer helper.
* By default the system line separator is used
*/
private String lineSeparator = System.getProperty("line.separator");
/**
* Environment which Spoon is executed.
*/
private Environment env;
/**
* The string buffer in which the code is generated.
*/
protected final StringBuffer sbf = new StringBuffer();
/**
* Number of tabs when we print the source code.
*/
private int nbTabs = 0;
/**
* Current line number.
*/
private int line = 1;
/**
* Mapping for line numbers.
*/
private Map<Integer, Integer> lineNumberMapping = new HashMap<>();
/*
* each writeln() sets this to true.
* if true then first call of write first writes tabs and then resets this to false
*/
protected boolean shouldWriteTabs = true;
/*
* true if last written character was \r
* It helps to detect windows EOL, which is \r\n
*/
private boolean lastCharWasCR = false;
boolean prefixBlockComments;
public PrinterHelper() {
}
public PrinterHelper(Environment env) {
this.env = env;
}
/**
* resets to the initial state
*/
public void reset() {
sbf.setLength(0);
nbTabs = 0;
line = 1;
shouldWriteTabs = true;
//create new map, because clients keeps reference to it
lineNumberMapping = new HashMap<>();
}
/**
* Outputs a string.
*/
public PrinterHelper write(String s) {
if (s != null) {
int len = s.length();
for (int i = 0; i < len; i++) {
write(s.charAt(i));
}
}
return this;
}
/**
* Outputs a char.
*/
public PrinterHelper write(char c) {
if (c == '\r') {
sbf.append(c);
line++;
shouldWriteTabs = true;
lastCharWasCR = true;
return this;
}
if (c == '\n') {
sbf.append(c);
if (lastCharWasCR) {
//increment line only once in sequence of \r\n.
//last was \r, so nothing to do
} else {
//increment line only once in sequence of \r\n.
//last was NOT \r, so do it now
line++;
shouldWriteTabs = true;
}
lastCharWasCR = false;
return this;
}
autoWriteTabs();
sbf.append(c);
lastCharWasCR = false;
return this;
}
/**
* Generates a new line.
*/
public PrinterHelper writeln() {
write(lineSeparator);
return this;
}
private void writeTabsInternal() {
for (int i = 0; i < nbTabs; i++) {
if (env != null && env.isUsingTabulations()) {
sbf.append('\t');
} else {
int indentationSize = 2;
if (env != null) {
indentationSize = env.getTabulationSize();
}
sbf.append(" ".repeat(indentationSize));
}
}
}
protected void autoWriteTabs() {
if (shouldWriteTabs) {
writeTabsInternal();
shouldWriteTabs = false;
}
}
/**
* Increments the current number of tabs.
*/
public PrinterHelper incTab() {
nbTabs++;
return this;
}
/**
* Decrements the current number of tabs.
*/
public PrinterHelper decTab() {
nbTabs--;
return this;
}
/**
* @return the current number of tabs.
*/
public int getTabCount() {
return nbTabs;
}
/**
* Sets the current number of tabs.
*/
public PrinterHelper setTabCount(int tabCount) {
nbTabs = tabCount;
return this;
}
public boolean removeLine() {
String ls = lineSeparator;
int i = sbf.length() - ls.length();
boolean hasWhite = false;
while (i > 0 && !ls.equals(sbf.substring(i, i + ls.length()))) {
if (!isWhite(sbf.charAt(i))) {
return false;
}
hasWhite = true;
i--;
}
if (i <= 0) {
return false;
}
hasWhite = hasWhite || isWhite(sbf.charAt(i - 1));
sbf.replace(i, i + ls.length(), hasWhite ? "" : " ");
line--;
return true;
}
private boolean isWhite(char c) {
return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
}
/** writes as many newlines as needed to align the line number again between the element position and the current line number */
public PrinterHelper adjustStartPosition(CtElement e) {
if (!e.isImplicit() && e.getPosition().isValidPosition()) {
// we should add some lines
while (line < e.getPosition().getLine()) {
writeln();
}
// trying to remove some lines
while (line > e.getPosition().getLine()) {
if (!removeLine()) {
break;
}
}
}
return this;
}
/**
* writes as many newlines as needed for the current line to match the end line of the passed element
* @param e element whose line number will be preserved by adding newlines
* @return PrinterHelper
*/
public PrinterHelper adjustEndPosition(CtElement e) {
if (env != null && env.isPreserveLineNumbers() && e.getPosition().isValidPosition()) {
// let's add lines if required
while (line < e.getPosition().getEndLine()) {
writeln();
}
}
return this;
}
public void undefineLine() {
if (lineNumberMapping.get(line) == null) {
putLineNumberMapping(0);
}
}
public void mapLine(CtElement e, CtCompilationUnit unitExpected) {
SourcePosition sp = e.getPosition();
if ((sp.isValidPosition())
&& (sp.getCompilationUnit() == unitExpected)
&& !(sp instanceof PartialSourcePositionImpl)) {
// only map elements coming from the source CU
putLineNumberMapping(e.getPosition().getLine());
} else {
undefineLine();
}
}
public void putLineNumberMapping(int valueLine) {
lineNumberMapping.put(this.line, valueLine);
}
public Map<Integer, Integer> getLineNumberMapping() {
return Collections.unmodifiableMap(lineNumberMapping);
}
@Override
public String toString() {
return sbf.toString();
}
/**
* @return current line separator. By default there is CR LF, LF or CR depending on the Operation system
* defined by System.getProperty("line.separator")
*/
public String getLineSeparator() {
return lineSeparator;
}
/**
* @param lineSeparator characters which will be printed as End of line.
* By default there is System.getProperty("line.separator")
*/
public void setLineSeparator(String lineSeparator) {
this.lineSeparator = lineSeparator;
}
/** writes a space ' ' */
public void writeSpace() {
this.write(' ');
}
public void setShouldWriteTabs(boolean b) {
this.shouldWriteTabs = b;
}
/**
* Sets whether lines in block comments should be prefixed by a {@code *}.
* @param prefixBlockComments whether block comments should be prefixed.
*/
public void setPrefixBlockComments(boolean prefixBlockComments) {
this.prefixBlockComments = prefixBlockComments;
}
}