Search in sources :

Example 56 with Issue

use of org.eclipse.xtext.validation.Issue in project n4js by eclipse.

the class N4HeadlessCompiler method validateProject.

/*
	 * ===============================================================================================================
	 *
	 * PROJECT VALIDATION
	 *
	 * ===============================================================================================================
	 */
/**
 * Validates all non-external Xtext resources of the given project. Prints issues and adds them to the given issue
 * acceptor.
 *
 * @param markedProject
 *            the project to validate
 * @param recorder
 *            the progress recorder
 * @param issueAcceptor
 *            the issue acceptor
 * @throws N4JSCompileErrorException
 *             if an error occurs during validation
 */
private void validateProject(MarkedProject markedProject, N4ProgressStateRecorder recorder, IssueAcceptor issueAcceptor) throws N4JSCompileErrorException {
    if (logger.isVerbose())
        logger.info(" Validating project " + markedProject);
    IssueCollector issueCollector = new IssueCollector();
    IssueFilter issueFilter = new IssueFilter(issueCollector, issue -> issue.getSeverity() == Severity.ERROR);
    issueAcceptor = new IssueAcceptorTee(issueAcceptor, issueFilter);
    // validation TODO see IDE-1426 redesign validation calls with generators
    for (Resource resource : markedProject.resources) {
        if (// is Xtext resource
        resource instanceof XtextResource && // is validating
        (!n4jsCore.isNoValidate(resource.getURI())) && // not in external folder
        (!markedProject.externalResources.contains(resource))) {
            if (logger.isCreateDebugOutput())
                logger.debug("   Validating resource " + resource.getURI());
            XtextResource xtextResource = (XtextResource) resource;
            IResourceValidator validator = xtextResource.getResourceServiceProvider().getResourceValidator();
            List<Issue> issues = validator.validate(xtextResource, CheckMode.ALL, CancelIndicator.NullImpl);
            if (!issues.isEmpty()) {
                recorder.markResourceIssues(resource, issues);
                issueAcceptor.acceptAll(issues);
                issues.stream().forEach(logger::issue);
            }
        }
    }
    // Projects should not compile if there are severe errors:
    if (!isKeepOnCompiling()) {
        failOnErrors(issueCollector.getCollectedIssues(), markedProject.project.getProjectId());
    }
}
Also used : Issue(org.eclipse.xtext.validation.Issue) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Example 57 with Issue

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

the class ModelValidator method validate.

/**
 * Validate the resource and notify the logger of any error and warning.
 *
 * @param resource
 *          the resource
 * @param logger
 *          the logger
 * @return the list of issues found in the resource
 */
public List<Issue> validate(final Resource resource, final Logger logger) {
    EcoreUtil.resolveAll(resource);
    final List<Issue> issues = resourceValidator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl);
    for (final Issue issue : issues) {
        logIssue(resource, issue, logger);
    }
    return issues;
}
Also used : Issue(org.eclipse.xtext.validation.Issue)

Example 58 with Issue

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

the class DefaultCheckQuickfixProvider method getFixMethodPredicate.

/**
 * Returns a method predicate indicating whether a given method is an executable quickfix method. Both Check
 * quickfix methods and native quickfix methods are considered.
 *
 * @param issueCode
 *          the issue code
 * @return the fix method predicate
 * @see com.avaloq.tools.ddk.check.runtime.quickfix.CoreFix CoreFix annotation
 * @see org.eclipse.xtext.ui.editor.quickfix.Fix Fix annotation
 */
@Override
protected Predicate<Method> getFixMethodPredicate(final String issueCode) {
    return new Predicate<Method>() {

        @Override
        public boolean apply(final Method input) {
            CoreFix coreFixAnnotation = input.getAnnotation(CoreFix.class);
            Fix fixAnnotation = input.getAnnotation(Fix.class);
            if (coreFixAnnotation == null && fixAnnotation == null) {
                // Definitely no candidate
                return false;
            }
            final boolean typesMatch = Void.TYPE == input.getReturnType() && input.getParameterTypes().length == 2 && input.getParameterTypes()[0].isAssignableFrom(Issue.class);
            boolean result;
            if (coreFixAnnotation != null) {
                result = coreFixAnnotation != null && issueCode.equals(coreFixAnnotation.value()) && typesMatch && input.getParameterTypes()[1].isAssignableFrom(CoreIssueResolutionAcceptor.class);
            } else {
                result = fixAnnotation != null && issueCode.equals(fixAnnotation.value()) && typesMatch && input.getParameterTypes()[1].isAssignableFrom(IssueResolutionAcceptor.class);
            }
            return result;
        }
    };
}
Also used : Issue(org.eclipse.xtext.validation.Issue) CoreFix(com.avaloq.tools.ddk.check.runtime.quickfix.CoreFix) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix) CoreFix(com.avaloq.tools.ddk.check.runtime.quickfix.CoreFix) Method(java.lang.reflect.Method) Predicate(com.google.common.base.Predicate)

Example 59 with Issue

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

the class FormatFragmentUtil method getFormatModel.

/**
 * Retrieve the format model associated with a given grammar.
 * <p>
 * <em>Note</em>: Expected to either be in same folder with the same name (except for the extension) or in the SRC outlet.
 * </p>
 *
 * @param grammar
 *          the grammar, must not be {@code null}
 * @param context
 *          xpand execution context, must not be {@code null}
 * @return the format model, or {@code null} if the resource could not be loaded
 * @throws FileNotFoundException
 *           thrown if the format file could not be found
 */
