Search in sources :

Example 31 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class AbstractFormattingConfig method getAbstractElements.

protected List<AbstractElement> getAbstractElements(EObject obj) {
    if (obj instanceof AbstractElement)
        return Collections.singletonList((AbstractElement) obj);
    if (obj instanceof AbstractRule) {
        AbstractRule rule = (AbstractRule) obj;
        if (rule.getType().getClassifier() instanceof EClass)
            return Collections.singletonList(rule.getAlternatives());
        List<AbstractElement> result = Lists.newArrayList();
        for (RuleCall rc : grammarAccess.findRuleCalls(rule)) {
            CrossReference cr = GrammarUtil.containingCrossReference(rc);
            result.add(cr == null ? rc : cr);
        }
        return result;
    }
    return null;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) AbstractElement(org.eclipse.xtext.AbstractElement) CrossReference(org.eclipse.xtext.CrossReference) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 32 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class FormattingConfigBasedStream method collectLocators.

protected Set<ElementLocator> collectLocators(EObject ele) {
    Set<ElementLocator> result = Sets.newHashSet(activeRangeLocators);
    Collection<ElementLocator> loc = Sets.newHashSet();
    if (ele instanceof AbstractElement)
        for (ElementPattern pattern : matcher.matchNext((AbstractElement) ele)) loc.add(pattern.getLocator());
    if ((last instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) last)) || (ele instanceof AbstractRule && hiddenTokenHelper.isComment((AbstractRule) ele)))
        loc = collectLocatorsForComments(loc, last, ele);
    last = ele;
    for (ElementLocator locator : loc) if (locator.getType() == LocatorType.RANGE && !activeRangeLocators.add(locator))
        activeRangeLocators.remove(locator);
    result.addAll(loc);
    for (ElementLocator locator : result) {
        if (locator instanceof IndentationLocatorStart)
            indentationLevel++;
        else if (locator instanceof IndentationLocatorEnd)
            indentationLevel--;
    }
    return result;
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) ElementLocator(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementLocator) ElementPattern(org.eclipse.xtext.formatting.impl.AbstractFormattingConfig.ElementPattern) AbstractRule(org.eclipse.xtext.AbstractRule) IndentationLocatorStart(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorStart) IndentationLocatorEnd(org.eclipse.xtext.formatting.impl.FormattingConfig.IndentationLocatorEnd)

Example 33 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class GrammarUtilTest method testGetReference.

@Test
public void testGetReference() throws Exception {
    this.with(LangATestLanguageStandaloneSetup.class);
    XtextResource resource = this.getResourceFromStringAndExpect("type A extends B", 1);
    Assignment asExtends = this.<LangATestLanguageGrammarAccess>get(LangATestLanguageGrammarAccess.class).getTypeAccess().getExtendsAssignment_2_1();
    AbstractElement _terminal = asExtends.getTerminal();
    CrossReference xref = ((CrossReference) _terminal);
    EObject _model = this.getModel(resource);
    Main model = ((Main) _model);
    EObject typeA = model.getTypes().get(0);
    EReference ref = GrammarUtil.getReference(xref, typeA.eClass());
    Assert.assertNotNull(ref);
    Assert.assertEquals("extends", ref.getName());
    Assert.assertFalse(ref.isMany());
    Assert.assertEquals(typeA.eClass(), ref.getEReferenceType());
}
Also used : Assignment(org.eclipse.xtext.Assignment) AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) LangATestLanguageGrammarAccess(org.eclipse.xtext.linking.services.LangATestLanguageGrammarAccess) CrossReference(org.eclipse.xtext.CrossReference) XtextResource(org.eclipse.xtext.resource.XtextResource) Main(org.eclipse.xtext.linking.langATestLanguage.Main) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Example 34 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class GrammarUtilTest method testFindCurrentType_01.

@Test
public void testFindCurrentType_01() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://1\'");
    _builder.newLine();
    _builder.append("Rule:");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Fragment;");
    _builder.newLine();
    _builder.append("fragment Fragment*: name=ID;");
    _builder.newLine();
    String model = _builder.toString();
    final XtextResource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractRule rule = IterableExtensions.<AbstractRule>head(grammar.getRules());
    final AbstractElement fragmentCall = rule.getAlternatives();
    final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
    Assert.assertEquals("Rule", currentType.getName());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EClassifier(org.eclipse.emf.ecore.EClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Example 35 with AbstractElement

use of org.eclipse.xtext.AbstractElement in project xtext-core by eclipse.

the class GrammarUtilTest method testFindCurrentType_06.

@Test
public void testFindCurrentType_06() throws Exception {
    this.with(XtextStandaloneSetup.class);
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("grammar myLang with org.eclipse.xtext.common.Terminals");
    _builder.newLine();
    _builder.append("generate g \'http://1\'");
    _builder.newLine();
    _builder.append("Rule:");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("Fragment;");
    _builder.newLine();
    _builder.append("fragment Fragment returns Rule:");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("name=ID Fragment?");
    _builder.newLine();
    _builder.append(";");
    _builder.newLine();
    String model = _builder.toString();
    final XtextResource r = this.getResourceFromString(model);
    EObject _get = r.getContents().get(0);
    final Grammar grammar = ((Grammar) _get);
    final AbstractRule rule = IterableExtensions.<AbstractRule>last(grammar.getRules());
    AbstractElement _alternatives = rule.getAlternatives();
    final AbstractElement fragmentCall = IterableExtensions.<AbstractElement>last(((Group) _alternatives).getElements());
    final EClassifier currentType = GrammarUtil.findCurrentType(fragmentCall);
    Assert.assertEquals("Rule", currentType.getName());
}
Also used : AbstractElement(org.eclipse.xtext.AbstractElement) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EClassifier(org.eclipse.emf.ecore.EClassifier) XtextResource(org.eclipse.xtext.resource.XtextResource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Test(org.junit.Test)

Aggregations

AbstractElement (org.eclipse.xtext.AbstractElement)126 EObject (org.eclipse.emf.ecore.EObject)39 AbstractRule (org.eclipse.xtext.AbstractRule)34 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)31 RuleCall (org.eclipse.xtext.RuleCall)26 ParserRule (org.eclipse.xtext.ParserRule)18 Grammar (org.eclipse.xtext.Grammar)17 EList (org.eclipse.emf.common.util.EList)15 Assignment (org.eclipse.xtext.Assignment)15 Test (org.junit.Test)14 Group (org.eclipse.xtext.Group)13 UnorderedGroup (org.eclipse.xtext.UnorderedGroup)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 XtextResource (org.eclipse.xtext.resource.XtextResource)11 Action (org.eclipse.xtext.Action)10 TerminalRule (org.eclipse.xtext.TerminalRule)10 Alternatives (org.eclipse.xtext.Alternatives)9 EClassifier (org.eclipse.emf.ecore.EClassifier)8 InternalEObject (org.eclipse.emf.ecore.InternalEObject)8