org.eclipse.xtext.resource.XtextSyntaxDiagnostic Java Examples

The following examples show how to use org.eclipse.xtext.resource.XtextSyntaxDiagnostic. 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: DefaultFoldingStructureProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @see org.eclipse.xtext.ui.editor.model.IXtextModelListener#modelChanged(org.eclipse.xtext.resource.XtextResource)
 */
@Override
public void modelChanged(XtextResource resource) {
	if (resource == null)
		return;
	boolean existingSyntaxErrors = Iterables.any(resource.getErrors(), new Predicate<Diagnostic>() {
		@Override
		public boolean apply(Diagnostic diagnostic) {
			return diagnostic instanceof XtextSyntaxDiagnostic;
		}
	});

	if (!existingSyntaxErrors) {
		calculateProjectionAnnotationModel(false);
	}
}
 
Example #2
Source File: DiagnosticConverterImpl.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity,	IAcceptor<Issue> acceptor) {
	IssueImpl issue = new Issue.IssueImpl();
	issue.setSyntaxError(diagnostic instanceof XtextSyntaxDiagnostic);
	issue.setSeverity(severity);
	issue.setLineNumber(diagnostic.getLine());
	issue.setColumn(diagnostic.getColumn());
	issue.setMessage(diagnostic.getMessage());

	if (diagnostic instanceof org.eclipse.xtext.diagnostics.Diagnostic) {
		org.eclipse.xtext.diagnostics.Diagnostic xtextDiagnostic = (org.eclipse.xtext.diagnostics.Diagnostic) diagnostic;
		issue.setOffset(xtextDiagnostic.getOffset());
		issue.setLength(xtextDiagnostic.getLength());
	}
	if (diagnostic instanceof AbstractDiagnostic) {
		AbstractDiagnostic castedDiagnostic = (AbstractDiagnostic)diagnostic;
		issue.setUriToProblem(castedDiagnostic.getUriToProblem());
		issue.setCode(castedDiagnostic.getCode());
		issue.setData(castedDiagnostic.getData());
		issue.setLineNumberEnd(castedDiagnostic.getLineEnd());
		issue.setColumnEnd(castedDiagnostic.getColumnEnd());
	}
	issue.setType(CheckType.FAST);
	acceptor.accept(issue);
}
 
Example #3
Source File: N4JSDiagnosticConverter.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void convertResourceDiagnostic(Diagnostic diagnostic, Severity severity, IAcceptor<Issue> acceptor) {
	// START: Following copied from super method
	LSPIssue issue = new LSPIssue(); // Changed
	issue.setSyntaxError(diagnostic instanceof XtextSyntaxDiagnostic);
	issue.setSeverity(severity);
	issue.setLineNumber(diagnostic.getLine());
	issue.setColumn(diagnostic.getColumn());
	issue.setMessage(diagnostic.getMessage());

	if (diagnostic instanceof org.eclipse.xtext.diagnostics.Diagnostic) {
		org.eclipse.xtext.diagnostics.Diagnostic xtextDiagnostic = (org.eclipse.xtext.diagnostics.Diagnostic) diagnostic;
		issue.setOffset(xtextDiagnostic.getOffset());
		issue.setLength(xtextDiagnostic.getLength());
	}
	if (diagnostic instanceof AbstractDiagnostic) {
		AbstractDiagnostic castedDiagnostic = (AbstractDiagnostic) diagnostic;
		issue.setUriToProblem(castedDiagnostic.getUriToProblem());
		issue.setCode(castedDiagnostic.getCode());
		issue.setData(castedDiagnostic.getData());

		// START: Changes here
		INode node = ReflectionUtils.getMethodReturn(AbstractDiagnostic.class, "getNode", diagnostic);
		int posEnd = castedDiagnostic.getOffset() + castedDiagnostic.getLength();
		LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(node, posEnd);
		issue.setLineNumberEnd(lineAndColumn.getLine());
		issue.setColumnEnd(lineAndColumn.getColumn());
		// END: Changes here
	}
	issue.setType(CheckType.FAST);
	acceptor.accept(issue);
	// END: Following copied from super method
}
 
Example #4
Source File: GrammarResource.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
@Override
protected void addSyntaxDiagnostic(List<Diagnostic> diagnostics, INode node) {
	SyntaxErrorMessage syntaxErrorMessage = node.getSyntaxErrorMessage();
	if (CardinalityAwareSyntaxErrorMessageProvider.CARDINALITY_ISSUE.equals(syntaxErrorMessage.getIssueCode())) {
		super.getWarnings().add(new XtextSyntaxDiagnostic(node));
	} else {
		super.addSyntaxDiagnostic(diagnostics, node);
	}
}
 