@SuppressWarnings("PMD.NPathComplexity")
public static FormatConfiguration getFormatModel(final Grammar grammar, final XpandExecutionContext context) throws FileNotFoundException {
    Variable resourceUriVariable = context.getVariable("resourceUri");
    if (resourceUriVariable == null) {
        return null;
    }
    URI uri = (URI) resourceUriVariable.getValue();
    final Resource grammarResource = grammar.eResource();
    final ResourceSet resourceSet = grammarResource.getResourceSet();
    Resource formatResource = null;
    try {
        formatResource = resourceSet.getResource(uri, true);
    } catch (final ClasspathUriResolutionException e) {
        // make another attempt
        uri = getDefaultFormatLocation(grammar, context);
        try {
            formatResource = resourceSet.getResource(uri, true);
        } catch (WrappedException e1) {
            formatResource = resourceSet.getResource(uri, false);
            if (formatResource != null) {
                resourceSet.getResources().remove(formatResource);
            }
            // NOPMD
            throw new FileNotFoundException(uri.toString());
        }
    }
    if (formatResource == null) {
        throw new FileNotFoundException(uri.toString());
    }
    final List<Issue> issues = getModelValidator().validate(formatResource, LOG);
    for (final Issue issue : issues) {
        if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
            throw new WorkflowInterruptedException("Errors found in " + uri.toString() + ": " + issue.getMessage());
        }
    }
    return formatResource.getContents().size() == 0 ? null : (FormatConfiguration) formatResource.getContents().get(0);
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) Variable(org.eclipse.xtend.expression.Variable) Issue(org.eclipse.xtext.validation.Issue) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) WorkflowInterruptedException(org.eclipse.emf.mwe.core.WorkflowInterruptedException) Resource(org.eclipse.emf.ecore.resource.Resource) FileNotFoundException(java.io.FileNotFoundException) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 60 with Issue

use of org.eclipse.xtext.validation.Issue in project statecharts by Yakindu.

the class DefaultValidationIssueStore method processIssues.

@Override
public synchronized void processIssues(List<Issue> issues, IProgressMonitor monitor) {
    final Multimap<String, SCTIssue> newVisibleIssues = ArrayListMultimap.create();
    for (Issue issue : issues) {
        if (issue instanceof SCTIssue) {
            String semanticURI = ((SCTIssue) issue).getSemanticURI();
            newVisibleIssues.put(semanticURI, (SCTIssue) issue);
        }
    }
    final Multimap<String, SCTIssue> oldVisibleIssues = ArrayListMultimap.create();
    synchronized (visibleIssues) {
        oldVisibleIssues.putAll(visibleIssues);
        // normal and expensive checks will not be executed by the live
        // validation, so persistent markers have to be copied
        Iterable<SCTIssue> persistentIssues = Iterables.filter(visibleIssues.values(), new Predicate<SCTIssue>() {

            public boolean apply(SCTIssue input) {
                CheckType type = input.getType();
                Severity severity = input.getSeverity();
                return CheckType.NORMAL == type || CheckType.EXPENSIVE == type || Severity.INFO == severity;
            }
        });
        for (SCTIssue sctIssue : persistentIssues) {
            newVisibleIssues.put(sctIssue.getSemanticURI(), sctIssue);
        }
        visibleIssues.clear();
        visibleIssues.putAll(newVisibleIssues);
    }
    SetView<String> changes = Sets.symmetricDifference(oldVisibleIssues.keySet(), newVisibleIssues.keySet());
    for (String string : changes) {
        notifyListeners(string);
    }
    SetView<String> intersection = Sets.intersection(oldVisibleIssues.keySet(), newVisibleIssues.keySet());
    for (String string : intersection) {
        if (changedSeverity(string, oldVisibleIssues, newVisibleIssues) || changedErrorCount(string, oldVisibleIssues, newVisibleIssues)) {
            notifyListeners(string);
        }
    }
}
Also used : SCTIssue(org.yakindu.sct.model.sgraph.ui.validation.SCTIssue) Issue(org.eclipse.xtext.validation.Issue) SCTIssue(org.yakindu.sct.model.sgraph.ui.validation.SCTIssue) CheckType(org.eclipse.xtext.validation.CheckType) Severity(org.eclipse.xtext.diagnostics.Severity)

Aggregations

Issue (org.eclipse.xtext.validation.Issue)108 Test (org.junit.Test)40 XtextResource (org.eclipse.xtext.resource.XtextResource)35 Resource (org.eclipse.emf.ecore.resource.Resource)23 List (java.util.List)20 IResourceValidator (org.eclipse.xtext.validation.IResourceValidator)20 URI (org.eclipse.emf.common.util.URI)16 ArrayList (java.util.ArrayList)12 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)12 IFile (org.eclipse.core.resources.IFile)11 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)11 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)11 IssueResolution (org.eclipse.xtext.ui.editor.quickfix.IssueResolution)11 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)10 Severity (org.eclipse.xtext.diagnostics.Severity)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)8 StringInputStream (org.eclipse.xtext.util.StringInputStream)8 EObject (org.eclipse.emf.ecore.EObject)6