Search in sources :

Example 1 with ITextReplacer

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

the class RichStringFormatter method setNewLines.

protected void setNewLines(final IFormattableDocument doc, final int offset, final int length, final int indentationIncrease, final int indentationDecrease, final int newLines) {
    IHiddenRegionFormatting _createHiddenRegionFormatting = doc.getFormatter().createHiddenRegionFormatting();
    final Procedure1<IHiddenRegionFormatting> _function = (IHiddenRegionFormatting it) -> {
        it.setIndentationIncrease(Integer.valueOf(indentationIncrease));
        it.setIndentationDecrease(Integer.valueOf(indentationDecrease));
        it.setNewLinesMin(Integer.valueOf(newLines));
        it.setNewLinesDefault(Integer.valueOf(newLines));
        it.setNewLinesMax(Integer.valueOf(newLines));
    };
    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 2 with ITextReplacer

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

the class AbstractFormatter2 method createCommentReplacer.

public ITextReplacer createCommentReplacer(IComment comment) {
    EObject grammarElement = comment.getGrammarElement();
    if (grammarElement instanceof AbstractRule) {
        String ruleName = ((AbstractRule) grammarElement).getName();
        if (ruleName.startsWith("ML"))
            return new MultilineCommentReplacer(comment, '*');
        if (ruleName.startsWith("SL")) {
            if (comment.getLineRegions().get(0).getIndentation().getLength() > 0)
                return new SinglelineDocCommentReplacer(comment, "//");
            else
                return new SinglelineCodeCommentReplacer(comment, "//");
        }
    }
    String elementName = new GrammarElementTitleSwitch().showQualified().showRule().doSwitch(grammarElement);
    throw new IllegalStateException("No " + ITextReplacer.class.getSimpleName() + " configured for " + elementName);
}
Also used : GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) MultilineCommentReplacer(org.eclipse.xtext.formatting2.internal.MultilineCommentReplacer) EObject(org.eclipse.emf.ecore.EObject) SinglelineCodeCommentReplacer(org.eclipse.xtext.formatting2.internal.SinglelineCodeCommentReplacer) SinglelineDocCommentReplacer(org.eclipse.xtext.formatting2.internal.SinglelineDocCommentReplacer) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 3 with ITextReplacer

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

the class FormattableDocument method set.

@Override
public Pair<IHiddenRegion, IHiddenRegion> set(IHiddenRegion first, IHiddenRegion second, Procedure1<? super IHiddenRegionFormatter> init) {
    if (first != null && second != null) {
        AbstractFormatter2 formatter = getFormatter();
        IHiddenRegionFormatting f1 = formatter.createHiddenRegionFormatting();
        IHiddenRegionFormatting f2 = formatter.createHiddenRegionFormatting();
        init.apply(formatter.createHiddenRegionFormatter(f1, f2));
        ITextReplacer replacer1 = formatter.createHiddenRegionReplacer(first, f1);
        ITextReplacer replacer2 = formatter.createHiddenRegionReplacer(second, f2);
        addReplacer(replacer1);
        addReplacer(replacer2);
    }
    return Pair.of(first, second);
}
Also used : IHiddenRegionFormatting(org.eclipse.xtext.formatting2.IHiddenRegionFormatting) ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2)

Example 4 with ITextReplacer

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

the class HiddenRegionReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    AbstractFormatter2 formatter = context.getFormatter();
    List<IHiddenRegionPart> hiddens = region.getParts();
    if (hiddens.isEmpty()) {
        return formatter.createWhitespaceReplacer(region, formatting).createReplacements(context);
    } else if ((hiddens.size() == 1 && hiddens.get(0) instanceof IWhitespace)) {
        return formatter.createWhitespaceReplacer(hiddens.get(0), formatting).createReplacements(context);
    } else {
        List<ITextReplacer> replacers = createReplacers(formatter);
        applyHiddenRegionFormatting(replacers);
        ITextReplacerContext current = context;
        current.setNextReplacerIsChild();
        for (ITextReplacer replacer : replacers) current = replacer.createReplacements(current.withReplacer(replacer));
        return current;
    }
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) AbstractFormatter2(org.eclipse.xtext.formatting2.AbstractFormatter2) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) List(java.util.List) IWhitespace(org.eclipse.xtext.formatting2.regionaccess.IWhitespace) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 5 with ITextReplacer

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

the class TextReplacerContext method getRegion.

protected ITextSegment getRegion(int index) {
    ITextReplacerContext current = this;
    while (current != null) {
        ITextReplacer replacer2 = current.getReplacer();
        if (replacer2 != null) {
            if (index == 0) {
                return replacer2.getRegion();
            } else
                index--;
        }
        current = current.getPreviousContext();
    }
    return null;
}
Also used : ITextReplacer(org.eclipse.xtext.formatting2.ITextReplacer) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

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