org.eclipse.xtext.validation.RangeBasedDiagnostic Java Examples

The following examples show how to use org.eclipse.xtext.validation.RangeBasedDiagnostic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractXtextTestUtil.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}