Search in sources :

Example 1 with ReferencedMetamodel

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

the class Xtext2EcoreTransformer method collectEPackages.

private void collectEPackages() {
    final List<AbstractMetamodelDeclaration> metamodelDeclarations = grammar.getMetamodelDeclarations();
    final Map<String, GeneratedMetamodel> generateUs = Maps.newLinkedHashMap();
    for (AbstractMetamodelDeclaration metamodelDeclaration : metamodelDeclarations) {
        try {
            if (metamodelDeclaration instanceof ReferencedMetamodel) {
                // load imported metamodel
                ReferencedMetamodel referencedMetamodel = (ReferencedMetamodel) metamodelDeclaration;
                EPackage referencedEPackage = referencedMetamodel.getEPackage();
                if (referencedEPackage != null)
                    collectClassInfosOf(referencedEPackage, referencedMetamodel);
            } else if (metamodelDeclaration instanceof GeneratedMetamodel) {
                String alias = Strings.emptyIfNull(metamodelDeclaration.getAlias());
                if (generateUs.containsKey(alias)) {
                    GeneratedMetamodel prev = generateUs.get(alias);
                    if (prev != null) {
                        if (prev.getEPackage() != null) {
                            prev.getEPackage().eResource().getResourceSet().getResources().remove(prev.getEPackage().eResource());
                        }
                        prev.setEPackage(null);
                    }
                    generateUs.put(alias, null);
                    EPackage pack = metamodelDeclaration.getEPackage();
                    if (pack != null)
                        pack.eResource().getResourceSet().getResources().remove(pack.eResource());
                    metamodelDeclaration.setEPackage(null);
                    throw new TransformationException(TransformationErrorCode.AliasForMetamodelAlreadyExists, "Alias '" + alias + "' registered more than once.", metamodelDeclaration);
                }
                generateUs.put(alias, (GeneratedMetamodel) metamodelDeclaration);
            } else
                throw new IllegalStateException("unknown metamodelDeclaraton " + metamodelDeclaration);
        } catch (TransformationException e) {
            reportError(e);
        }
    }
    for (GeneratedMetamodel metamodel : generateUs.values()) {
        try {
            if (metamodel != null)
                addGeneratedEPackage(metamodel);
        } catch (TransformationException e) {
            reportError(e);
        }
    }
}
Also used : GeneratedMetamodel(org.eclipse.xtext.GeneratedMetamodel) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with ReferencedMetamodel

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

the class Xtext2EcoreTransformer method getTypeRef.

TypeRef getTypeRef(String qualifiedName) {
    TypeRef result = XtextFactory.eINSTANCE.createTypeRef();
    String[] split = qualifiedName.split("::");
    String name = qualifiedName;
    if (split.length > 1) {
        result.setMetamodel(findMetamodel(grammar, split[0], split[1]));
        name = split[1];
    } else {
        result.setMetamodel(findDefaultMetamodel(grammar, qualifiedName));
    }
    if (result.getMetamodel() instanceof ReferencedMetamodel && result.getMetamodel().getEPackage() != null) {
        result.setClassifier(result.getMetamodel().getEPackage().getEClassifier(name));
    }
    return result;
}
Also used : TypeRef(org.eclipse.xtext.TypeRef) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel)

Example 3 with ReferencedMetamodel

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

the class XtextValidationTest method testBug_280413_01.

@Test
public void testBug_280413_01() throws Exception {
    XtextResource resource = getResourceFromStringAndExpect("grammar org.foo.Bar with org.eclipse.xtext.testlanguages.SimpleExpressionsTestLanguage\n" + "import 'classpath:/org/eclipse/xtext/testlanguages/SimpleExpressionsTestLanguage.ecore' as mm\n" + "Atom returns mm::Atom: name = ID;", 1);
    assertEquals(resource.getErrors().toString(), 1, resource.getErrors().size());
    assertTrue(resource.getWarnings().toString(), resource.getWarnings().isEmpty());
    Diagnostic diag = Diagnostician.INSTANCE.validate(resource.getContents().get(0));
    assertNotNull("diag", diag);
    assertEquals(diag.getChildren().toString(), 2, diag.getChildren().size());
    assertEquals("diag.isError", diag.getSeverity(), Diagnostic.ERROR);
    ReferencedMetamodel metamodel = (ReferencedMetamodel) diag.getChildren().get(0).getData().get(0);
    assertNotNull(metamodel);
}
Also used : Diagnostic(org.eclipse.emf.common.util.Diagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) Test(org.junit.Test)

