Search in sources :

Example 26 with Issue

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

the class N4HeadlessCompiler method postProcessResources.

/*
	 * ===============================================================================================================
	 *
	 * PROJECT POST-PROCESSING
	 *
	 * ===============================================================================================================
	 */
/**
 * Post processing on the whole project. While validations can trigger post processing in a lazy way, they do not
 * guarantee that all resources will be fully processed. This is an issue in larger project graphs. Unlike in the
 * IDE which can at any point load AST on demand based on the TModel, the HLC does not allow to access AST after
 * unloading therefore we explicitly post process all N4JS resources in the given project.
 *
 * @param markedProject
 *            project to trigger post process.
 */
private void postProcessResources(MarkedProject markedProject) {
    if (logger.isCreateDebugOutput())
        logger.debug(" PostProcessing " + markedProject);
    Iterables.filter(markedProject.resources, resource -> resource.isLoaded()).forEach(resource -> {
        if (resource instanceof N4JSResource) {
            N4JSResource n4jsResource = (N4JSResource) resource;
            // Make sure the resource is fully postprocessed before unloading the AST. Otherwise, resolving
            // cross references to the elements inside the resources from dependent projects will fail.
            n4jsResource.performPostProcessing();
        }
    });
}
Also used : Arrays(java.util.Arrays) SortedSet(java.util.SortedSet) ListIterator(java.util.ListIterator) Inject(com.google.inject.Inject) FileUtils(org.eclipse.n4js.utils.io.FileUtils) IN4JSCore(org.eclipse.n4js.projectModel.IN4JSCore) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer) HashMultimap(com.google.common.collect.HashMultimap) TreeMultimap(com.google.common.collect.TreeMultimap) Optional(com.google.common.base.Optional) Map(java.util.Map) Path(java.nio.file.Path) Collections2(org.eclipse.n4js.utils.collections.Collections2) N4JSModel(org.eclipse.n4js.internal.N4JSModel) BiMap(com.google.common.collect.BiMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) Set(java.util.Set) IHeadlessLogger(org.eclipse.n4js.generator.headless.logging.IHeadlessLogger) ResourceDescriptionsData(org.eclipse.xtext.resource.impl.ResourceDescriptionsData) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) DelegatingIAllContainerAdapter(org.eclipse.xtext.resource.containers.DelegatingIAllContainerAdapter) List(java.util.List) FileBasedWorkspace(org.eclipse.n4js.internal.FileBasedWorkspace) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceType(org.eclipse.n4js.utils.ResourceType) CheckMode(org.eclipse.xtext.validation.CheckMode) Joiner(com.google.common.base.Joiner) Iterables(com.google.common.collect.Iterables) URI(org.eclipse.emf.common.util.URI) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) N4JSProject(org.eclipse.n4js.internal.N4JSProject) TreeSet(java.util.TreeSet) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) HashSet(java.util.HashSet) JavaIoFileSystemAccess(org.eclipse.xtext.generator.JavaIoFileSystemAccess) ImmutableList(com.google.common.collect.ImmutableList) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) LinkedList(java.util.LinkedList) GeneratorException(org.eclipse.n4js.generator.GeneratorException) XtextResource(org.eclipse.xtext.resource.XtextResource) Severity(org.eclipse.xtext.diagnostics.Severity) N4FilebasedWorkspaceResourceSetContainerState(org.eclipse.n4js.internal.N4FilebasedWorkspaceResourceSetContainerState) DecimalFormat(java.text.DecimalFormat) Lazy(org.eclipse.n4js.utils.Lazy) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) IOException(java.io.IOException) File(java.io.File) N4JSResource(org.eclipse.n4js.resource.N4JSResource) HashBiMap(com.google.common.collect.HashBiMap) Provider(com.google.inject.Provider) Issue(org.eclipse.xtext.validation.Issue) OrderedResourceDescriptionsData(org.eclipse.n4js.resource.OrderedResourceDescriptionsData) Collections(java.util.Collections) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) N4JSResource(org.eclipse.n4js.resource.N4JSResource)

