use of org.eclipse.xtext.diagnostics.DiagnosticMessage in project xtext-core by eclipse.
the class XtextLinker method setDefaultValueImpl.
@Override
protected void setDefaultValueImpl(EObject obj, EReference ref, IDiagnosticProducer producer) {
if (XtextPackage.eINSTANCE.getTypeRef_Metamodel() == ref) {
final TypeRef typeRef = (TypeRef) obj;
final String typeRefName = GrammarUtil.getTypeRefName(typeRef);
final List<EObject> metamodels = XtextMetamodelReferenceHelper.findBestMetamodelForType(typeRef, "", typeRefName, scopeProvider.getScope(typeRef, ref));
if (metamodels.isEmpty() || metamodels.size() > 1)
producer.addDiagnostic(new DiagnosticMessage("Cannot find meta model for type '" + typeRefName + "'", Severity.ERROR, null));
else
typeRef.setMetamodel((AbstractMetamodelDeclaration) metamodels.get(0));
} else if (XtextPackage.eINSTANCE.getCrossReference_Terminal() == ref) {
AbstractRule rule = GrammarUtil.findRuleForName(GrammarUtil.getGrammar(obj), "ID");
if (rule == null)
producer.addDiagnostic(new DiagnosticMessage("Cannot resolve implicit reference to rule 'ID'", Severity.ERROR, null));
else {
RuleCall call = XtextFactory.eINSTANCE.createRuleCall();
call.setRule(rule);
((CrossReference) obj).setTerminal(call);
}
} else if (XtextPackage.eINSTANCE.getNamedArgument_Parameter() == ref) {
final NamedArgument argument = (NamedArgument) obj;
if (!argument.isCalledByName()) {
RuleCall ruleCall = EcoreUtil2.getContainerOfType(argument, RuleCall.class);
AbstractRule calledRule = ruleCall.getRule();
if (!(calledRule instanceof ParserRule)) {
producer.addDiagnostic(new DiagnosticMessage("Arguments can only be used with parser rules.", Severity.ERROR, null));
return;
}
if (!calledRule.eIsProxy()) {
ParserRule casted = (ParserRule) calledRule;
int idx = ruleCall.getArguments().indexOf(argument);
if (idx < casted.getParameters().size()) {
argument.setParameter(casted.getParameters().get(idx));
return;
} else if (casted.getParameters().size() == 0) {
producer.addDiagnostic(new DiagnosticMessage("Rule " + calledRule.getName() + " has no arguments.", Severity.ERROR, null));
} else {
String message = "Invalid number of arguments for rule " + calledRule.getName() + ", expecting " + casted.getParameters().size() + " but was " + (idx + 1);
producer.addDiagnostic(new DiagnosticMessage(message, Severity.ERROR, null));
}
}
}
} else {
super.setDefaultValueImpl(obj, ref, producer);
}
}
use of org.eclipse.xtext.diagnostics.DiagnosticMessage in project xtext-core by eclipse.
the class LazyLinkingResourceTest method testWarningInsteadOfError.
@Test
public void testWarningInsteadOfError() throws Exception {
final EAnnotation source = EcoreFactory.eINSTANCE.createEAnnotation();
LazyLinkingResource res = new LazyLinkingResource();
res.setEncoder(new LazyURIEncoder() {
@Override
public boolean isCrossLinkFragment(Resource res, String s) {
return "foo".equals(s);
}
@Override
public Triple<EObject, EReference, INode> decode(Resource res, String uriFragment) {
return Tuples.create((EObject) source, EcorePackage.Literals.EANNOTATION__REFERENCES, (INode) new LeafNode());
}
});
res.setLinkingService(new ILinkingService() {
@Override
public List<EObject> getLinkedObjects(EObject context, EReference reference, INode node) throws IllegalNodeException {
return Collections.emptyList();
}
});
res.setDiagnosticMessageProvider(new LinkingDiagnosticMessageProvider() {
@Override
public DiagnosticMessage getUnresolvedProxyMessage(ILinkingDiagnosticContext context) {
return new DiagnosticMessage("myMessage", Severity.WARNING, null);
}
});
assertTrue(res.getWarnings().isEmpty());
assertNull(res.getEObject("foo"));
assertEquals(1, res.getWarnings().size());
assertTrue(res.getErrors().isEmpty());
assertEquals("myMessage", res.getWarnings().get(0).getMessage());
}
Aggregations