Class AbstractJavaFormatter

  • All Implemented Interfaces:
    IFormatter2
    Direct Known Subclasses:
    XtextFormatterJava

    @Beta
    public abstract class AbstractJavaFormatter
    extends AbstractFormatter2

    This is an abstract base class for language-specific formatters, intended to be extended with a java class.

    It is working very much like AbstractFormatter2 but does support additional methods to simplify java formatter code.

    the "_format" or "format" methods are called by reflection. The name is important as well as there are exactly two arguments whereas the first one is of the element type you like to format and the second one of type IFormattableDocument. The methods should be protected.

    Example code for a java formatter:

     import static org.xtext.example.mydsl.myDsl.MyDslPackage.Literals.*;
     
     public class MyDslFormatter2 extends AbstractJavaFormatter {
            protected void _format(Model model, IFormattableDocument doc) {
                    for (Parent parent : model.getParents())
                            doc.format(parent);
            }
     
            protected void _format(Parent parent, IFormattableDocument doc) {
                    doc.prepend(regionFor(parent).keyword("parent"), this::noSpace);
                    doc.append(regionFor(parent).keyword("parent"), this::oneSpace);
                    doc.append(regionFor(parent).feature(PARENT__NAME), this::oneSpace);
                    doc.prepend(regionFor(parent).keyword("{"), this::oneSpace);
                    doc.append(regionFor(parent).keyword("{"), this::newLine);
                    doc.interior(regionFor(parent).keyword("{"), regionFor(parent).keyword("}"), this::indent);
                    doc.append(regionFor(parent).keyword("}"), it -> it.setNewLines(1, 1, 2));
                    for (Child child : parent.getChildren())
                            doc.format(child);
            }
     
            protected void _format(Child child, IFormattableDocument doc) {
                    doc.append(regionFor(child).keyword("child"), this::oneSpace);
                    doc.append(regionFor(child).feature(CHILD__NAME), this::newLine);
            }
     }
     

    This is suitable for the following grammar:

     grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
     
     generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
     
     Model: parents+=Parent*;
     Parent: 'parent' name=ID '!' ('{' children+=Child* '}')?;
     Child: 'child' name=ID;
     

    Since:
    2.24
    • Constructor Detail

      • AbstractJavaFormatter

        public AbstractJavaFormatter()
    • Method Detail

      • format

        public void format​(java.lang.Object child,
                           IFormattableDocument document)
        Description copied from class: AbstractFormatter2
        Implement this method to create a language-specific formatter. See the JavaDoc of this class for a more detailed description of what your code should do.
        Specified by:
        format in class AbstractFormatter2
        Parameters:
        child - An XtextResource or an object for from your semantic model.
        document - The document to collect and execute ITextReplacers.
      • formatUsingPolymorphicDispatcher

        protected void formatUsingPolymorphicDispatcher​(java.lang.Object child,
                                                        IFormattableDocument document)
        You can call this method in a Java subclass of an Xtend polymorphic dispatcher based subclass of this class to use the reflective behaviour again. since 2.28
      • createPolymorhicDispatcher

        protected org.eclipse.xtext.util.PolymorphicDispatcher<java.lang.Void> createPolymorhicDispatcher()
        Override if you like to specify formatting methods in different way then default (using annotations or similar).
      • allRegionsFor

        protected ISemanticRegionsFinder allRegionsFor​(org.eclipse.emf.ecore.EObject semanticElement)
      • allSemanticRegions

        protected java.lang.Iterable<ISemanticRegion> allSemanticRegions​(org.eclipse.emf.ecore.EObject semanticElement)
      • grammarElement

        protected org.eclipse.emf.ecore.EObject grammarElement​(org.eclipse.emf.ecore.EObject semanticElement)
      • immediatelyFollowing

        protected ISemanticRegionFinder immediatelyFollowing​(org.eclipse.emf.ecore.EObject semanticElement)
      • immediatelyPreceding

        protected ISemanticRegionFinder immediatelyPreceding​(org.eclipse.emf.ecore.EObject semanticElement)
      • isMultiline

        protected boolean isMultiline​(org.eclipse.emf.ecore.EObject semanticElement)
      • nextHiddenRegion

        protected IHiddenRegion nextHiddenRegion​(org.eclipse.emf.ecore.EObject semanticElement)
      • previousHiddenRegion

        protected IHiddenRegion previousHiddenRegion​(org.eclipse.emf.ecore.EObject semanticElement)
      • regionForEObject

        protected IEObjectRegion regionForEObject​(org.eclipse.emf.ecore.EObject semanticElement)
      • semanticRegions

        protected java.lang.Iterable<ISemanticRegion> semanticRegions​(org.eclipse.emf.ecore.EObject semanticElement)
      • noAutowrap

        protected void noAutowrap​(IHiddenRegionFormatter hrf)
        Suppresses auto wrap in this hidden region.
        Since:
        2.25
      • noIndentation

        protected void noIndentation​(IHiddenRegionFormatter hrf)
        Resets the indentation level to zero.
        Since:
        2.25
      • newLine

        protected void newLine​(IHiddenRegionFormatter hrf)
        Forces a line break in this hidden region.
        Since:
        2.25
      • noSpace

        protected void noSpace​(IHiddenRegionFormatter hrf)
        Format this hidden region with using no space (zero characters).
        Since:
        2.25
      • oneSpace

        protected void oneSpace​(IHiddenRegionFormatter hrf)
        One space is added at this hidden region.
        Since:
        2.25