Search in sources :

Example 1 with ITextReplacerContext

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

the class FormattableDocument method renderToTextReplacements.

@Override
public List<ITextReplacement> renderToTextReplacements() {
    ITextReplacerContext first = getFormatter().createTextReplacerContext(this);
    ITextReplacerContext last = createReplacements(first);
    List<ITextReplacement> replacements = last.getReplacementsUntil(first);
    return replacements;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 2 with ITextReplacerContext

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

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

the class MaxLineWidthDocument method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext context) {
    ITextReplacerContext last = super.createReplacements(context);
    List<ITextReplacement> replacements = last.getReplacementsUntil(context);
    String string = applyTextReplacements(replacements);
    if (string.contains("\n"))
        throw new FormattingNotApplicableException();
    int leadingCharCount = context.getLeadingCharsInLineCount();
    int formattedLength = string.length();
    int lineLength = leadingCharCount + formattedLength;
    if (lineLength > maxLineWidth)
        throw new FormattingNotApplicableException();
    return last;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) FormattingNotApplicableException(org.eclipse.xtext.formatting2.FormattingNotApplicableException) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 4 with ITextReplacerContext

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

the class SubDocument method createReplacements.

@Override
public ITextReplacerContext createReplacements(ITextReplacerContext previous) {
    ITextReplacerContext context = ((TextReplacerContext) previous).withDocument(this);
    context.setNextReplacerIsChild();
    return ((TextReplacerContext) super.createReplacements(context)).withDocument(previous.getDocument());
}
Also used : ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 5 with ITextReplacerContext

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

the class TextReplacerContext method getReplacementsUntil.

@Override
public List<ITextReplacement> getReplacementsUntil(ITextReplacerContext first) {
    ITextReplacerContext current = this;
    List<Iterable<ITextReplacement>> reversedReplacements = Lists.newArrayList();
    while (current != null) {
        Iterable<ITextReplacement> localReplacements = current.getLocalReplacements();
        if (!Iterables.isEmpty(localReplacements))
            reversedReplacements.add(localReplacements);
        if (current == first)
            break;
        current = current.getPreviousContext();
    }
    Collections.reverse(reversedReplacements);
    List<ITextReplacement> flattenedReplacements = new TextReplacementList<ITextReplacement>();
    for (Iterable<ITextReplacement> chunk : reversedReplacements) Iterables.addAll(flattenedReplacements, chunk);
    return flattenedReplacements;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Aggregations

ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)10 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)5 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)4 ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)4 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)4 IComment (org.eclipse.xtext.formatting2.regionaccess.IComment)3 ILineRegion (org.eclipse.xtext.formatting2.regionaccess.ILineRegion)3 FormattingNotApplicableException (org.eclipse.xtext.formatting2.FormattingNotApplicableException)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 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)1 IWhitespace (org.eclipse.xtext.formatting2.regionaccess.IWhitespace)1