use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class NodeModelStreamer method feedTokenStream.
@Override
public ITextRegion feedTokenStream(ITokenStream out, ICompositeNode in, int offset, int length) throws IOException {
List<INode> nodes = getLeafs(in, offset, offset + length);
if (nodes.isEmpty())
return new TextRegion(in.getOffset(), 0);
if (out instanceof ITokenStreamExtension)
((ITokenStreamExtension) out).init(findRootRuleForRegion(nodes.get(0)));
boolean lastIsTokenOrComment = false;
for (INode node : nodes) {
boolean currentIsTokenOrComment = tokenUtil.isCommentNode(node) || tokenUtil.isToken(node);
if (lastIsTokenOrComment && currentIsTokenOrComment)
writeHiddenEmpty(out);
lastIsTokenOrComment = currentIsTokenOrComment;
if (node instanceof ILeafNode) {
ILeafNode leaf = (ILeafNode) node;
if (leaf.isHidden())
writeHidden(out, leaf);
else
writeSemantic(out, leaf);
} else if (node instanceof ICompositeNode)
writeSemantic(out, (ICompositeNode) node);
}
out.flush();
int rStart = nodes.get(0).getOffset();
int rLength = nodes.get(nodes.size() - 1).getEndOffset() - rStart;
return new TextRegion(rStart, rLength);
}
use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class FormattableDocumentTest method shouldFormat_02.
@Test(expected = IllegalStateException.class)
public void shouldFormat_02() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
Collection<ITextRegion> _regions = it.getRequest().getRegions();
TextRegion _textRegion = new TextRegion(0, 6);
_regions.add(_textRegion);
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist");
it.setToBeFormatted(_builder);
it.setFormatter(new GenericFormatter() {
@Override
protected void format(final EObject model, final ITextRegionExtensions regionAccess, final IFormattableDocument document) {
throw new IllegalStateException("this method should never be called");
}
@Override
public boolean shouldFormat(final Object obj, final IFormattableDocument document) {
return true;
}
});
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("idlist");
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class FormattableDocumentTest method aroundDocument.
@Test
public void aroundDocument() {
final Procedure1<GenericFormatterTestRequest> _function = (GenericFormatterTestRequest it) -> {
Collection<ITextRegion> _regions = it.getRequest().getRegions();
TextRegion _textRegion = new TextRegion(0, 6);
_regions.add(_textRegion);
StringConcatenation _builder = new StringConcatenation();
_builder.append("idlist");
it.setToBeFormatted(_builder);
final GenericFormatter<IDList> _function_1 = new GenericFormatter<IDList>() {
@Override
protected void format(final IDList model, @Extension final ITextRegionExtensions regions, @Extension final IFormattableDocument document) {
final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it_1) -> {
it_1.setSpace("!");
};
document.surround(regions.regionFor(model).keyword("idlist"), _function);
}
};
it.setFormatter(_function_1);
StringConcatenation _builder_1 = new StringConcatenation();
_builder_1.append("!idlist!");
it.setExpectation(_builder_1);
};
this._genericFormatterTester.assertFormatted(_function);
}
use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class AbstractTraceRegionToString method render.
protected List<String> render(final AbstractTraceRegionToString.File file, final int width) {
try {
String _xifexpression = null;
if ((file.uri == null)) {
_xifexpression = this.getLocalText();
} else {
_xifexpression = this.getRemoteText(file.uri);
}
final String text = _xifexpression;
ITextRegion _elvis = null;
ITextRegion _xifexpression_1 = null;
if ((file.uri == null)) {
_xifexpression_1 = this.getLocalFrame();
} else {
_xifexpression_1 = this.getRemoteFrame(file.uri);
}
if (_xifexpression_1 != null) {
_elvis = _xifexpression_1;
} else {
int _length = text.length();
TextRegion _textRegion = new TextRegion(0, _length);
_elvis = _textRegion;
}
final ITextRegion frame = _elvis;
final Function1<AbstractTraceRegionToString.Insert, Boolean> _function = (AbstractTraceRegionToString.Insert it) -> {
return Boolean.valueOf(((it.offset >= frame.getOffset()) && (it.offset <= (frame.getOffset() + frame.getLength()))));
};
final Iterable<AbstractTraceRegionToString.Insert> inframe = IterableExtensions.<AbstractTraceRegionToString.Insert>filter(file.inserts, _function);
final Function<AbstractTraceRegionToString.Insert, Integer> _function_1 = (AbstractTraceRegionToString.Insert it) -> {
return Integer.valueOf(it.offset);
};
final Function1<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>, Integer> _function_2 = (Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>> it) -> {
return it.getKey();
};
final List<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>> offsets = IterableExtensions.<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>, Integer>sortBy(IterableExtensions.<Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>>>toList(Multimaps.<Integer, AbstractTraceRegionToString.Insert>index(inframe, _function_1).asMap().entrySet()), _function_2);
int last = frame.getOffset();
final StringBuilder result = new StringBuilder();
for (final Map.Entry<Integer, Collection<AbstractTraceRegionToString.Insert>> e : offsets) {
{
final Integer offset = e.getKey();
final String insert = this.render(e.getValue(), width);
final String prefix = text.substring(last, (offset).intValue());
result.append(prefix);
result.append(insert);
last = (offset).intValue();
}
}
int _offset = frame.getOffset();
int _length_1 = frame.getLength();
final int end = (_offset + _length_1);
if ((last < end)) {
result.append(text.substring(last, end));
}
String _string = result.toString();
StringReader _stringReader = new StringReader(_string);
return CharStreams.readLines(_stringReader);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.
the class XtendHighlightingCalculatorTest method addPosition.
@Override
public void addPosition(int offset, int length, String... ids) {
if (logEnabled) {
System.out.print("acceptor.addPosition(" + (offset - 22) + ", " + length);
for (String id : ids) {
System.out.print(", \"" + id + "\"");
}
System.out.println(");");
}
assertTrue("length = " + length, length >= 0);
TextRegion region = new TextRegion(offset, length);
assertEquals(1, ids.length);
// I deactivated the following check as it is to strict/to simple in the face of the new fine-grained coloring
// assertFalse(region.toString() + " is not contained in " + expectedRegions, expectedRegions.isEmpty());
Collection<String> expectedIds = expectedRegions.get(region);
if (strictMode && expectedIds.isEmpty())
fail("No Region for (offset: " + offset + ", length : " + length + ", id : " + ids[0] + ") expected.");
if (expectedIds.size() > 0)
assertTrue("expected: " + expectedRegions.toString() + " but was: " + region + " (" + ids[0] + ")", expectedIds.contains(ids[0]));
if (expectedIds.contains(ids[0]))
expectedRegions.remove(region, ids[0]);
Collection<String> unexpectedIds = explicitNotExpectedRegions.get(region);
assertFalse("unexpected highlighting as position: " + region + " (" + ids[0] + ")", unexpectedIds.contains(ids[0]));
}
Aggregations