Search in sources :

Example 6 with ITextReplacer

use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.

the class FormattableDocument method addReplacer.

@Override
public void addReplacer(ITextReplacer replacer) {
    if (!this.getRegion().contains(replacer.getRegion())) {
        String frameTitle = getClass().getSimpleName();
        ITextSegment frameRegion = getRegion();
        String replacerTitle = replacer.getClass().getSimpleName();
        ITextSegment replacerRegion = replacer.getRegion();
        RegionsOutsideFrameException exception = new RegionsOutsideFrameException(frameTitle, frameRegion, Tuples.create(replacerTitle, replacerRegion));
        getRequest().getExceptionHandler().accept(exception);
        return;
    }
    try {
        getReplacers().add(replacer, getFormatter().createTextReplacerMerger());
    } catch (ConflictingRegionsException e) {
        getRequest().getExceptionHandler().accept(e);
    }
}
Also used : HiddenRegionFormattingToString(org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 7 with ITextReplacer

use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-xtend by eclipse.

the class RichStringFormatter method setSpace.

protected void setSpace(final IFormattableDocument doc, final int offset, final int length, final String space) {
    IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
    final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
        it.setSpace(space);
    };
    final IHiddenRegionFormatting fmt = ObjectExtensions.<IHiddenRegionFormatting>operator_doubleArrow(_createHiddenRegionFormatting, _function);
    AbstractFormatter2 _formatter = doc.getFormatter();
    ITextRegionAccess _textRegionAccess = this._iTextRegionExtensions.getTextRegionAccess();
    TextSegment _textSegment = new TextSegment(_textRegionAccess, offset, length);
    final ITextReplacer replacer = _formatter.createWhitespaceReplacer(_textSegment, fmt);
    doc.addReplacer(replacer);
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2) TextSegment(org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)

Example 8 with ITextReplacer

use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.

the class ConditionalReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    context.setNextReplacerIsChild();
    for (ISubFormatter formatter : subFormatters) {
        try {
            ITextSegment region = getRegion();
            SubDocument subDocument = new SubDocument(region, getDocument());
            for (ITextReplacer replacer : replacers) subDocument.addReplacer(replacer);
            formatter.format(subDocument);
            ITextReplacerContext first = context.withReplacer(subDocument);
            ITextReplacerContext last = subDocument.createReplacements(first);
            return last;
        } catch (FormattingNotApplicableException e) {
        // no need to do anything.
        // Try the next SubFormatter until one doens't throw a FormattingNotApplicableException
        }
    }
    throw new FormattingNotApplicableException();
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ISubFormatter(org.eclipse.xtext.formatting2.ISubFormatter) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) FormattingNotApplicableException(org.eclipse.xtext.formatting2.FormattingNotApplicableException) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 9 with ITextReplacer

use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.

the class FormattableDocument method createReplacements.

protected ITextReplacerContext createReplacements(ITextReplacerContext previous) {
    Integer maxLineWidth = getRequest().getPreferences().getPreference(FormatterPreferenceKeys.maxLineWidth);
    ITextReplacerContext context = previous.withDocument(this);
    ITextReplacerContext wrappable = null;
    Set<ITextReplacer> wrapped = Sets.newHashSet();
    Iterator<ITextReplacer> replacers = getReplacers().iterator();
    while (replacers.hasNext()) {
        ITextReplacer replacer = replacers.next();
        context = context.withReplacer(replacer);
        if (wrappable != null && context.isWrapSincePrevious()) {
            wrappable = null;
        }
        if (wrappable != null && needsAutowrap(wrappable, context, maxLineWidth)) {
            // then doesn't
            while (context != wrappable) {
                context = context.getPreviousContext();
            }
            replacer = context.getReplacer();
            replacers = getReplacers().iteratorAfter(replacer);
            context.setAutowrap(true);
            wrappable = null;
        }
        ITextReplacerContext nextContext = replacer.createReplacements(context);
        if (wrappable != null && context.isWrapInRegion()) {
            wrappable = null;
        } else {
            Integer canAutowrap = context.canAutowrap();
            if (canAutowrap != null && canAutowrap >= 0 && !context.isAutowrap() && !wrapped.contains(replacer)) {
                boolean can = true;
                if (wrappable != null) {
                    int lastEndOffset = wrappable.canAutowrap() + wrappable.getReplacer().getRegion().getEndOffset();
                    int thisEndOffset = canAutowrap + context.getReplacer().getRegion().getEndOffset();
                    can = lastEndOffset < thisEndOffset;
                }
                if (can) {
                    wrappable = context;
                    wrapped.add(replacer);
                }
            }
        }
        context = nextContext;
    }
    return context.withDocument(previous.getDocument());
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 10 with ITextReplacer

use of org.eclipse.xtext.formatting2.ITextReplacer in project xtext-core by eclipse.

the class FormattableDocument method toString.

@Override
public String toString() {
    TextRegionsToString toString = new TextRegionsToString();
    toString.setFrame(this.getRegion());
    toString.setTitle(getClass().getSimpleName() + " with ITextReplacers");
    for (ITextReplacer repl : getReplacers()) toString.add(repl.getRegion(), repl.getClass().getSimpleName() + ": " + repl.toString());
    return toString.toString();
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Aggregations

ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)13 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)5 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)5 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)4 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)3 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 TextSegment (org.eclipse.xtext.formatting2.regionaccess.internal.TextSegment)2 List (java.util.List)1 EObject (org.eclipse.emf.ecore.EObject)1 AbstractRule (org.eclipse.xtext.AbstractRule)1 FormattingNotApplicableException (org.eclipse.xtext.formatting2.FormattingNotApplicableException)1 ISubFormatter (org.eclipse.xtext.formatting2.ISubFormatter)1 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)1 MultilineCommentReplacer (org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer)1 SinglelineCodeCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer)1 SinglelineDocCommentReplacer (org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer)1 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)1 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)1 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1