Example 27 with Issue

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

the class ExportFragment method getModel.

/**
 * Get the export model that we have to process.
 *
 * @param grammar
 *          The grammar
 * @return The model
 */
private synchronized ExportModel getModel(final Grammar grammar) {
    // NOPMD NPathComplexity by wth on 24.11.10 08:22
    if (modelLoaded) {
        return model;
    }
    modelLoaded = true;
    Resource resource = grammar.eResource();
    if (resource == null) {
        return null;
    }
    final ResourceSet resourceSet = resource.getResourceSet();
    URI uri = null;
    if (getExportFileURI() != null) {
        uri = URI.createURI(getExportFileURI());
    } else {
        uri = grammar.eResource().getURI().trimFileExtension().appendFileExtension(EXPORT_FILE_EXTENSION);
    }
    try {
        resource = resourceSet.getResource(uri, true);
        final List<Issue> issues = VALIDATOR.validate(resource, LOGGER);
        for (final Issue issue : issues) {
            if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
                throw new WorkflowInterruptedException(NLS.bind(Messages.ExportFragment_EXPORT_ERRORS, uri));
            }
        }
        if (resource.getContents().size() == 0) {
            return null;
        }
        model = (ExportModel) resource.getContents().get(0);
        return model;
    } catch (final ClasspathUriResolutionException e) {
        // Resource does not exist.
        if (getExportFileURI() != null) {
            // NOPMD PreserveStackTrace by wth on 24.11.10 08:27
            throw new WorkflowInterruptedException(NLS.bind(Messages.ExportFragment_NO_EXPORT_FILE, uri));
        }
        // No file found at implicit location: work with a null model, generating code that implements the default behavior.
        return null;
    }
}
Also used : 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) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 28 with Issue

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

the class ValidMarkerUpdateJob method validate.

/**
 * Validate the given resource and create the corresponding markers. The CheckMode is a constant calculated from the constructor
 * parameters.
 *
 * @param resourceValidator
 *          the resource validator (not null)
 * @param file
 *          the EFS file (not null)
 * @param resource
 *          the EMF resource (not null)
 * @param monitor
 *          the monitor (not null)
 */
protected void validate(final IResourceValidator resourceValidator, final IFile file, final Resource resource, final IProgressMonitor monitor) {
    try {
        // $NON-NLS-1$
        monitor.subTask("validating " + file.getName());
        final List<Issue> list = resourceValidator.validate(resource, checkMode, getCancelIndicator(monitor));
        if (list != null) {
            // resourceValidator.validate returns null if canceled (and not an empty list)
            file.deleteMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_ZERO);
            file.deleteMarkers(MarkerTypes.NORMAL_VALIDATION, true, IResource.DEPTH_ZERO);
            if (performExpensiveValidation) {
                file.deleteMarkers(MarkerTypes.EXPENSIVE_VALIDATION, true, IResource.DEPTH_ZERO);
            }
            for (final Issue issue : list) {
                markerCreator.createMarker(issue, file, MarkerTypes.forCheckType(issue.getType()));
            }
        }
    } catch (final CoreException e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        monitor.worked(1);
    }
}
Also used : Issue(org.eclipse.xtext.validation.Issue) CoreException(org.eclipse.core.runtime.CoreException)

Example 29 with Issue

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

the class ValidValidatorFragment method getValidModel.

/**
 * Gets the valid model.
 *
 * @param grammar
 *          the grammar
 * @return the valid model
 */
