Class AbstractFormatter2
- java.lang.Object
-
- org.eclipse.xtext.formatting2.AbstractFormatter2
-
- All Implemented Interfaces:
IFormatter2
- Direct Known Subclasses:
AbstractJavaFormatter
public abstract class AbstractFormatter2 extends java.lang.Object implements IFormatter2
This is an abstract base class for language-specific formatters.
It is the formatters responsibility to create a list of text replacements, which, when applied to a text document, increase the documents readability for humans. At the same time the text changes should not have an impact on the semantic model a parser would create from the document.
Formatters based on this class compute text replacements in two logical steps: First, they traverses the semantic model and collect
text replaces
. Each replacer is responsible for a (small) section of the document. In a second step, the replacers are executed from the beginning to the end of the document and create the actualtext replacements
. This decoupling allows it to traverse the semantic model in arbitrary order and yet compute information that requires sequential processing of the document, e.g. indentation.To allow subclasses to implement this mechanism conveniently, there are several helper classes:
ITextRegionAccess
allows to obtain text regions for elements in the semantic model.ITextReplacer
createsreplacements
for a specific region in the document.IFormattableDocument
collectsreplacers
and validates if they overlap.
A formatter based on this class typically uses the
ITextRegionAccess
to obtain theIHiddenRegion
which precedes or trailssemantic object
,features
,keywords
, orrule calls
. ThisIHiddenRegion
represents the whitespace, newlines and comments between semantic tokens. No matter if there are zero or N hidden tokens between two semantic tokens, there is always exactly oneIHiddenRegion
. In other words, theIHiddenRegion
groups all hidden tokens between two semantic tokens.For the
IHiddenRegion
, the formatter will create aITextReplacer
and store the replacer in theIFormattableDocument
. Typically this is aHiddenRegionReplacer
parameterized with anIHiddenRegionFormatting
. TheHiddenRegionReplacer
will then delegate to aWhitespaceReplacer
orCommentReplacer
, depending on which kind of tokens are inside the hidden region.To format a document with syntax confirming to the parser rule:
Entity: 'entity' name=ValidID '{' features+=Feature* '}';
the following formatter implementation (Xtend code) can be used:
def dispatch void format(Entity entity, extension IFormattableDocument document) { val open = entity.regionFor.keyword("{") val close = entity.regionFor.keyword("}") entity.regionFor.feature(ABSTRACT_ELEMENT__NAME).surround[oneSpace] entity.superType.surround[oneSpace] open.append[newLine] interior(open, close)[indent] format(entity.getSuperType(), document); for (Feature feature : entity.features) { feature.format feature.append[setNewLines(1, 1, 2)] } }
For the full example, see the DomainModel Example. It can be accessed via
Eclipse -> File -> New -> Example.
The class
Entity
is part of the semantic model.The method calls
regionFor.feature()
andregionFor.keyword()
are using extension methods:ITextRegionExtensions.regionFor(EObject)
andISemanticRegionFinder.keyword(String)
respectivelyISemanticRegionFinder.feature(EStructuralFeature)
. They return anISemanticRegion
.The methods
prepend()
,append()
andsurround()
are extension methods fromIFormattableDocument
. They create and register anHiddenRegionReplacer
for theIHiddenRegion
before and/or after the providedISemanticRegion
.Override
create*()
methods to customize formatter-local services.This implementation is stateful and cannot be used in parallel.
-
-
Field Summary
Fields Modifier and Type Field Description protected ITextRegionExtensions
textRegionExtensions
Offer subclasses access to the methods fromITextRegionAccess
as extension methods.
-
Constructor Summary
Constructors Constructor Description AbstractFormatter2()
-
Method Summary
-
-
-
Field Detail
-
textRegionExtensions
@Extension protected ITextRegionExtensions textRegionExtensions
Offer subclasses access to the methods fromITextRegionAccess
as extension methods.
-
-
Method Detail
-
_format
protected void _format(java.lang.Object obj, IFormattableDocument document)
Fall-back for types that are not handled by a subclasse's dispatch method.
-
_format
protected void _format(org.eclipse.emf.ecore.EObject obj, IFormattableDocument document)
Fall-back for types that are not handled by a subclasse's dispatch method.
-
_format
protected void _format(java.lang.Void obj, IFormattableDocument document)
Fall-back for subclasses that accidently try to dispatch over null values.
-
_format
protected void _format(XtextResource resource, IFormattableDocument document)
Forresources
, assume we want to format the first EObject from the contents list only. Because that's where the parser puts the semantic model.
-
createCommentReplacer
public ITextReplacer createCommentReplacer(IComment comment)
-
createFormattableRootDocument
public IFormattableDocument createFormattableRootDocument()
-
createFormattableSubDocument
public IFormattableSubDocument createFormattableSubDocument(ITextSegment region, IFormattableDocument parent)
-
createHiddenRegionFormatter
public IHiddenRegionFormatter createHiddenRegionFormatter(IHiddenRegionFormatting formatting)
-
createHiddenRegionFormatter
public IHiddenRegionFormatter createHiddenRegionFormatter(IHiddenRegionFormatting f1, IHiddenRegionFormatting f2)
-
createHiddenRegionFormatting
public IHiddenRegionFormatting createHiddenRegionFormatting()
-
createHiddenRegionFormattingMerger
public IMerger<IHiddenRegionFormatting> createHiddenRegionFormattingMerger()
-
createHiddenRegionReplacer
public ITextReplacer createHiddenRegionReplacer(IHiddenRegion region, IHiddenRegionFormatting formatting)
-
createTextReplacerContext
public ITextReplacerContext createTextReplacerContext(IFormattableDocument document)
-
createTextReplacerMerger
public IMerger<ITextReplacer> createTextReplacerMerger()
-
createWhitespaceReplacer
public ITextReplacer createWhitespaceReplacer(ITextSegment hiddens, IHiddenRegionFormatting formatting)
-
format
public final java.util.List<ITextReplacement> format(FormatterRequest request)
Description copied from interface:IFormatter2
Execute the formatter.- Specified by:
format
in interfaceIFormatter2
-
handleTraceMissing
protected IFormattableDocument handleTraceMissing(IFormattableDocument problematic, RegionTraceMissingException e)
-
format
public abstract void format(java.lang.Object obj, IFormattableDocument document)
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.- Parameters:
obj
- An XtextResource or an object for from your semantic model.document
- The document to collect and executeITextReplacer
s.
-
getPreference
public <T> T getPreference(TypedPreferenceKey<T> key)
- Returns:
- a preference value from
FormatterRequest.getPreferences()
-
getPreferences
public ITypedPreferenceValues getPreferences()
- Returns:
- the preferences from the
FormatterRequest
-
getRequest
public FormatterRequest getRequest()
- Returns:
- the
FormatterRequest
that was passed intoIFormatter2.format(FormatterRequest)
when invoking the formatter.
-
getTextRegionAccess
public ITextRegionAccess getTextRegionAccess()
-
initalize
@Deprecated protected void initalize(FormatterRequest request)
Deprecated.This method is deprecated, use #initialize(FormatterRequest) instead
-
initialize
protected void initialize(FormatterRequest request)
Overwrite this method to initialize member fields beforeformat(Object, IFormattableDocument)
is called.
-
isInRequestedRange
protected boolean isInRequestedRange(org.eclipse.emf.ecore.EObject obj)
-
postProcess
protected java.util.List<ITextReplacement> postProcess(IFormattableDocument document, java.util.List<ITextReplacement> replacements)
-
reset
protected void reset()
Overwrite this method to clean up member fields afterformat(Object, IFormattableDocument)
has been called.
-
shouldFormat
public boolean shouldFormat(java.lang.Object obj, IFormattableDocument document)
-
-