use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class AbstractParseTreeConstructor method write.
protected ITextRegion write(AbstractToken token, WsMergerStream out, ITextRegion location) throws IOException {
ITextRegion currentLocation = location;
INode node = token.getNode();
if (node != null) {
currentLocation = currentLocation.merge(node.getTextRegion());
}
if (!token.getTokensForSemanticChildren().isEmpty()) {
for (AbstractToken t : token.getTokensForSemanticChildren()) currentLocation = write(t, out, currentLocation);
} else {
if (token instanceof CommentToken)
out.writeComment((ILeafNode) node);
else {
String val = token.serialize(node);
if (val != null) {
if (token instanceof AssignmentToken)
out.writeSemantic(((AssignmentToken) token).getAssignmentElement(), val, node);
else
out.writeSemantic(token.getGrammarElement(), val, node);
}
}
}
return currentLocation;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class AbstractParseTreeConstructor method serializeSubtree.
@Override
public TreeConstructionReport serializeSubtree(EObject object, ITokenStream out) throws IOException {
TreeConstructionReportImpl report = createReport(object);
AbstractToken root = serialize(object, report);
Set<ICompositeNode> roots = Sets.newHashSet();
Map<EObject, AbstractToken> obj2token = Maps.newHashMap();
collectRootsAndEObjects(root, obj2token, roots);
// dump("", root);
Map<ILeafNode, EObject> comments = commentAssociater.associateCommentsWithSemanticEObjects(object, roots);
for (ICompositeNode r : roots) assignNodesByMatching(obj2token, r, comments);
WsMergerStream wsout = new WsMergerStream(out);
// dump("", root);
// System.out.println(EmfFormatter.objToStr(roots.iterator().next(),
// ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_LENGTH,
// ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_OFFSET,
// ParsetreePackage.Literals.ABSTRACT_NODE__TOTAL_LINE, ParsetreePackage.Literals.ABSTRACT_NODE__PARENT));
ITextRegion previousLocation = ITextRegion.EMPTY_REGION;
initStream(root, wsout);
previousLocation = write(root, wsout, previousLocation);
wsout.flush();
report.setPreviousLocation(previousLocation);
return report;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class TokenRegionProvider method getTokenReplaceRegion.
public ReplaceRegion getTokenReplaceRegion(String changedText, ReplaceRegion replaceRegion) {
int lengthDelta = replaceRegion.getText().length() - replaceRegion.getLength();
ITextRegion tokenRegion = getTokenRegion(changedText, new TextRegion(replaceRegion.getOffset(), replaceRegion.getText().length()));
if (tokenRegion.getOffset() == replaceRegion.getOffset() && tokenRegion.getLength() == replaceRegion.getText().length())
return replaceRegion;
else
return new ReplaceRegion(tokenRegion.getOffset(), tokenRegion.getLength() - lengthDelta, changedText.substring(tokenRegion.getOffset(), tokenRegion.getOffset() + tokenRegion.getLength()));
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class TokenRegionProvider method getTokenRegion.
public ITextRegion getTokenRegion(String text, ITextRegion region) {
Lexer lexer = lexerProvider.get();
lexer.setCharStream(new ANTLRStringStream(text));
int currentStart = 0;
int currentEnd = 0;
CommonToken nextToken = (CommonToken) lexer.nextToken();
int regionStart = region.getOffset();
int regionEnd = regionStart + region.getLength();
while (nextToken != Token.EOF_TOKEN && currentEnd <= regionStart) {
currentStart = nextToken.getStartIndex();
currentEnd = nextToken.getStopIndex() + 1;
nextToken = (CommonToken) lexer.nextToken();
}
// nextToken is either EOF or the first token that follows the start of the given region
while (nextToken != Token.EOF_TOKEN && currentEnd < regionEnd) {
currentEnd = nextToken.getStopIndex() + 1;
nextToken = (CommonToken) lexer.nextToken();
}
if (currentStart != regionStart || currentEnd != regionEnd)
return new TextRegion(currentStart, currentEnd - currentStart);
else
return region;
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class AbstractTraceRegionToString method render.
protected List<String> render(AbstractTraceRegionToString.File file, int width) {
try {
String text = file.uri == null ? getLocalText() : getRemoteText(file.uri);
ITextRegion localOrRemoteFrame = file.uri == null ? getLocalFrame() : getRemoteFrame(file.uri);
ITextRegion frame = localOrRemoteFrame == null ? new TextRegion(0, text.length()) : localOrRemoteFrame;
int last = frame.getOffset();
StringBuilder result = new StringBuilder();
for (Map.Entry<Integer, Collection<Insert>> e : sortBy(toList(index(filter(file.inserts, i -> i.offset >= frame.getOffset() && i.offset <= (frame.getOffset() + frame.getLength())), i -> i.offset).asMap().entrySet()), i -> i.getKey())) {
int offset = e.getKey().intValue();
result.append(text.substring(last, offset));
result.append(render(e.getValue(), width));
last = offset;
}
int end = frame.getOffset() + frame.getLength();
if (last < end) {
result.append(text.substring(last, end));
}
return CharStreams.readLines(new StringReader(result.toString()));
} catch (IOException t) {
throw new RuntimeIOException(t);
}
}
Aggregations