forked from processing/processing4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocSketch.java
More file actions
291 lines (203 loc) · 8.14 KB
/
PreprocSketch.java
File metadata and controls
291 lines (203 loc) · 8.14 KB
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
package processing.mode.java;
import com.google.classpath.ClassPath;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import processing.app.Problem;
import processing.app.Sketch;
import processing.core.PApplet;
import processing.mode.java.preproc.ImportStatement;
import processing.mode.java.preproc.TextTransform.OffsetMapper;
public class PreprocSketch {
public final Sketch sketch;
public final CompilationUnit compilationUnit;
public final String[] classPathArray;
public final ClassPath classPath;
public final URLClassLoader classLoader;
public final String[] searchClassPathArray;
public final int[] tabStartOffsets;
public final String scrubbedPdeCode;
public final String pdeCode;
public final String javaCode;
public final OffsetMapper offsetMapper;
public final boolean hasSyntaxErrors;
public final boolean hasCompilationErrors;
public final List<ImportStatement> programImports;
public final List<ImportStatement> coreAndDefaultImports;
public final List<ImportStatement> codeFolderImports;
public final List<Problem> otherProblems;
public final List<IProblem> iproblems;
public final Map<String, Integer> javaFileMapping;
/// JAVA -> SKETCH -----------------------------------------------------------
public boolean inRange(SketchInterval interval) {
return interval != SketchInterval.BEFORE_START &&
interval.stopPdeOffset < pdeCode.length();
}
public String getPdeCode(SketchInterval si) {
if (si == SketchInterval.BEFORE_START) return "";
int stop = Math.min(si.stopPdeOffset, pdeCode.length());
int start = Math.min(si.startPdeOffset, stop);
return pdeCode.substring(start, stop);
}
public SketchInterval mapJavaToSketch(ASTNode node) {
return mapJavaToSketch(node.getStartPosition(),
node.getStartPosition() + node.getLength());
}
public boolean isJavaTab(String fileName) {
return javaFileMapping.containsKey(fileName);
}
public int getJavaTabIndex(String fileName) {
return javaFileMapping.get(fileName);
}
public SketchInterval mapJavaToSketch(IProblem iproblem) {
String originalFile = new String(iproblem.getOriginatingFileName());
boolean isJavaTab = javaFileMapping.containsKey(originalFile);
// If is a ".java" tab, do not need to map into combined sketch source.
if (isJavaTab) {
return new SketchInterval(
javaFileMapping.get(originalFile),
iproblem.getSourceStart(),
iproblem.getSourceEnd() + 1,
iproblem.getSourceStart(), // Is outside main sketch code
iproblem.getSourceEnd() + 1 // Is outside main sketch code
);
} else {
return mapJavaToSketch(
iproblem.getSourceStart(),
iproblem.getSourceEnd() + 1 // make it exclusive
);
}
}
public SketchInterval mapJavaToSketch(int startJavaOffset, int stopJavaOffset) {
int length = stopJavaOffset - startJavaOffset;
int startPdeOffset = javaOffsetToPdeOffset(startJavaOffset);
int stopPdeOffset;
if (length == 0) {
stopPdeOffset = startPdeOffset;
} else {
stopPdeOffset = javaOffsetToPdeOffset(stopJavaOffset-1);
if (stopPdeOffset >= 0 && (stopPdeOffset > startPdeOffset || length == 1)) {
stopPdeOffset += 1;
}
}
if (startPdeOffset < 0 || stopPdeOffset < 0) {
return SketchInterval.BEFORE_START;
}
int tabIndex = pdeOffsetToTabIndex(startPdeOffset);
if (startPdeOffset >= pdeCode.length()) {
startPdeOffset = pdeCode.length() - 1;
stopPdeOffset = startPdeOffset + 1;
}
return new SketchInterval(tabIndex,
pdeOffsetToTabOffset(tabIndex, startPdeOffset),
pdeOffsetToTabOffset(tabIndex, stopPdeOffset),
startPdeOffset, stopPdeOffset);
}
private int javaOffsetToPdeOffset(int javaOffset) {
return offsetMapper.getInputOffset(javaOffset);
}
public int pdeOffsetToTabIndex(int pdeOffset) {
pdeOffset = Math.max(0, pdeOffset);
int tab = Arrays.binarySearch(tabStartOffsets, pdeOffset);
if (tab < 0) {
tab = -(tab + 1) - 1;
}
return tab;
}
public int pdeOffsetToTabOffset(int tabIndex, int pdeOffset) {
int tabStartOffset = tabStartOffsets[clipTabIndex(tabIndex)];
return pdeOffset - tabStartOffset;
}
/// SKETCH -> JAVA -----------------------------------------------------------
public int tabOffsetToJavaOffset(int tabIndex, int tabOffset) {
int tabStartOffset = tabStartOffsets[clipTabIndex(tabIndex)];
int pdeOffset = tabStartOffset + tabOffset;
return offsetMapper.getOutputOffset(pdeOffset);
}
/// LINE NUMBERS -------------------------------------------------------------
public int tabOffsetToJavaLine(int tabIndex, int tabOffset) {
int javaOffset = tabOffsetToJavaOffset(tabIndex, tabOffset);
return offsetToLine(javaCode, javaOffset);
}
public int tabOffsetToTabLine(int tabIndex, int tabOffset) {
int tabStartOffset = tabStartOffsets[clipTabIndex(tabIndex)];
return offsetToLine(pdeCode, tabStartOffset, tabStartOffset + tabOffset);
}
// TODO: optimize
private static int offsetToLine(String text, int offset) {
return offsetToLine(text, 0, offset);
}
// TODO: optimize
private static int offsetToLine(String text, int start, int offset) {
int line = 0;
while (offset >= start) {
offset = text.lastIndexOf('\n', offset-1);
line++;
}
return line - 1;
}
/// Util ---------------------------------------------------------------------
private int clipTabIndex(int tabIndex) {
return PApplet.constrain(tabIndex, 0, tabStartOffsets.length - 1);
}
/// BUILDER BUSINESS /////////////////////////////////////////////////////////
/**
* There is a lot of fields and having constructor with this many parameters
* is just not practical. Fill stuff into builder and then simply build it.
* Builder also guards against calling methods in the middle of building process.
*/
public static class Builder {
public Sketch sketch;
public CompilationUnit compilationUnit;
public String[] classPathArray;
public ClassPath classPath;
public URLClassLoader classLoader;
public String[] searchClassPathArray;
public int[] tabStartOffsets = new int[0];
public String scrubbedPdeCode;
public String pdeCode;
public String javaCode;
public OffsetMapper offsetMapper;
public boolean hasSyntaxErrors;
public boolean hasCompilationErrors;
public final List<ImportStatement> programImports = new ArrayList<>();
public final List<ImportStatement> coreAndDefaultImports = new ArrayList<>();
public final List<ImportStatement> codeFolderImports = new ArrayList<>();
public final List<Problem> otherProblems = new ArrayList<>();
public List<IProblem> iproblems;
public Map<String, Integer> javaFileMapping;
public PreprocSketch build() {
return new PreprocSketch(this);
}
}
public static PreprocSketch empty() {
return new Builder().build();
}
private PreprocSketch(Builder b) {
sketch = b.sketch;
compilationUnit = b.compilationUnit;
classPathArray = b.classPathArray;
classPath = b.classPath;
classLoader = b.classLoader;
searchClassPathArray = b.searchClassPathArray;
tabStartOffsets = b.tabStartOffsets;
scrubbedPdeCode = b.scrubbedPdeCode;
pdeCode = b.pdeCode;
javaCode = b.javaCode;
offsetMapper = b.offsetMapper != null ? b.offsetMapper : OffsetMapper.EMPTY_MAPPER;
hasSyntaxErrors = b.hasSyntaxErrors;
hasCompilationErrors = b.hasCompilationErrors;
otherProblems = b.otherProblems;
javaFileMapping = b.javaFileMapping;
iproblems = b.iproblems;
programImports = Collections.unmodifiableList(b.programImports);
coreAndDefaultImports = Collections.unmodifiableList(b.coreAndDefaultImports);
codeFolderImports = Collections.unmodifiableList(b.codeFolderImports);
}
}