-
Notifications
You must be signed in to change notification settings - Fork 317
/
TreeAppendable.java
650 lines (574 loc) · 19.9 KB
/
TreeAppendable.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*******************************************************************************
* Copyright (c) 2012, 2022 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.xbase.compiler.output;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.generator.trace.AbstractTraceRegion;
import org.eclipse.xtext.generator.trace.ILocationData;
import org.eclipse.xtext.generator.trace.ITraceURIConverter;
import org.eclipse.xtext.generator.trace.LocationData;
import org.eclipse.xtext.generator.trace.SourceRelativeURI;
import org.eclipse.xtext.resource.ILocationInFileProvider;
import org.eclipse.xtext.resource.ILocationInFileProviderExtension;
import org.eclipse.xtext.util.IAcceptor;
import org.eclipse.xtext.util.ITextRegion;
import org.eclipse.xtext.util.ITextRegionWithLineInformation;
import org.eclipse.xtext.util.TextRegionWithLineInformation;
import org.eclipse.xtext.xbase.compiler.GeneratorConfig;
import org.eclipse.xtext.xbase.compiler.ImportManager;
import org.eclipse.xtext.xbase.jvmmodel.IJvmModelAssociations;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference;
import org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReferenceSerializer;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
* @author Sebastian Zarnekow - Initial contribution and API
* @author Lorenzo Bettini - blankLine
*/
public class TreeAppendable implements ITreeAppendable, IAcceptor<String>, CharSequence {
private static final Logger log = Logger.getLogger(TreeAppendable.class);
/**
* A {@link org.eclipse.xtext.xbase.compiler.output.TreeAppendable.Visitor visitor} can be used
* to manipulate an existing {@link TreeAppendable} or to create a completely new one recursively.
* Implementors may override {@link #visit(String)} and {@link #visit(TreeAppendable)}
*/
public abstract static class Visitor {
/**
* Manipulate the given appendable or return a new one.
* @param original the visited {@link TreeAppendable}
* @return the original appendable or a new one.
*/
protected TreeAppendable visit(TreeAppendable original) {
visitChildren(original);
return original;
}
/**
* Traverses the children of the given appendable and manipulates it in-place.
* @param parent the appendable whose children should be visited.
*/
protected void visitChildren(TreeAppendable parent) {
for (int i = 0; i < parent.children.size(); i++) {
Object o = parent.children.get(i);
if (o instanceof String) {
parent.children.set(i, visit((String) o));
} else {
parent.children.set(i, visit((TreeAppendable) o));
}
}
}
/**
* Manipulate the given string and return the result.
* @param string the visited {@link String}
* @return the original string or a new one.
*/
protected String visit(/* @NonNull */ String string) {
return string;
}
}
private final List<Object> children;
private final SharedAppendableState state;
private final ILocationInFileProvider locationProvider;
private final IJvmModelAssociations jvmModelAssociations;
private final Set<ILocationData> locationData;
private final ITraceURIConverter traceURIConverter;
private boolean closed = false;
private boolean useForDebugging = false;
private LightweightTypeReferenceSerializer lightweightTypeReferenceSerializer;
public TreeAppendable(ImportManager importManager, ITraceURIConverter converter, ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, EObject source,
String indentation, String lineSeparator) {
this(
new SharedAppendableState(indentation, lineSeparator, importManager, source.eResource()),
converter,
locationProvider,
jvmModelAssociations,
source);
}
protected TreeAppendable(SharedAppendableState state, ITraceURIConverter converter, ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, EObject source) {
this(
state,
converter,
locationProvider,
jvmModelAssociations,
createAllLocationData(
converter,
locationProvider,
jvmModelAssociations,
source,
ILocationInFileProviderExtension.RegionDescription.INCLUDING_COMMENTS,
false), // don't skip empty root regions
false);
}
protected TreeAppendable(SharedAppendableState state,
final ITraceURIConverter converter,
ILocationInFileProvider locationProvider,
IJvmModelAssociations jvmModelAssociations,
Set<ILocationData> sourceLocations,
boolean useForDebugging) {
this.state = state;
this.traceURIConverter = converter;
this.locationProvider = locationProvider;
this.jvmModelAssociations = jvmModelAssociations;
this.children = Lists.newArrayList();
this.locationData = sourceLocations;
this.useForDebugging = useForDebugging;
this.lightweightTypeReferenceSerializer = createLightweightTypeReferenceSerializer();
}
protected LightweightTypeReferenceSerializer createLightweightTypeReferenceSerializer() {
return new LightweightTypeReferenceSerializer(this);
}
@Override
public boolean isJava() {
return true;
}
public ITraceURIConverter getTraceURIConverter() {
return traceURIConverter;
}
/**
* @since 2.4
*/
@Override
public ErrorTreeAppendable errorChild() {
ErrorTreeAppendable errorChild = new ErrorTreeAppendable(state, traceURIConverter, locationProvider, jvmModelAssociations, getLocationData(), useForDebugging);
children.add(errorChild);
return errorChild;
}
@Override
public TreeAppendable trace(EObject object) {
return trace(object, false);
}
@Override
public TreeAppendable trace(EObject object, boolean useForDebugging) {
return trace(object, ILocationInFileProviderExtension.RegionDescription.FULL, useForDebugging);
}
public TreeAppendable trace(EObject object, ILocationInFileProviderExtension.RegionDescription region, boolean useForDebugging) {
// TODO use locationProvider from service registry
Set<ILocationData> locationData = createAllLocationData(traceURIConverter, locationProvider, jvmModelAssociations, object, region);
if (locationData.isEmpty())
return this;
return trace(locationData, useForDebugging);
}
protected TreeAppendable trace(Set<ILocationData> newData, boolean useForDebugging) {
if (this.useForDebugging == useForDebugging && newData.equals(locationData)) {
return this;
}
TreeAppendable result = createChild(state, locationProvider, jvmModelAssociations, newData, useForDebugging);
if(result != this)
children.add(result);
return result;
}
/**
* @since 2.4
*/
protected TreeAppendable createChild(SharedAppendableState state, ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, Set<ILocationData> newData, boolean useForDebugging) {
return new TreeAppendable(state, traceURIConverter, locationProvider, jvmModelAssociations, newData, useForDebugging);
}
@Override
public ITreeAppendable trace(ILocationData location) {
return trace(location, false);
}
@Override
public ITreeAppendable trace(ILocationData location, boolean useForDebugging) {
return trace(Collections.singleton(location), useForDebugging);
}
/* @Nullable */
protected static ILocationData createLocationData(ITraceURIConverter converter, ILocationInFileProvider locationProvider, EObject object, ILocationInFileProviderExtension.RegionDescription query) {
return createLocationData(converter, locationProvider, object, query, true);
}
/* @Nullable */
private static ILocationData createLocationData(ITraceURIConverter converter, ILocationInFileProvider locationProvider, EObject object, ILocationInFileProviderExtension.RegionDescription query, boolean skipEmpty) {
ITextRegion textRegion = locationProvider instanceof ILocationInFileProviderExtension ?
((ILocationInFileProviderExtension) locationProvider).getTextRegion(object, query) : locationProvider.getFullTextRegion(object);
if (!(textRegion instanceof ITextRegionWithLineInformation)) {
if (log.isDebugEnabled())
log.debug("location provider returned text region without line information.", new Exception());
if (textRegion != null)
textRegion = new TextRegionWithLineInformation(textRegion.getOffset(), textRegion.getLength(), 0, 0);
else
return null;
}
// usually we want to skip empty regions but if the root region is empty, we want to use it to store the path information along
// with the empty offset / length pair
if (skipEmpty && textRegion == ITextRegion.EMPTY_REGION) {
return null;
}
ILocationData newData = createLocationData(converter, object, (ITextRegionWithLineInformation) textRegion);
return newData;
}
protected static Set<ILocationData> createAllLocationData(ITraceURIConverter converter, ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, EObject object, ILocationInFileProviderExtension.RegionDescription query, boolean skipEmpty) {
Set<EObject> sourceElements = jvmModelAssociations.getSourceElements(object);
Set<ILocationData> result = Collections.emptySet();
if (sourceElements.isEmpty()) {
ILocationData locationData = createLocationData(converter, locationProvider, object, query, skipEmpty);
if (locationData != null) {
result = Collections.singleton(locationData);
}
} else {
result = Sets.newHashSet();
for(EObject sourceElement: sourceElements) {
ILocationData locationData = createLocationData(converter, locationProvider, sourceElement, query, skipEmpty);
if (locationData != null) {
result.add(locationData);
}
}
}
return result;
}
private static Set<ILocationData> createAllLocationData(ITraceURIConverter converter, ILocationInFileProvider locationProvider, IJvmModelAssociations jvmModelAssociations, EObject object, ILocationInFileProviderExtension.RegionDescription query) {
return createAllLocationData(converter, locationProvider, jvmModelAssociations, object, query, true);
}
@Override
public ITreeAppendable trace(Iterable<? extends EObject> objects) {
if (Iterables.isEmpty(objects))
throw new IllegalArgumentException("List of objects may not be empty");
int size = Iterables.size(objects);
if (size == 1)
return trace(objects.iterator().next(), false);
Set<ILocationData> newData = new LinkedHashSet<ILocationData>(size);
for(EObject object: objects) {
ILocationData locationData = createLocationData(traceURIConverter, locationProvider, object, ILocationInFileProviderExtension.RegionDescription.FULL);
if (locationData != null)
newData.add(locationData);
}
if (newData.isEmpty() && !isUseForDebugging())
return this;
return trace(newData, false);
}
@Override
public ITreeAppendable trace(EObject object, EStructuralFeature feature, int indexInList) {
ITextRegion textRegion = locationProvider.getFullTextRegion(object, feature, indexInList);
if (!(textRegion instanceof ITextRegionWithLineInformation)) {
if (log.isDebugEnabled())
log.debug("location provider returned text region without line information. Synthesized dummy data.", new Exception());
textRegion = new TextRegionWithLineInformation(textRegion.getOffset(), textRegion.getLength(), 0, 0);
}
if (textRegion == ITextRegion.EMPTY_REGION) {
return this;
}
ILocationData newData = createLocationData(traceURIConverter, object, (ITextRegionWithLineInformation) textRegion);
return trace(Collections.singleton(newData), false);
}
protected static ILocationData createLocationData(ITraceURIConverter converter, EObject object, ITextRegionWithLineInformation textRegion) {
Resource resource = object.eResource();
SourceRelativeURI uriForTrace = null;
if (converter != null) {
uriForTrace = converter.getURIForTrace(resource);
}
ILocationData newData = new LocationData(textRegion, uriForTrace);
return newData;
}
public TreeAppendable acceptVisitor(TreeAppendable.Visitor visitor) {
return visitor.visit(this);
}
public Set<ILocationData> getLocationData() {
return locationData;
}
/**
* Access the children of the {@link TreeAppendable}. The list contains either {@link String strings}
* or other {@link TreeAppendable TreeAppendables}. The list may be empty.
* @return the children of this appendable.
*/
public List<? extends Object> getChildren() {
return children;
}
/**
* @noreference This method is not intended to be referenced by clients.
*/
@Override
public void accept(/* @Nullable */ String text) {
children.add(text);
}
protected void markClosed() {
if (closed)
return;
closeLastChild();
closed = true;
}
protected void closeLastChild() {
if (closed) {
throw new IllegalStateException("TreeAppendable was already closed");
}
if (!children.isEmpty()) {
Object lastChild = children.get(children.size() - 1);
if (lastChild instanceof TreeAppendable) {
((TreeAppendable) lastChild).markClosed();
}
}
}
protected boolean isClosed() {
return closed;
}
@Override
public TreeAppendable append(JvmType type) {
closeLastChild();
state.appendType(type, this);
return this;
}
@Override
public ITreeAppendable append(Class<?> type) {
closeLastChild();
state.appendType(type, this);
return this;
}
@Override
public ITreeAppendable append(LightweightTypeReference typeRef) {
typeRef.accept(lightweightTypeReferenceSerializer);
return this;
}
protected ITreeAppendable appendTreeAppendable(ITreeAppendable other) {
closeLastChild();
if (other instanceof TreeAppendable) {
((TreeAppendable) other).markClosed();
} else {
// TODO improve
throw new IllegalArgumentException("Unexpected implementation");
}
children.add(other);
return this;
}
@Override
public ITreeAppendable append(CharSequence content) {
if (content instanceof ITreeAppendable) {
return appendTreeAppendable((ITreeAppendable)content);
}
closeLastChild();
if (content == null) {
appendIndented("null");
log.warn("null was passed to treeappendable", new NullPointerException());
} else {
appendIndented(content.toString());
}
return this;
}
public TreeAppendable appendUnsafe(String string) {
accept(string);
return this;
}
@Override
public TreeAppendable newLine() {
closeLastChild();
state.appendNewLineAndIndentation(this);
return this;
}
/**
* Inserts a blank line, without indentation
*
* @since 2.27
*/
@Override
public ITreeAppendable blankLine() {
closeLastChild();
state.appendNewLine(this);
return this;
}
@Override
public TreeAppendable increaseIndentation() {
closeLastChild();
state.increaseIndentation();
return this;
}
@Override
public TreeAppendable decreaseIndentation() {
closeLastChild();
state.decreaseIndentation();
return this;
}
@Override
@Deprecated
public List<String> getImports() {
return state.getImports();
}
@Override
public void openScope() {
state.openScope();
}
@Override
public void openPseudoScope() {
state.openPseudoScope();
}
@Override
public String declareVariable(Object key, String proposedName) {
return state.declareVariable(key, proposedName);
}
@Override
public String declareSyntheticVariable(Object key, String proposedName) {
return state.declareSyntheticVariable(key, proposedName);
}
@Override
public String declareUniqueNameVariable(Object key, String proposedName) {
return state.declareUniqueNameVariable(key, proposedName);
}
@Override
public String removeName(Object key) {
return state.removeName(key);
}
@Override
public String getName(Object key) {
return state.getName(key);
}
@Override
public boolean hasName(Object key) {
return state.hasName(key);
}
@Override
public Object getObject(String name) {
return state.getObject(name);
}
@Override
public boolean hasObject(String name) {
return state.hasObject(name);
}
@Override
public void closeScope() {
state.closeScope();
}
@Override
public String getContent() {
StringBuilder result = new StringBuilder(8 * 1024);
doGetContent(result);
return result.toString();
}
@Override
public char charAt(int index) {
return toString().charAt(index);
}
@Override
public CharSequence subSequence(int start, int end) {
return toString().subSequence(start, end);
}
@Override
public String toString() {
return getContent();
}
protected void doGetContent(StringBuilder result) {
for (Object child : children) {
if (child instanceof String) {
result.append(child);
} else {
((TreeAppendable) child).doGetContent(result);
}
}
}
@Override
public int length() {
return toString().length();
}
@Override
public AbstractTraceRegion getTraceRegion() {
if (locationData == null) {
throw new IllegalStateException("tree appendable was used without tracing");
}
return new AppendableBasedTraceRegion(null, this, 0, 0);
}
protected void appendIndented(String text) {
int length = text.length();
int nextLineOffset = 0;
int idx = 0;
while (idx < length) {
char currentChar = text.charAt(idx);
// check for \r or \r\n
if (currentChar == '\r') {
int delimiterLength = 1;
if (idx + 1 < length && text.charAt(idx + 1) == '\n') {
delimiterLength++;
idx++;
}
int lineLength = idx - delimiterLength - nextLineOffset + 1;
children.add(text.substring(nextLineOffset, nextLineOffset + lineLength));
state.appendNewLineAndIndentation(this);
nextLineOffset = idx + 1;
} else if (currentChar == '\n') {
int lineLength = idx - nextLineOffset;
children.add(text.substring(nextLineOffset, nextLineOffset + lineLength));
state.appendNewLineAndIndentation(this);
nextLineOffset = idx + 1;
}
idx++;
}
if (nextLineOffset != length) {
int lineLength = length - nextLineOffset;
children.add(text.substring(nextLineOffset, nextLineOffset + lineLength));
}
}
public boolean isUseForDebugging() {
if (useForDebugging) {
// HACK only count if it has 'direct' content not just sub trees.
for (Object c : children) {
if (!(c instanceof ITreeAppendable))
return true;
}
}
return false;
}
/**
* @since 2.4
*/
public SharedAppendableState getState() {
return state;
}
/**
* @since 2.34
*/
public ImportManager getImportManager() {
return state.getImportManager();
}
String getLineSeparator() {
return state.getLineSeparator();
}
String getIndentationString() {
return state.getIndentationString();
}
/**
* @nooverride This method is not intended to be re-implemented or extended by clients.
* @noreference This method is not intended to be referenced by clients.
* @since 2.4
*/
public void dump() {
dump("");
System.out.println();
}
/**
* @nooverride This method is not intended to be re-implemented or extended by clients.
* @noreference This method is not intended to be referenced by clients.
* @since 2.4
*/
protected void dump(String indent) {
if(closed)
System.out.println(indent + "<closed>");
StringBuilder currentChildren = null;
for(Object child: children) {
if(child instanceof TreeAppendable) {
if(currentChildren != null)
System.out.println(indent + currentChildren.toString());
currentChildren = null;
((TreeAppendable) child).dump(" " + indent);
}
else {
if(currentChildren == null)
currentChildren = new StringBuilder();
currentChildren.append(child.toString().replace(state.getLineSeparator(), "\\n"));
}
}
if(currentChildren != null)
System.out.println(indent + currentChildren.toString());
if(closed)
System.out.println(indent + "</closed>");
}
@Override
public GeneratorConfig getGeneratorConfig() {
return state.getGeneratorConfig();
}
}