Search in sources :

Example 31 with ITextReplacement

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

the class TextReplacerContext method getLeadingCharsInLineCount.

@Override
public int getLeadingCharsInLineCount() {
    ITextRegionAccess access = getDocument().getRequest().getTextRegionAccess();
    int lastOffset = replacer.getRegion().getOffset();
    ITextReplacerContext current = this;
    int count = 0;
    while (current != null) {
        Iterator<ITextReplacement> localReplacements = current.getLocalReplacementsReverse().iterator();
        while (localReplacements.hasNext()) {
            ITextReplacement rep = localReplacements.next();
            int endOffset = rep.getEndOffset();
            if (endOffset > lastOffset) {
                // System.out.println("error");
                continue;
            }
            String between = access.textForOffset(endOffset, lastOffset - endOffset);
            int idx = between.lastIndexOf('\n');
            if (idx >= 0)
                return count + logicalLength(between.substring(idx + 1));
            count += logicalLength(between);
            String text = rep.getReplacementText();
            int idx2 = text.lastIndexOf('\n');
            if (idx2 >= 0)
                return count + logicalLength(text.substring(idx2 + 1));
            count += logicalLength(text);
            lastOffset = rep.getOffset();
        }
        current = current.getPreviousContext();
    }
    String rest = access.textForOffset(0, lastOffset);
    int idx = rest.lastIndexOf('\n');
    if (idx >= 0)
        return count + logicalLength(rest.substring(idx + 1));
    count += lastOffset;
    return count;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) String(java.lang.String) ITextReplacerContext(org.eclipse.xtext.formatting2.ITextReplacerContext)

Example 32 with ITextReplacement

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

the class TextReplacerContext method toStringLocal.

protected String toStringLocal() {
    List<String> items = Lists.newArrayList();
    if (autowrap)
        items.add("autowrap");
    if (canAutowrap != null && canAutowrap >= 0)
        items.add("canAutowrap");
    if (replacer != null) {
        ITextSegment region = replacer.getRegion();
        items.add(format("replacer=[%d-%d-%s|%s]", region.getOffset(), region.getLength(), replacer.getClass().getSimpleName(), replacer.toString()));
    }
    if (replacements != null)
        for (ITextReplacement r : replacements) {
            String fmt = "replacement=[%d-%d|%s]";
            items.add(format(fmt, r.getOffset(), r.getLength(), r.getReplacementText()));
        }
    return Joiner.on("; ").join(items);
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) String(java.lang.String) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 33 with ITextReplacement

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

the class AbstractFormatter2 method postProcess.

protected List<ITextReplacement> postProcess(IFormattableDocument document, List<ITextReplacement> replacements) {
    List<ITextSegment> expected = Lists.newArrayList();
    IHiddenRegion current = getTextRegionAccess().regionForRootEObject().getPreviousHiddenRegion();
    while (current != null) {
        if (current.isUndefined() && isInRequestedRange(current.getOffset(), current.getEndOffset()))
            expected.addAll(current.getMergedSpaces());
        current = current.getNextHiddenRegion();
    }
    if (expected.isEmpty())
        return replacements;
    List<ITextSegment> missing = TextRegions.difference(expected, replacements);
    if (missing.isEmpty())
        return replacements;
    List<ITextReplacement> result = Lists.newArrayList(replacements);
    for (ITextSegment seg : missing) {
        IHiddenRegion h = null;
        if (seg instanceof IHiddenRegion)
            h = (IHiddenRegion) seg;
        if (seg instanceof IHiddenRegionPart)
            h = ((IHiddenRegionPart) seg).getHiddenRegion();
        if (h != null && (h.getNextSemanticRegion() == null || h.getPreviousSemanticRegion() == null))
            result.add(seg.replaceWith(""));
        else
            result.add(seg.replaceWith(" "));
    }
    return result;
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) IHiddenRegionPart(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment)

Example 34 with ITextReplacement

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

the class AbstractFormatter2 method format.

@Override
public final List<ITextReplacement> format(FormatterRequest request) {
    try {
        initialize(request);
        XtextResource xtextResource = request.getTextRegionAccess().getResource();
        IFormattableDocument document = createFormattableRootDocument();
        try {
            format(xtextResource, document);
        } catch (RegionTraceMissingException e) {
            document = handleTraceMissing(document, e);
        }
        List<ITextReplacement> rendered = document.renderToTextReplacements();
        List<ITextReplacement> postprocessed = postProcess(document, rendered);
        return postprocessed;
    } finally {
        reset();
    }
}
Also used : RegionTraceMissingException(org.eclipse.xtext.formatting2.internal.RegionTraceMissingException) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 35 with ITextReplacement

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

the class FormatterTester method assertReplacementsAreInRegion.

protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions, String doc) {
    Set<ITextReplacement> invalid = Sets.newHashSet();
    ALLOWED: for (ITextRegion allowed : regions) for (ITextReplacement r : rep) {
        if (allowed.contains(r))
            continue ALLOWED;
        invalid.add(r);
    }
    if (!invalid.isEmpty()) {
        String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
        fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
    }
}
Also used : ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

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