use of org.eclipse.xtext.parsetree.reconstr.ITokenStream in project xtext-core by eclipse.
the class DefaultNodeModelFormatter method format.
@Override
public IFormattedRegion format(ICompositeNode root, int offset, int length) {
String indent = getIndentation(root, offset);
TokenStringBuffer buf = new TokenStringBuffer();
ITokenStream out = offset == 0 ? buf : new FilterFirstWhitespaceStream(buf);
ITokenStream fmt;
if (formatter instanceof IFormatterExtension) {
EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(root);
if (semanticElement != null)
fmt = ((IFormatterExtension) formatter).createFormatterStream(semanticElement, indent, out, false);
else {
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380406
ITextRegion rootRegion = root.getTextRegion();
return new FormattedRegion(rootRegion.getOffset(), rootRegion.getLength(), root.getText());
}
} else
fmt = formatter.createFormatterStream(indent, out, false);
try {
ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length);
return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString());
} catch (IOException e) {
// this should never happen since TokenStringBuffer doesn't throw IOEs.
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.parsetree.reconstr.ITokenStream in project xtext-core by eclipse.
the class Serializer method serialize.
protected void serialize(EObject obj, ITokenStream tokenStream, SaveOptions options) throws IOException {
ISerializationDiagnostic.Acceptor errors = ISerializationDiagnostic.EXCEPTION_THROWING_ACCEPTOR;
ITokenStream formatterTokenStream;
if (formatter instanceof IFormatterExtension)
formatterTokenStream = ((IFormatterExtension) formatter).createFormatterStream(obj, null, tokenStream, !options.isFormatting());
else
formatterTokenStream = formatter.createFormatterStream(null, tokenStream, !options.isFormatting());
ISerializationContext context = getIContext(obj);
ISequenceAcceptor acceptor = new TokenStreamSequenceAdapter(formatterTokenStream, grammar.getGrammar(), errors);
serialize(context, obj, acceptor, errors);
formatterTokenStream.flush();
}
Aggregations