Search in sources :

Example 1 with TokenStringBuffer

use of org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer 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);
    }
}
Also used : ITokenStream(org.eclipse.xtext.parsetree.reconstr.ITokenStream) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) IFormatterExtension(org.eclipse.xtext.formatting.IFormatterExtension) TokenStringBuffer(org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer) IOException(java.io.IOException)

Example 2 with TokenStringBuffer

use of org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer in project xtext-core by eclipse.

the class Serializer method serialize.

@Override
public String serialize(EObject obj, SaveOptions options) {
    checkNotNull(obj, "obj must not be null.");
    checkNotNull(options, "options must not be null.");
    try {
        if (formatter2Provider != null) {
            StringBuilder builder = new StringBuilder();
            serialize(obj, builder, options);
            return builder.toString();
        } else {
            TokenStringBuffer tokenStringBuffer = new TokenStringBuffer();
            serialize(obj, tokenStringBuffer, options);
            return tokenStringBuffer.toString();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TokenStringBuffer(org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 TokenStringBuffer (org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer)2 EObject (org.eclipse.emf.ecore.EObject)1 IFormatterExtension (org.eclipse.xtext.formatting.IFormatterExtension)1 ITokenStream (org.eclipse.xtext.parsetree.reconstr.ITokenStream)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1