private ValidModel getValidModel(final Grammar grammar) {
    if (model != null) {
        return model;
    }
    Resource resource = null;
    final String name = GrammarUtil.getName(grammar) + '.' + XTEXT_EXTENSION;
    URI uri;
    for (final Resource res : grammar.eResource().getResourceSet().getResources()) {
        if (res.getURI() != null && name.equals(EmfResourceUtil.getFileName(res.getURI()))) {
            resource = res;
            break;
        }
    }
    if (getValidURI() == null) {
        Assert.isNotNull(resource, NLS.bind(Messages.RESOURCE_NOT_FOUND, name));
        uri = resource.getURI().trimFileExtension().appendFileExtension(VALID_EXTENSION);
    } else {
        uri = URI.createURI(getValidURI());
    }
    resource = resource.getResourceSet().getResource(uri, true);
    final List<Issue> issues = VALIDATOR.validate(resource, LOGGER);
    for (final Issue issue : issues) {
        if (issue.isSyntaxError() || issue.getSeverity() == Severity.ERROR) {
            throw new WorkflowInterruptedException(NLS.bind(Messages.ERROR_FOUND, uri.toString()));
        }
    }
    model = (ValidModel) resource.getContents().get(0);
    return model;
}
Also used : Issue(org.eclipse.xtext.validation.Issue) WorkflowInterruptedException(org.eclipse.emf.mwe.core.WorkflowInterruptedException) Resource(org.eclipse.emf.ecore.resource.Resource) URI(org.eclipse.emf.common.util.URI)

Example 30 with Issue

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

the class DefaultValidationJob method runInternal.

@Override
protected IStatus runInternal(final IProgressMonitor monitor) {
    try {
        if (!resource.isLoaded())
            return Status.CANCEL_STATUS;
        if (resource instanceof AbstractSCTResource) {
            relinkModel(monitor, (AbstractSCTResource) resource);
        }
        if (monitor.isCanceled())
            return Status.CANCEL_STATUS;
        TransactionalValidationRunner runner = new TransactionalValidationRunner(validator, resource, CheckMode.FAST_ONLY, new CancelIndicator() {

            public boolean isCanceled() {
                return monitor.isCanceled();
            }
        });
        TransactionalEditingDomain editingDomain = TransactionUtil.getEditingDomain(resource);
        if (editingDomain == null)
            return Status.CANCEL_STATUS;
        try {
            editingDomain.runExclusive(runner);
        } catch (Throwable ex) {
            // Since xtext 2.8 this may throw an OperationCanceledError
            return Status.CANCEL_STATUS;
        }
        final List<Issue> issues = runner.getResult();
        if (issues == null)
            return Status.CANCEL_STATUS;
        validationIssueProcessor.processIssues(issues, monitor);
    } catch (Exception ex) {
        ex.printStackTrace();
        return new Status(IStatus.ERROR, DiagramActivator.PLUGIN_ID, ex.getMessage());
    }
    return Status.OK_STATUS;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) Issue(org.eclipse.xtext.validation.Issue) AbstractSCTResource(org.yakindu.sct.model.sgraph.resource.AbstractSCTResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) ExecutionException(org.eclipse.core.commands.ExecutionException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException)

Aggregations

Issue (org.eclipse.xtext.validation.Issue)105 Test (org.junit.Test)38 XtextResource (org.eclipse.xtext.resource.XtextResource)33 Resource (org.eclipse.emf.ecore.resource.Resource)21 IResourceValidator (org.eclipse.xtext.validation.IResourceValidator)18 URI (org.eclipse.emf.common.util.URI)16 List (java.util.List)14 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)12 IssueResolution (org.eclipse.xtext.ui.editor.quickfix.IssueResolution)12 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)11 ArrayList (java.util.ArrayList)9 Severity (org.eclipse.xtext.diagnostics.Severity)9 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)9 EObject (org.eclipse.emf.ecore.EObject)8 IOException (java.io.IOException)7 CoreException (org.eclipse.core.runtime.CoreException)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 StringInputStream (org.eclipse.xtext.util.StringInputStream)7 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)6 IssueResolutionAcceptor (org.eclipse.xtext.ui.editor.quickfix.IssueResolutionAcceptor)6