use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.
the class AbstractTraceForURIProvider method getTraceToSource.
public Trace getTraceToSource(final SomeFile generatedFile) {
final PersistedTrace persistedTrace = findPersistedTrace(generatedFile);
if (persistedTrace == null || !persistedTrace.exists()) {
return null;
}
Trace result = newAbstractTrace(generatedFile);
result.setTraceToSource(true);
result.setTraceRegionProvider(new ITraceRegionProvider() {
@Override
public AbstractTraceRegion getTraceRegion() {
AbstractTraceRegion traceRegion = cachedTraces.getTraceRegion(persistedTrace);
if (traceRegion == null) {
throw new TraceNotFoundException();
}
return traceRegion;
}
});
return result;
}
use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-xtend by eclipse.
the class LineNumberMappingTests method assertLineNumbers.
public void assertLineNumbers(final CharSequence xtendCodeWithLineNumbers) {
final AbstractTraceRegion region = this.getTraceRegion(xtendCodeWithLineNumbers);
final List<LineMappingProvider.LineMapping> normalizedMappings = this.lineMappingProvider.getLineMapping(region);
final String[] lines = xtendCodeWithLineNumbers.toString().split("\n");
int _size = ((List<String>) Conversions.doWrapArray(lines)).size();
int _minus = (_size - 1);
IntegerRange _upTo = new IntegerRange(0, _minus);
for (final Integer lineNumber : _upTo) {
{
final LineMappingProvider.LineMapping mapping = this.findMapping(normalizedMappings, lineNumber);
final String line = lines[(lineNumber).intValue()];
if ((mapping != null)) {
int _indexOf = line.indexOf("//");
boolean _equals = (_indexOf == (-1));
if (_equals) {
StringConcatenation _builder = new StringConcatenation();
_builder.append("Line ");
_builder.append(lineNumber);
_builder.append(" is mapped to ");
_builder.append(mapping.targetStartLine);
_builder.append("(\'");
_builder.append(line);
_builder.append("\')");
Assert.fail(_builder.toString());
}
int _indexOf_1 = line.indexOf("//");
int _plus = (_indexOf_1 + 2);
final String expectation = line.substring(_plus).trim();
int expTargetStart = (-1);
int expTargetEnd = (-1);
int _indexOf_2 = expectation.indexOf("..");
boolean _notEquals = (_indexOf_2 != (-1));
if (_notEquals) {
final int idx = expectation.indexOf("..");
expTargetStart = Integer.parseInt(expectation.substring(0, idx));
expTargetEnd = Integer.parseInt(expectation.substring((idx + 2)));
} else {
expTargetStart = (expTargetEnd = Integer.parseInt(expectation));
}
Assert.assertEquals(line, expTargetStart, mapping.targetStartLine);
Assert.assertEquals(("unexpected end in line : " + line), expTargetEnd, mapping.targetEndLine);
} else {
int _indexOf_3 = line.indexOf("//");
boolean _notEquals_1 = (_indexOf_3 != (-1));
if (_notEquals_1) {
Assert.fail(("Unmatched expectation : " + line));
}
}
}
}
}
use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-xtend by eclipse.
the class CompilerTraceTest method tracesTo.
public void tracesTo(final CharSequence xtend, final CharSequence java) {
try {
final String xtendWithSpaces = ((" " + xtend) + " ");
final Matcher xtendMatcher = this.p.matcher(xtendWithSpaces);
Assert.assertTrue("xtendMatcher.matches", xtendMatcher.matches());
final String xtendGroup1 = xtendMatcher.group(1);
final String xtendGroup2 = xtendMatcher.group(2);
final String xtendGroup3 = xtendMatcher.group(3);
final String actualXtendCode = ((xtendGroup1 + xtendGroup2) + xtendGroup3);
final XtendFile file = this.file(actualXtendCode, true);
XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
final JvmGenericType inferredType = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
CharSequence compiledCode = this.generator.generateType(inferredType, this.generatorConfigProvider.get(inferredType));
compiledCode = this.postProcessor.postProcess(null, compiledCode);
final Matcher javaMatcher = this.p.matcher(java.toString());
Assert.assertTrue("javaMatcher.matches", javaMatcher.matches());
final String javaGroup1 = javaMatcher.group(1);
final String javaGroup2 = javaMatcher.group(2);
final String javaGroup3 = javaMatcher.group(3);
final String actualJavaExpectation = ((javaGroup1 + javaGroup2) + javaGroup3);
Assert.assertEquals(actualJavaExpectation, compiledCode.toString());
AbstractTraceRegion _traceRegion = ((ITraceRegionProvider) compiledCode).getTraceRegion();
URI _createURI = URI.createURI(file.eResource().getURI().path());
SourceRelativeURI _sourceRelativeURI = new SourceRelativeURI(_createURI);
URI _createURI_1 = URI.createURI(file.eResource().getURI().path());
SourceRelativeURI _sourceRelativeURI_1 = new SourceRelativeURI(_createURI_1);
AbstractTraceRegion _merge = this.merge(_traceRegion.invertFor(_sourceRelativeURI, _sourceRelativeURI_1));
final SimpleTrace trace = new SimpleTrace(_merge);
int _length = xtendGroup1.length();
int _length_1 = xtendGroup2.length();
TextRegion _textRegion = new TextRegion(_length, _length_1);
final Iterable<? extends ILocationInResource> locations = trace.getAllAssociatedLocations(_textRegion);
int _length_2 = javaGroup1.length();
int _length_3 = javaGroup2.length();
final TextRegion expectedRegion = new TextRegion(_length_2, _length_3);
Assert.assertFalse(IterableExtensions.isEmpty(locations));
for (final ILocationInResource location : locations) {
ITextRegionWithLineInformation _textRegion_1 = location.getTextRegion();
boolean _equals = Objects.equal(_textRegion_1, expectedRegion);
if (_equals) {
return;
}
}
Assert.fail(((("locations did not match expectation: " + locations) + " / ") + expectedRegion));
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.
the class AbstractTrace method getAllTraceRegions.
protected Iterable<AbstractTraceRegion> getAllTraceRegions(final ITextRegion localRegion) {
final AbstractTraceRegion left = findTraceRegionAtLeftOffset(localRegion.getOffset());
final int end = localRegion.getOffset() + localRegion.getLength();
if (left == null) {
return Collections.emptyList();
}
return new Iterable<AbstractTraceRegion>() {
@Override
public Iterator<AbstractTraceRegion> iterator() {
AbstractTraceRegion root = getRootTraceRegion();
if (root == null)
return ImmutableSet.<AbstractTraceRegion>of().iterator();
final Iterator<AbstractTraceRegion> allLeafs = root.leafIterator();
Iterator<AbstractTraceRegion> result = new AbstractIterator<AbstractTraceRegion>() {
AbstractTraceRegion first;
{
while (first == null && allLeafs.hasNext()) {
AbstractTraceRegion next = allLeafs.next();
if (next.getMyOffset() == left.getMyOffset()) {
this.first = next;
break;
}
}
}
@Override
protected AbstractTraceRegion computeNext() {
if (first != null) {
AbstractTraceRegion result = first;
first = null;
return result;
}
if (!allLeafs.hasNext())
return endOfData();
AbstractTraceRegion candidate = allLeafs.next();
if (candidate.getMyOffset() >= end) {
return endOfData();
}
return candidate;
}
};
return result;
}
};
}
use of org.eclipse.xtext.generator.trace.AbstractTraceRegion in project xtext-core by eclipse.
the class AbstractTrace method getBestAssociatedLocation.
@Override
public ILocationInResource getBestAssociatedLocation(ITextRegion localRegion, AbsoluteURI uri) {
IProjectConfig projectConfig = getLocalProjectConfig();
AbstractTraceRegion left = findTraceRegionAtLeftOffset(localRegion.getOffset());
left = findParentByURI(left, uri, projectConfig);
AbstractTraceRegion right = findTraceRegionAtRightOffset(localRegion.getOffset() + localRegion.getLength());
right = findParentByURI(right, uri, projectConfig);
return mergeRegions(left, right);
}
Aggregations