Search in sources :

Example 76 with EClassifier

use of org.eclipse.emf.ecore.EClassifier in project xtext-eclipse by eclipse.

the class AbstractTemplateVariableResolver method getEClassifierForGrammar.

protected EClassifier getEClassifierForGrammar(String fqnClassName, Grammar grammar) {
    int dotIndex = fqnClassName.indexOf('.');
    String packageName = null;
    String className = fqnClassName;
    if (dotIndex > 0) {
        packageName = fqnClassName.substring(0, dotIndex);
        className = fqnClassName.substring(dotIndex + 1);
    }
    List<AbstractMetamodelDeclaration> allMetamodelDeclarations = GrammarUtil.allMetamodelDeclarations(grammar);
    for (AbstractMetamodelDeclaration decl : allMetamodelDeclarations) {
        EPackage pack = decl.getEPackage();
        if (packageName == null || pack.getName().equals(packageName)) {
            EClassifier eClassifier = pack.getEClassifier(className);
            if (eClassifier != null) {
                return eClassifier;
            }
        }
    }
    return null;
}
Also used : EClassifier(org.eclipse.emf.ecore.EClassifier) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) EPackage(org.eclipse.emf.ecore.EPackage)

Example 77 with EClassifier

use of org.eclipse.emf.ecore.EClassifier in project xtext-eclipse by eclipse.

the class EmfResourceReferenceUpdater method createReferenceUpdates.

