Search in sources :

Example 6 with ITextReplacerContext

use of org.eclipse.xtext.formatting2.ITextReplacerContext 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)

Example 7 with ITextReplacerContext

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

the class WhitespaceReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    if (formatting.getAutowrap() != null && formatting.getAutowrap() >= 0)
        context.setCanAutowrap(formatting.getAutowrap());
    String space = formatting.getSpace();
    int trailingNewLinesOfPreviousRegion = trailingNewLinesOfPreviousRegion();
    int computedNewLineCount = computeNewLineCount(context);
    int newLineCount = Math.max(computedNewLineCount - trailingNewLinesOfPreviousRegion, 0);
    if (newLineCount == 0 && context.isAutowrap()) {
        IAutowrapFormatter onAutowrap = formatting.getOnAutowrap();
        if (onAutowrap != null) {
            onAutowrap.format(region, formatting, context.getDocument());
        }
        newLineCount = 1;
    }
    int indentationCount = computeNewIndentation(context);
    if (newLineCount == 0 && trailingNewLinesOfPreviousRegion == 0) {
        if (space != null)
            context.addReplacement(region.replaceWith(space));
    } else {
        boolean noIndentation = formatting.getNoIndentation() == Boolean.TRUE;
        String newLines = context.getNewLinesString(newLineCount);
        String indentation = noIndentation ? "" : context.getIndentationString(indentationCount);
        context.addReplacement(region.replaceWith(newLines + indentation));
    }
    return context.withIndentation(indentationCount);
}
Also used : HiddenRegionFormattingToString(org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString) IAutowrapFormatter(org.eclipse.xtext.formatting2.IAutowrapFormatter)

Example 8 with ITextReplacerContext

use of org.eclipse.xtext.formatting2.ITextReplacerContext 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 ITextReplacerContext

use of org.eclipse.xtext.formatting2.ITextReplacerContext 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 ITextReplacerContext

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

the class MultilineCommentReplacer method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    if (!multiline)
        return context;
    IComment comment = getComment();
    ITextRegionAccess access = comment.getTextRegionAccess();
    List<ILineRegion> lines = comment.getLineRegions();
    String oldIndentation = lines.get(0).getIndentation().getText();
    String indentationString = context.getIndentationString();
    String newIndentation = indentationString + " " + prefix + " ";
    for (int i = 1; i < lines.size() - 1; i++) {
        ITextSegment line = lines.get(i);
        String text = line.getText();
        int prefixOffset = prefixOffset(text);
        ITextSegment target;
        if (prefixOffset >= 0)
            target = access.regionForOffset(line.getOffset(), prefixOffset + 1);
        else if (text.startsWith(oldIndentation))
            target = access.regionForOffset(line.getOffset(), oldIndentation.length());
        else
            target = access.regionForOffset(line.getOffset(), 0);
        context.addReplacement(target.replaceWith(newIndentation));
    }
    if (lines.size() > 1) {
        ILineRegion line = lines.get(lines.size() - 1);
        context.addReplacement(line.getIndentation().replaceWith(indentationString + " "));
    }
    return context;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IComment(org.eclipse.xtext.formatting2.regionaccess.IComment) ILineRegion(org.eclipse.xtext.formatting2.regionaccess.ILineRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Aggregations

ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)10 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)5 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)4 FormattingNotApplicableException (org.eclipse.xtext.formatting2.FormattingNotApplicableException)2 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)2 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)2 String (java.lang.String)1 List (java.util.List)1 AbstractFormatter2 (org.eclipse.xtext.formatting2.AbstractFormatter2)1 IAutowrapFormatter (org.eclipse.xtext.formatting2.IAutowrapFormatter)1 ISubFormatter (org.eclipse.xtext.formatting2.ISubFormatter)1 HiddenRegionFormattingToString (org.eclipse.xtext.formatting2.debug.HiddenRegionFormattingToString)1 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)1 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)1 IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)1