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);
}
}
}
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;
}
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);
}
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;
}
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);
}
}
}
}
}
}
}
Aggregations