Search in sources :

Example 11 with ISemanticRegion

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

the class SemanticRegionIterable method iterator.

@Override
public Iterator<ISemanticRegion> iterator() {
    return new AbstractIterator<ISemanticRegion>() {

        private ISemanticRegion next = first;

        @Override
        protected ISemanticRegion computeNext() {
            if (next == null)
                return endOfData();
            ISemanticRegion result = next;
            next = next.getNextSemanticRegion();
            if (result == last)
                next = null;
            return result;
        }
    };
}
Also used : AbstractIterator(com.google.common.collect.AbstractIterator) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)

Example 12 with ISemanticRegion

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

the class AbstractSemanticRegionsFinder method keywordPairs.

@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(Keyword kw1, Keyword kw2) {
    Preconditions.checkNotNull(kw1);
    Preconditions.checkNotNull(kw2);
    Preconditions.checkArgument(kw1 != kw2);
    Predicate<ISemanticRegion> p1 = createPredicate(kw1);
    Predicate<ISemanticRegion> p2 = createPredicate(kw2);
    List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
    Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
    LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
    for (ISemanticRegion region : all) {
        if (p1.apply(region))
            stack.push(region);
        else if (!stack.isEmpty())
            result.add(Pair.of(stack.pop(), region));
    }
    return result.build();
}
Also used : ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) LinkedList(java.util.LinkedList) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 13 with ISemanticRegion

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

the class AbstractSemanticRegionsFinder method keywordPairs.

@Override
public List<Pair<ISemanticRegion, ISemanticRegion>> keywordPairs(String kw1, String kw2) {
    Preconditions.checkNotNull(kw1);
    Preconditions.checkNotNull(kw2);
    Preconditions.checkArgument(!kw1.equals(kw2));
    Predicate<ISemanticRegion> p1 = new KeywordPredicate(kw1);
    Predicate<ISemanticRegion> p2 = new KeywordPredicate(kw2);
    List<ISemanticRegion> all = findAll(Predicates.or(p1, p2));
    Builder<Pair<ISemanticRegion, ISemanticRegion>> result = ImmutableList.builder();
    LinkedList<ISemanticRegion> stack = new LinkedList<ISemanticRegion>();
    for (ISemanticRegion region : all) {
        if (p1.apply(region))
            stack.push(region);
        else {
            AbstractRule regionRule = GrammarUtil.containingRule(region.getGrammarElement());
            while (!stack.isEmpty()) {
                ISemanticRegion candidate = stack.pop();
                if (region.getSemanticElement() == candidate.getSemanticElement()) {
                    AbstractRule candidateRule = GrammarUtil.containingRule(candidate.getGrammarElement());
                    if (regionRule == candidateRule) {
                        result.add(Pair.of(candidate, region));
                        break;
                    }
                }
            }
        }
    }
    return result.build();
}
Also used : ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) AbstractRule(org.eclipse.xtext.AbstractRule) LinkedList(java.util.LinkedList) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 14 with ISemanticRegion

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

the class SemanticRegionFinderTest method regionForRuleCallAssignedTerminal.

@Test
public void regionForRuleCallAssignedTerminal() throws Exception {
    Mixed mixed = parseAs("6 (foo)", Mixed.class);
    ISemanticRegionsFinder finder = toAccess(mixed).regionForEObject(mixed).getRegionFor();
    ISemanticRegion actual = finder.ruleCall(grammarAccess.getMixedAccess().getNameIDTerminalRuleCall_2_2_0_0());
    ISemanticRegion actuals = finder.ruleCall(grammarAccess.getMixedAccess().getNameIDTerminalRuleCall_2_2_0_0());
    Assert.assertEquals("foo", actual, actuals);
}
Also used : Mixed(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Mixed) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) ISemanticRegionsFinder(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder) Test(org.junit.Test)

Example 15 with ISemanticRegion

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

the class SemanticRegionFinderTest method regionForRuleCallAssignedDataType.

@Test
public void regionForRuleCallAssignedDataType() throws Exception {
    Mixed mixed = parseAs("6 (datatype foo)", Mixed.class);
    ISemanticRegionsFinder finder = toAccess(mixed).regionForEObject(mixed).getRegionFor();
    ISemanticRegion actual = finder.ruleCall(grammarAccess.getMixedAccess().getDatatypeDatatypeParserRuleCall_2_2_2_0());
    ISemanticRegion actuals = finder.ruleCall(grammarAccess.getMixedAccess().getDatatypeDatatypeParserRuleCall_2_2_2_0());
    Assert.assertEquals("datatype foo", actual, actuals);
}
Also used : Mixed(org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Mixed) ISemanticRegion(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion) ISemanticRegionsFinder(org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder) Test(org.junit.Test)

Aggregations

ISemanticRegion (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion)59 Test (org.junit.Test)30 ISemanticRegionsFinder (org.eclipse.xtext.formatting2.regionaccess.ISemanticRegionsFinder)18 ITextRegionAccess (org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess)15 IHiddenRegionFormatter (org.eclipse.xtext.formatting2.IHiddenRegionFormatter)14 ITextRegionDiffBuilder (org.eclipse.xtext.formatting2.regionaccess.ITextRegionDiffBuilder)13 Mixed (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.Mixed)12 Procedure1 (org.eclipse.xtext.xbase.lib.Procedures.Procedure1)9 IHiddenRegion (org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)7 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)7 EObject (org.eclipse.emf.ecore.EObject)4 IEObjectRegion (org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion)4 AssignedAction (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.AssignedAction)4 XExpression (org.eclipse.xtext.xbase.XExpression)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)3 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)3 ValueList (org.eclipse.xtext.formatting2.regionaccess.internal.regionaccesstestlanguage.ValueList)3 LinkedList (java.util.LinkedList)2 EReference (org.eclipse.emf.ecore.EReference)2 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)2