Example 4 with ReferencedMetamodel

use of org.eclipse.xtext.ReferencedMetamodel in project xtext-eclipse by eclipse.

the class EcoreRefactoringParticipant method getRenamedElementsOrProxies.

@Override
protected List<EObject> getRenamedElementsOrProxies(EObject originalTarget) {
    if (originalTarget instanceof ParserRule) {
        TypeRef returnType = ((ParserRule) originalTarget).getType();
        if (returnType != null && returnType.getClassifier() != null && !Strings.isEmpty(returnType.getClassifier().getName()) && equal(((ParserRule) originalTarget).getName(), returnType.getClassifier().getName()) && returnType.getClassifier().eClass() != null && returnType.getClassifier().getEPackage() != null && !Strings.isEmpty(returnType.getClassifier().getEPackage().getNsURI())) {
            String packageNsURI = returnType.getClassifier().getEPackage().getNsURI();
            QualifiedName classifierQualifiedName = QualifiedName.create(packageNsURI, returnType.getClassifier().getName());
            URI platformResourceURI = findPlatformResourceURI(classifierQualifiedName, EcorePackage.Literals.ECLASS);
            if (platformResourceURI == null) {
                if (returnType.getMetamodel() instanceof ReferencedMetamodel)
                    getStatus().add(ERROR, "Return type '{0}' is not indexed.", returnType.getClassifier());
            } else {
                EObject classifierProxy = EcoreFactory.eINSTANCE.create(returnType.getClassifier().eClass());
                ((InternalEObject) classifierProxy).eSetProxyURI(platformResourceURI);
                if (!isDisableWarning)
                    getStatus().add(WARNING, "Renaming EClass '{0}' in ecore file. Please rerun the Ecore generator.", returnType.getClassifier());
                return singletonList(classifierProxy);
            }
        }
    }
    return null;
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) TypeRef(org.eclipse.xtext.TypeRef) QualifiedName(org.eclipse.xtext.naming.QualifiedName) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) URI(org.eclipse.emf.common.util.URI) ReferencedMetamodel(org.eclipse.xtext.ReferencedMetamodel) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 5 with ReferencedMetamodel

use of org.eclipse.xtext.ReferencedMetamodel 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

ReferencedMetamodel (org.eclipse.xtext.ReferencedMetamodel)9 EPackage (org.eclipse.emf.ecore.EPackage)4 TypeRef (org.eclipse.xtext.TypeRef)4 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)3 Grammar (org.eclipse.xtext.Grammar)3 EClassifier (org.eclipse.emf.ecore.EClassifier)2 EnumLiteralDeclaration (org.eclipse.xtext.EnumLiteralDeclaration)2 GeneratedMetamodel (org.eclipse.xtext.GeneratedMetamodel)2 Keyword (org.eclipse.xtext.Keyword)2 ParserRule (org.eclipse.xtext.ParserRule)2 Test (org.junit.Test)2 Diagnostic (org.eclipse.emf.common.util.Diagnostic)1 URI (org.eclipse.emf.common.util.URI)1 EClass (org.eclipse.emf.ecore.EClass)1 EEnum (org.eclipse.emf.ecore.EEnum)1 EEnumLiteral (org.eclipse.emf.ecore.EEnumLiteral)1 EObject (org.eclipse.emf.ecore.EObject)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 StyledString (org.eclipse.jface.viewers.StyledString)1 AbstractRule (org.eclipse.xtext.AbstractRule)1