Search in sources :

Example 1 with IFormatterExtension

use of org.eclipse.xtext.formatting.IFormatterExtension in project xtext-core by eclipse.

the class Serializer method serialize.

public TreeConstructionReport serialize(EObject obj, ITokenStream tokenStream, SaveOptions options) throws IOException {
    if (options.isValidating()) {
        List<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
        validator.validateRecursive(obj, new IConcreteSyntaxValidator.DiagnosticListAcceptor(diagnostics), new HashMap<Object, Object>());
        if (!diagnostics.isEmpty())
            throw new IConcreteSyntaxValidator.InvalidConcreteSyntaxException("These errors need to be fixed before the model can be serialized.", diagnostics);
    }
    ITokenStream formatterTokenStream;
    if (formatter instanceof IFormatterExtension)
        formatterTokenStream = ((IFormatterExtension) formatter).createFormatterStream(obj, null, tokenStream, !options.isFormatting());
    else
        formatterTokenStream = formatter.createFormatterStream(null, tokenStream, !options.isFormatting());
    TreeConstructionReport report = parseTreeReconstructor.serializeSubtree(obj, formatterTokenStream);
    formatterTokenStream.flush();
    return report;
}
Also used : ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.emf.common.util.Diagnostic) IFormatterExtension(org.eclipse.xtext.formatting.IFormatterExtension) EObject(org.eclipse.emf.ecore.EObject) TreeConstructionReport(org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor.TreeConstructionReport) IConcreteSyntaxValidator(org.eclipse.xtext.validation.IConcreteSyntaxValidator)

Example 2 with IFormatterExtension

use of org.eclipse.xtext.formatting.IFormatterExtension 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 3 with IFormatterExtension

use of org.eclipse.xtext.formatting.IFormatterExtension 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();
}
Also used : ISerializationDiagnostic(org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic) ITokenStream(org.eclipse.xtext.parsetree.reconstr.ITokenStream) ISerializationContext(org.eclipse.xtext.serializer.ISerializationContext) IFormatterExtension(org.eclipse.xtext.formatting.IFormatterExtension) ISequenceAcceptor(org.eclipse.xtext.serializer.acceptor.ISequenceAcceptor) TokenStreamSequenceAdapter(org.eclipse.xtext.serializer.acceptor.TokenStreamSequenceAdapter)

Aggregations

IFormatterExtension (org.eclipse.xtext.formatting.IFormatterExtension)3 EObject (org.eclipse.emf.ecore.EObject)2 ITokenStream (org.eclipse.xtext.parsetree.reconstr.ITokenStream)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Diagnostic (org.eclipse.emf.common.util.Diagnostic)1 TreeConstructionReport (org.eclipse.xtext.parsetree.reconstr.IParseTreeConstructor.TreeConstructionReport)1 TokenStringBuffer (org.eclipse.xtext.parsetree.reconstr.impl.TokenStringBuffer)1 ISerializationContext (org.eclipse.xtext.serializer.ISerializationContext)1 ISequenceAcceptor (org.eclipse.xtext.serializer.acceptor.ISequenceAcceptor)1 TokenStreamSequenceAdapter (org.eclipse.xtext.serializer.acceptor.TokenStreamSequenceAdapter)1 ISerializationDiagnostic (org.eclipse.xtext.serializer.diagnostic.ISerializationDiagnostic)1 ITextRegion (org.eclipse.xtext.util.ITextRegion)1 IConcreteSyntaxValidator (org.eclipse.xtext.validation.IConcreteSyntaxValidator)1