Search in sources :

Example 1 with IHiddenRegion

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

the class RegionAccessBuilderTest method assertToStringDoesNotCrash.

private void assertToStringDoesNotCrash(final ITextRegionAccess access) {
    IHiddenRegion _previousHiddenRegion = access.regionForRootEObject().getPreviousHiddenRegion();
    ISequentialRegion current = ((ISequentialRegion) _previousHiddenRegion);
    while ((current != null)) {
        {
            Assert.assertNotNull(current.toString());
            boolean _matched = false;
            if (current instanceof IHiddenRegion) {
                _matched = true;
                current = ((IHiddenRegion) current).getNextSemanticRegion();
            }
            if (!_matched) {
                if (current instanceof ISemanticRegion) {
                    _matched = true;
                    Assert.assertNotNull(((ISemanticRegion) current).getEObjectRegion().toString());
                    current = ((ISemanticRegion) current).getNextHiddenRegion();
                }
            }
        }
    }
}
Also used : ISequentialRegion(org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 2 with IHiddenRegion

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

the class InsertionPointFinder method findInsertionPoint.

public IHiddenRegion findInsertionPoint(ISerializationContext ctx, IEObjectRegion obj, AbstractElement ins) {
    ISerState insertionState = findState(ctx, ins);
    Set<AbstractElement> followers = collectAdjacent(insertionState, s -> s.getFollowers());
    Set<AbstractElement> precendents = collectAdjacent(insertionState, s -> s.getPrecedents());
    List<IAstRegion> regions = Lists.newArrayList(obj.getAstRegions());
    if (regions.isEmpty()) {
        return obj.getPreviousHiddenRegion();
    }
    if (followers.contains(regions.get(0).getGrammarElement())) {
        return obj.getPreviousHiddenRegion();
    }
    if (precendents.contains(regions.get(regions.size() - 1).getGrammarElement())) {
        return obj.getNextHiddenRegion();
    }
    for (int i = 0; i < regions.size() - 1; i++) {
        IAstRegion leading = regions.get(i);
        IAstRegion trailing = regions.get(i + 1);
        if (precendents.contains(leading.getGrammarElement()) && followers.contains(trailing.getGrammarElement())) {
            return leading.getNextHiddenRegion();
        }
    }
    return null;
}
Also used : ISerState(org.eclipse.xtext.serializer.analysis.ISerState) AbstractElement(org.eclipse.xtext.AbstractElement) IAstRegion(org.eclipse.xtext.formatting2.regionaccess.IAstRegion)

Example 3 with IHiddenRegion

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

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

the class FormatterTestHelper method assertAllWhitespaceIsFormatted.

protected void assertAllWhitespaceIsFormatted(ITextRegionAccess access, List<ITextReplacement> replacements) {
    List<ITextSegment> expected = Lists.newArrayList();
    IHiddenRegion current = access.regionForRootEObject().getPreviousHiddenRegion();
    while (current != null) {
        expected.addAll(current.getMergedSpaces());
        current = current.getNextHiddenRegion();
    }
    List<ITextSegment> missing = TextRegions.difference(expected, replacements);
    if (!missing.isEmpty()) {
        TextRegionsToString toString = new TextRegionsToString().setTextRegionAccess(access);
        for (ITextSegment region : missing) toString.add(region, region.getClass().getSimpleName());
        String msg = "The following regions are not formatted:\n" + toString;
        System.err.println(msg);
        Assert.fail(msg);
    }
}
Also used : IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion) ITextSegment(org.eclipse.xtext.formatting2.regionaccess.ITextSegment) TextRegionAccessToString(org.eclipse.xtext.formatting2.debug.TextRegionAccessToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString) TextRegionsToString(org.eclipse.xtext.formatting2.debug.TextRegionsToString)

Example 5 with IHiddenRegion

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

the class FormattableDocument method append.

@Override
public <T extends EObject> T append(T owner, Procedure1<? super IHiddenRegionFormatter> after) {
    if (owner != null) {
        IEObjectRegion region = getTextRegionAccess().regionForEObject(owner);
        if (region != null) {
            IHiddenRegion gap = region.getNextHiddenRegion();
            set(gap, after);
        }
    }
    return owner;
}
Also used : IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Aggregations

IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)22 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)8 ITextSegment (org.eclipse.xtext.formatting2.regionaccess.ITextSegment)7 IHiddenRegionPart (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegionPart)4 ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)4 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)4 EObject (org.eclipse.emf.ecore.EObject)3 ISequentialRegion (org.eclipse.xtext.formatting2.regionaccess.ISequentialRegion)3 ArrayList (java.util.ArrayList)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 IHiddenRegionFormatting (org.eclipse.xtext.formatting2.IHiddenRegionFormatting)2 ITextReplacer (org.eclipse.xtext.formatting2.ITextReplacer)2 TextRegionAccessToString (org.eclipse.xtext.formatting2.debug.TextRegionAccessToString)2 TextRegionsToString (org.eclipse.xtext.formatting2.debug.TextRegionsToString)2 IAstRegion (org.eclipse.xtext.formatting2.regionaccess.IAstRegion)2 ImmutableList (com.google.common.collect.ImmutableList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 EList (org.eclipse.emf.common.util.EList)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1