Example #5
Source File: ReducedXtextResourceValidator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void issueFromXtextResourceDiagnostic(Diagnostic diagnostic, Severity severity,
		IAcceptor<Issue> acceptor) {
	if (diagnostic instanceof XtextSyntaxDiagnostic) {
		super.issueFromXtextResourceDiagnostic(diagnostic, severity, acceptor);
	} else if (diagnostic instanceof XtextLinkingDiagnostic) {
		XtextLinkingDiagnostic linkingDiagnostic = (XtextLinkingDiagnostic) diagnostic;
		if (linkingDiagnostic.getCode().equals(XtextLinkingDiagnosticMessageProvider.UNRESOLVED_RULE)) {
			super.issueFromXtextResourceDiagnostic(diagnostic, severity, acceptor);
		} else if (linkingDiagnostic.getMessage().contains("reference to Grammar")) {
			super.issueFromXtextResourceDiagnostic(diagnostic, severity, acceptor);
		}
	}
}
 
Example #6
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test public void testParseWithFractionError() throws Exception {
	String model = "a.b.c.d: 1/0;";
	Resource r = getResourceFromStringAndExpect(model, 1);
	EObject parsedModel = r.getContents().get(0);
	assertNotNull(parsedModel);
	EObject firstModel = ((List<EObject>) parsedModel.eGet(modelFeature)).get(0);
	assertFalse(firstModel.eIsSet(valueFeature));
	assertEquals(1, r.getErrors().size());
	XtextSyntaxDiagnostic diag = (XtextSyntaxDiagnostic) r.getErrors().get(0);
	assertEquals(model.indexOf("1/0"), diag.getOffset());
	assertEquals(3, diag.getLength());
}
 
Example #7
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test public void testParseWithFractionErrorAndSpaces() throws Exception {
	String model = "a.b.c.d:  1 / 0 ; ";
	Resource r = getResourceFromStringAndExpect(model, 1);
	EObject parsedModel = r.getContents().get(0);
	assertNotNull(parsedModel);
	EObject firstModel = ((List<EObject>) parsedModel.eGet(modelFeature)).get(0);
	assertFalse(firstModel.eIsSet(valueFeature));
	assertEquals(1, r.getErrors().size());
	XtextSyntaxDiagnostic diag = (XtextSyntaxDiagnostic) r.getErrors().get(0);
	assertEquals(model.indexOf("1 / 0"), diag.getOffset());
	assertEquals(5, diag.getLength());
}
 
Example #8
Source File: Bug362902Test.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testNoExceptionUncaught() throws Exception {
	String modelAsString = "Hello max ! Hello peter! favourite peter";
	Model model = (Model)getModelAndExpect(modelAsString, 2);
	EList<Diagnostic> errors = model.eResource().getErrors();
	Diagnostic diagnosticSyntax = errors.get(0);
	Diagnostic diagnosticLinking = errors.get(1);
	assertTrue(diagnosticSyntax instanceof XtextSyntaxDiagnostic);
	assertTrue(diagnosticLinking instanceof XtextLinkingDiagnostic);
}
 
Example #9
Source File: AbstractValidationTest.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Validates if there is a syntax error present in the source content.
 *
 * @param sourceFileName
 *          the file name that should be associated with the parsed content, must not be {@code null}
 * @param sourceContent
 *          source, must not be {@code null}
 */
protected void assertNoSyntaxErrorsOnResource(final String sourceFileName, final CharSequence sourceContent) {
  final XtextTestSource testSource = createTestSource(sourceFileName, sourceContent.toString());
  final List<Resource.Diagnostic> errors = testSource.getModel().eResource().getErrors().stream().filter(error -> error instanceof XtextSyntaxDiagnostic).collect(Collectors.toList());
  if (!errors.isEmpty()) {
    StringBuilder sb = new StringBuilder("Syntax error is present in the test source.\nList of all found syntax errors:");
    errors.forEach(err -> sb.append("\n\t " + err.getMessage()));
    fail(sb.toString());
  }
}
 
Example #10
Source File: AbstractTypeResolverTest.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public Iterable<Resource.Diagnostic> getLinkingAndSyntaxErrors(final Resource resource) {
  final Function1<Resource.Diagnostic, Boolean> _function = (Resource.Diagnostic it) -> {
    return Boolean.valueOf(((it instanceof XtextSyntaxDiagnostic) || (it instanceof XtextLinkingDiagnostic)));
  };
  return IterableExtensions.<Resource.Diagnostic>filter(resource.getErrors(), _function);
}
 
Example #11
Source File: AbstractQuickFixTest.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Assert that the test source has no syntax error.
 */
protected void assertNoSyntaxError() {
  Assert.assertFalse("The source has syntax errors", Iterables.any(getTestSource().getXtextResource().getErrors(), Predicates.instanceOf(XtextSyntaxDiagnostic.class)));
}
 
Example #12
Source File: SCTResourceTest.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
public void testInvalidExpressionParsing() {
	State state = createState("This is not a valid expression");
	res.getContents().add(state);
	assertEquals(1, res.getSyntaxDiagnostics().size());
	assertTrue(res.getErrors().get(0) instanceof XtextSyntaxDiagnostic);
}
 
Example #13
Source File: AbstractSCTResource.java    From statecharts with Eclipse Public License 1.0 4 votes vote down vote up
protected void createDiagnostics(IParseResult parseResult, SpecificationElement semanticTarget) {
	syntaxDiagnostics.get(semanticTarget).clear();
	for (INode error : parseResult.getSyntaxErrors()) {
		syntaxDiagnostics.put(semanticTarget, new XtextSyntaxDiagnostic(error));
	}
}