Search in sources :

Example 11 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement 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 12 with ITextReplacement

use of org.eclipse.xtext.formatting2.regionaccess.ITextReplacement 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 13 with ITextReplacement

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

Example 14 with ITextReplacement

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

the class TextReplacerContext method isInUndefinedRegion.

protected boolean isInUndefinedRegion(ITextReplacement repl) {
    ITextSegment replacerRegion = replacer.getRegion();
    IHiddenRegion hidden = null;
    if (replacerRegion instanceof IHiddenRegionPart) {
        hidden = ((IHiddenRegionPart) replacerRegion).getHiddenRegion();
    } else if (replacerRegion instanceof IHiddenRegion) {
        hidden = (IHiddenRegion) replacerRegion;
    }
    return hidden == null || hidden.isUndefined();
}
Also used : IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 15 with ITextReplacement

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

the class TextReplacerContext method isWrapInRegion.

@Override
public boolean isWrapInRegion() {
    ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
    ITextSegment region = getReplacer().getRegion();
    int lastOffset = region.getOffset();
    for (ITextReplacement rep : this.getLocalReplacements()) {
        int endOffset = rep.getOffset();
        String between = access.textForOffset(lastOffset, endOffset - lastOffset);
        if (between.contains("\n") || rep.getReplacementText().contains("\n")) {
            return true;
        }
        lastOffset = rep.getEndOffset();
    }
    String rest = access.textForOffset(lastOffset, region.getEndOffset() - lastOffset);
    if (rest.contains("\n")) {
        return true;
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) String(java.lang.String)

Aggregations

ITextReplacement (org.eclipse.xtext.formatting2.regionaccess.ITextReplacement)35 FormatterRequest (org.eclipse.xtext.formatting2.FormatterRequest)13 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)12 XtextResource (org.eclipse.xtext.resource.XtextResource)11 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)9 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)7 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)7 Resource (org.eclipse.emf.ecore.resource.Resource)5 IFormatter2 (org.eclipse.xtext.formatting2.IFormatter2)5 String (java.lang.String)4 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)4 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)4 ITextReplacerContext (org.eclipse.xtext.formatting2.ITextReplacerContext)4 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)4 ITextRegion (org.eclipse.xtext.util.ITextRegion)4 URI (org.eclipse.emf.common.util.URI)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 MapBasedPreferenceValues (org.eclipse.xtext.preferences.MapBasedPreferenceValues)3 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)3 List (java.util.List)2