Search in sources :

Example 11 with DiagnosticMessage

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);
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) TypeRef(org.eclipse.xtext.TypeRef) EObject(org.eclipse.emf.ecore.EObject) AbstractMetamodelDeclaration(org.eclipse.xtext.AbstractMetamodelDeclaration) NamedArgument(org.eclipse.xtext.NamedArgument) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 12 with DiagnosticMessage

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());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Resource(org.eclipse.emf.ecore.resource.Resource) Triple(org.eclipse.xtext.util.Triple) EAnnotation(org.eclipse.emf.ecore.EAnnotation) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) IllegalNodeException(org.eclipse.xtext.linking.impl.IllegalNodeException) ILinkingService(org.eclipse.xtext.linking.ILinkingService) LinkingDiagnosticMessageProvider(org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider) List(java.util.List) InternalEList(org.eclipse.emf.ecore.util.InternalEList) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Aggregations

DiagnosticMessage (org.eclipse.xtext.diagnostics.DiagnosticMessage)12 EObject (org.eclipse.emf.ecore.EObject)4 ExceptionDiagnostic (org.eclipse.xtext.diagnostics.ExceptionDiagnostic)3 ILinkingDiagnosticMessageProvider (org.eclipse.xtext.linking.ILinkingDiagnosticMessageProvider)3 ILinkingDiagnosticContext (org.eclipse.xtext.linking.ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext)3 XtextLinkingDiagnostic (org.eclipse.xtext.linking.impl.XtextLinkingDiagnostic)3 List (java.util.List)2 EReference (org.eclipse.emf.ecore.EReference)2 InternalEList (org.eclipse.emf.ecore.util.InternalEList)2 AbstractRule (org.eclipse.xtext.AbstractRule)2 RuleCall (org.eclipse.xtext.RuleCall)2 LinkedHashSet (java.util.LinkedHashSet)1 EAnnotation (org.eclipse.emf.ecore.EAnnotation)1 EClass (org.eclipse.emf.ecore.EClass)1 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 AbstractElement (org.eclipse.xtext.AbstractElement)1 AbstractMetamodelDeclaration (org.eclipse.xtext.AbstractMetamodelDeclaration)1 EnumRule (org.eclipse.xtext.EnumRule)1