Search in sources :

Example 1 with RangeBasedDiagnostic

use of org.eclipse.xtext.validation.RangeBasedDiagnostic in project dsl-devkit by dsldevkit.

the class AbstractXtextTestUtil method processDiagnostic.

/**
 * Gets the offset and the error message for a given {@link Diagnostic}.
 *
 * @param diagnostic
 *          instance of {@link Diagnostic}
 * @return
 *         offset and error message
 */
private Pair<Integer, String> processDiagnostic(final Diagnostic diagnostic) {
    StringBuilder errorMessage = new StringBuilder();
    if (diagnostic instanceof AbstractValidationDiagnostic) {
        AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
        errorMessage.append("Unexpected issue found. Code '");
        errorMessage.append(avd.getIssueCode()).append("'\n");
        errorMessage.append(avd.getMessage());
        if (avd instanceof FeatureBasedDiagnostic && ((FeatureBasedDiagnostic) avd).getFeature() != null) {
            List<INode> nodes = NodeModelUtils.findNodesForFeature(avd.getSourceEObject(), ((FeatureBasedDiagnostic) avd).getFeature());
            if (nodes != null && !nodes.isEmpty()) {
                return new Pair<Integer, String>(findFirstNonHiddenLeafNode(nodes.get(0)).getTotalOffset(), errorMessage.toString());
            }
        } else if (avd instanceof RangeBasedDiagnostic) {
            return new Pair<Integer, String>(((RangeBasedDiagnostic) avd).getOffset(), errorMessage.toString());
        } else {
            return new Pair<Integer, String>(NodeModelUtils.getNode(avd.getSourceEObject()).getTotalOffset(), errorMessage.toString());
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) RangeBasedDiagnostic(org.eclipse.xtext.validation.RangeBasedDiagnostic) AbstractValidationDiagnostic(org.eclipse.xtext.validation.AbstractValidationDiagnostic) FeatureBasedDiagnostic(org.eclipse.xtext.validation.FeatureBasedDiagnostic) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 2 with RangeBasedDiagnostic

use of org.eclipse.xtext.validation.RangeBasedDiagnostic in project dsl-devkit by dsldevkit.

the class AbstractValidationTest method memorizeUnexpectedErrors.

/**
 * Memorize the position and issue code of each unexpected diagnostic that appears in the file.
 * A diagnostic is considered as expected if a marker with the issue code in the test file was set.
 */
private void memorizeUnexpectedErrors() {
    for (Diagnostic diagnostic : getUnexpectedDiagnostics()) {
        if (diagnostic instanceof AbstractValidationDiagnostic) {
            AbstractValidationDiagnostic avd = (AbstractValidationDiagnostic) diagnostic;
            // Create error message
            StringBuilder sb = new StringBuilder();
            sb.append("Unexpected issue found. Code '");
            sb.append(avd.getIssueCode());
            sb.append(DOT_AND_LINEBREAK);
            // Retrieve the position and add the error
            if (avd instanceof FeatureBasedDiagnostic && ((FeatureBasedDiagnostic) avd).getFeature() != null) {
                List<INode> nodes = NodeModelUtils.findNodesForFeature(avd.getSourceEObject(), ((FeatureBasedDiagnostic) avd).getFeature());
                if (nodes.isEmpty()) {
                    INode node = NodeModelUtils.getNode(avd.getSourceEObject());
                    memorizeErrorOnPosition(getXtextTestUtil().findFirstNonHiddenLeafNode(node).getTotalOffset(), sb.toString());
                } else {
                    for (INode node : nodes) {
                        memorizeErrorOnPosition(getXtextTestUtil().findFirstNonHiddenLeafNode(node).getTotalOffset(), sb.toString());
                    }
                }
            } else if (avd instanceof RangeBasedDiagnostic) {
                memorizeErrorOnPosition(((RangeBasedDiagnostic) avd).getOffset(), sb.toString());
            } else {
                memorizeErrorOnPosition(NodeModelUtils.getNode(avd.getSourceEObject()).getTotalOffset(), sb.toString());
            }
        } else {
            // Create error message
            StringBuilder sb = new StringBuilder();
            sb.append("Unexpected diagnostic found. '");
            sb.append(diagnostic.toString());
            sb.append(DOT_AND_LINEBREAK);
            // Add error message
            memorizeErrorOnPosition(0, sb.toString());
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) RangeBasedDiagnostic(org.eclipse.xtext.validation.RangeBasedDiagnostic) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) XtextSyntaxDiagnostic(org.eclipse.xtext.resource.XtextSyntaxDiagnostic) AbstractValidationDiagnostic(org.eclipse.xtext.validation.AbstractValidationDiagnostic) RangeBasedDiagnostic(org.eclipse.xtext.validation.RangeBasedDiagnostic) AbstractDiagnostic(org.eclipse.xtext.diagnostics.AbstractDiagnostic) FeatureBasedDiagnostic(org.eclipse.xtext.validation.FeatureBasedDiagnostic) Diagnostic(org.eclipse.emf.common.util.Diagnostic) AbstractValidationDiagnostic(org.eclipse.xtext.validation.AbstractValidationDiagnostic) FeatureBasedDiagnostic(org.eclipse.xtext.validation.FeatureBasedDiagnostic)

Aggregations

INode (org.eclipse.xtext.nodemodel.INode)2 AbstractValidationDiagnostic (org.eclipse.xtext.validation.AbstractValidationDiagnostic)2 FeatureBasedDiagnostic (org.eclipse.xtext.validation.FeatureBasedDiagnostic)2 RangeBasedDiagnostic (org.eclipse.xtext.validation.RangeBasedDiagnostic)2 BasicDiagnostic (org.eclipse.emf.common.util.BasicDiagnostic)1 Diagnostic (org.eclipse.emf.common.util.Diagnostic)1 AbstractDiagnostic (org.eclipse.xtext.diagnostics.AbstractDiagnostic)1 XtextSyntaxDiagnostic (org.eclipse.xtext.resource.XtextSyntaxDiagnostic)1 Pair (org.eclipse.xtext.xbase.lib.Pair)1