@Override
protected void createReferenceUpdates(ElementRenameArguments elementRenameArguments, Multimap<URI, IReferenceDescription> resource2references, ResourceSet resourceSet, IRefactoringUpdateAcceptor updateAcceptor, IProgressMonitor monitor) {
    for (URI referringResourceURI : resource2references.keySet()) {
        try {
            if (monitor.isCanceled())
                throw new OperationCanceledException();
            Resource referringResource = resourceSet.getResource(referringResourceURI, false);
            EObject refactoredElement = resourceSet.getEObject(elementRenameArguments.getNewElementURI(elementRenameArguments.getTargetElementURI()), true);
            if (referringResource != refactoredElement.eResource()) {
                if (refactoredElement instanceof EClassifier) {
                    for (IReferenceDescription reference : resource2references.get(referringResourceURI)) {
                        EObject source = referringResource.getEObject(reference.getSourceEObjectUri().fragment());
                        if (source == null) {
                            LOG.error("Couldn't find source element " + reference.getSourceEObjectUri() + " in " + referringResource.getURI());
                        } else {
                            EObject referringEReference = source.eContainer();
                            if (referringEReference != null && referringEReference instanceof EReference)
                                ((EReference) referringEReference).setEType((EClassifier) refactoredElement);
                        }
                    }
                }
                changeUtil.addSaveAsUpdate(referringResource, updateAcceptor);
            }
        } catch (OperationCanceledException e) {
            throw e;
        } catch (Exception exc) {
            throw new WrappedException(exc);
        }
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) EClassifier(org.eclipse.emf.ecore.EClassifier) URI(org.eclipse.emf.common.util.URI) EReference(org.eclipse.emf.ecore.EReference) WrappedException(org.eclipse.emf.common.util.WrappedException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IReferenceDescription(org.eclipse.xtext.resource.IReferenceDescription)

Example 78 with EClassifier

use of org.eclipse.emf.ecore.EClassifier in project xtext-eclipse by eclipse.

the class XtextGrammarRefactoringIntegrationTest method testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier.

@Test
public void testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier() throws Exception {
    ResourceSet rs = resourceSetProvider.get();
    EcoreFactory eInstance = EcoreFactory.eINSTANCE;
    Resource ecoreModelResource = createEcoreModel(rs, ecoreURI, initialModelRoot);
    EClass greetingClass = getGreetingClass(ecoreModelResource);
    EReference greetingRefLocal = getReferenceoGreeting(ecoreModelResource, greetingClass);
    String greetingClassFragment = EcoreUtil.getURI(greetingClass).fragment();
    String greetingRefFragment = EcoreUtil.getURI(greetingRefLocal).fragment();
    EPackage refPackage = eInstance.createEPackage();
    refPackage.setName("myDsl2");
    refPackage.setNsPrefix("myDsl2");
    refPackage.setNsURI("http://testrefactoring2");
    EClass modelRefClass = eInstance.createEClass();
    refPackage.getEClassifiers().add(modelRefClass);
    modelRefClass.setName("ModelRef");
    EReference reference = eInstance.createEReference();
    reference.setName("ref");
    reference.setLowerBound(0);
    reference.setUpperBound(-1);
    reference.setEType(greetingClass);
    modelRefClass.getEStructuralFeatures().add(reference);
    Resource refToGreetingResource = createEcoreModel(rs, URI.createPlatformResourceURI(TEST_PROJECT + "/src/org/xtext/example/mydsl/" + "MyDsl2.ecore", true), refPackage);
    refToGreetingResource.unload();
    ecoreModelResource.unload();
    waitForBuild();
    waitForDisplay();
    XtextEditor editor = openEditor(grammarFile);
    doRefactoring(editor);
    waitForBuild();
    checkConsistenceOfGrammar(editor);
    ecoreModelResource.load(null);
    String renamedGreetingClassFragment = greetingClassFragment.replaceFirst(CLASSIFIERNAME, REFACTOREDCLASSIFIERNAME);
    EObject renamedGreetingClass = ecoreModelResource.getEObject(renamedGreetingClassFragment);
    assertNotNull(renamedGreetingClass);
    assertEquals(REFACTOREDCLASSIFIERNAME, SimpleAttributeResolver.NAME_RESOLVER.apply(renamedGreetingClass));
    EReference greetingReference = (EReference) ecoreModelResource.getEObject(greetingRefFragment);
    EClassifier eType = greetingReference.getEType();
    assertFalse(eType.eIsProxy());
    assertEquals(REFACTOREDCLASSIFIERNAME, eType.getName());
    refToGreetingResource.load(null);
    EReference externalReferenceToGreeting = getReferenceoGreeting(refToGreetingResource, eType);
    assertFalse(externalReferenceToGreeting.getEType().eIsProxy());
    assertEquals(REFACTOREDCLASSIFIERNAME, externalReferenceToGreeting.getEType().getName());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) EObject(org.eclipse.emf.ecore.EObject) EcoreFactory(org.eclipse.emf.ecore.EcoreFactory) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) EClassifier(org.eclipse.emf.ecore.EClassifier) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EReference(org.eclipse.emf.ecore.EReference) EPackage(org.eclipse.emf.ecore.EPackage) AbstractLinkedEditingIntegrationTest(org.eclipse.xtext.ui.testing.AbstractLinkedEditingIntegrationTest) Test(org.junit.Test)

Example 79 with EClassifier

use of org.eclipse.emf.ecore.EClassifier in project xtext-eclipse by eclipse.

the class GrammarTest method checkConcreteImplRule.

private AbstractRule checkConcreteImplRule(Grammar grammar, String ruleName) {
    AbstractRule concreteRule = GrammarUtil.findRuleForName(grammar, ruleName);
    assertNotNull(concreteRule);
    EClassifier returnType = ((ParserRule) concreteRule).getType().getClassifier();
    String returnTypeName = getClassifierName(returnType);
    assertEquals(ruleName, returnTypeName + "_Impl");
    List<Assignment> assignments = GrammarUtil.containedAssignments(concreteRule);
    assertEquals(1, assignments.size());
    assertEquals("name", assignments.get(0).getFeature());
    assertEquals("=", assignments.get(0).getOperator());
    List<Action> containedActions = GrammarUtil.containedActions(concreteRule);
    assertEquals(1, containedActions.size());
    assertEquals(returnTypeName, getClassifierName(containedActions.get(0).getType().getClassifier()));
    List<Keyword> containedKeywords = GrammarUtil.containedKeywords(concreteRule);
    assertEquals(1, containedKeywords.size());
    assertEquals(returnTypeName, containedKeywords.get(0).getValue());
    return concreteRule;
}
Also used : Assignment(org.eclipse.xtext.Assignment) Action(org.eclipse.xtext.Action) Keyword(org.eclipse.xtext.Keyword) EClassifier(org.eclipse.emf.ecore.EClassifier) AbstractRule(org.eclipse.xtext.AbstractRule)

Example 80 with EClassifier

use of org.eclipse.emf.ecore.EClassifier in project xtext-eclipse by eclipse.

the class XtextProposalProvider method completeParserRule.

private void completeParserRule(EObject model, final ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    final Grammar grammar = GrammarUtil.getGrammar(model);
    for (AbstractMetamodelDeclaration metamodelDeclaration : grammar.getMetamodelDeclarations()) {
        if (metamodelDeclaration instanceof ReferencedMetamodel) {
            ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
            EPackage ePackage = referencedMetamodel.getEPackage();
            if (ePackage != null) {
                for (EClassifier eClassifier : ePackage.getEClassifiers()) {
                    if (isProposeParserRule(eClassifier, grammar)) {
                        String proposal = eClassifier.getName();
                        String metamodelAlias = referencedMetamodel.getAlias();
                        if (metamodelAlias != null) {
                            proposal = proposal + " returns " + metamodelAlias + "::" + eClassifier.getName();
                        }
                        proposal = proposal + ": \n;\n";
                        ConfigurableCompletionProposal completionProposal = (ConfigurableCompletionProposal) createCompletionProposal(proposal, eClassifier.getName() + " - parser rule", getImage(XtextFactory.eINSTANCE.createParserRule()), context);
                        if (completionProposal != null) {
                            completionProposal.setCursorPosition(proposal.length() - 3);
                            acceptor.accept(completionProposal);
                        }
                    }
                }
            }
        }
    }
}
Also used : ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) EClassifier(org.eclipse.emf.ecore.EClassifier) Grammar(org.eclipse.xtext.Grammar) StyledString(org.eclipse.jface.viewers.StyledString) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) EPackage(org.eclipse.emf.ecore.EPackage)

Aggregations

EClassifier (org.eclipse.emf.ecore.EClassifier)153 EClass (org.eclipse.emf.ecore.EClass)94 Test (org.junit.Test)36 EPackage (org.eclipse.emf.ecore.EPackage)32 EReference (org.eclipse.emf.ecore.EReference)22 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)16 AbstractRule (org.eclipse.xtext.AbstractRule)14 EDataType (org.eclipse.emf.ecore.EDataType)13 EObject (org.eclipse.emf.ecore.EObject)13 XtextResource (org.eclipse.xtext.resource.XtextResource)10 EAttribute (org.eclipse.emf.ecore.EAttribute)8 EEnum (org.eclipse.emf.ecore.EEnum)8 Grammar (org.eclipse.xtext.Grammar)8 ParserRule (org.eclipse.xtext.ParserRule)8 ArrayList (java.util.ArrayList)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 Assignment (org.eclipse.xtext.Assignment)7 IScope (org.eclipse.xtext.scoping.IScope)6 HashSet (java.util.HashSet)5 BimserverDatabaseException (org.bimserver.BimserverDatabaseException)5