Search in sources :

Example 6 with MarkerCreator

use of org.eclipse.xtext.ui.editor.validation.MarkerCreator in project dsl-devkit by dsldevkit.

the class CheckMarkerUpdateJob method run.

/**
 * {@inheritDoc}
 */
@Override
protected IStatus run(final IProgressMonitor monitor) {
    // Let's start (number of task = number of resource * 2 (loading + validating))
    // $NON-NLS-1$
    monitor.beginTask("", 2 * this.uris.size());
    for (final URI uri : this.uris) {
        // Last chance to cancel before next validation
        if (monitor.isCanceled()) {
            return Status.CANCEL_STATUS;
        }
        final IResourceServiceProvider serviceProvider = serviceProviderRegistry.getResourceServiceProvider(uri);
        if (serviceProvider == null) {
            // This may happen for non-Xtext resources in ice entities
            if (LOGGER.isDebugEnabled()) {
                // $NON-NLS-1$
                LOGGER.debug(MessageFormat.format("Could not validate {0}: no resource service provider found", uri.toString()));
            }
            // Skip to next URI
            continue;
        }
        final IResourceValidator resourceValidator = serviceProvider.getResourceValidator();
        final IStorage2UriMapper uriMapper = serviceProvider.get(IStorage2UriMapper.class);
        final MarkerCreator markerCreator = serviceProvider.get(MarkerCreator.class);
        // Get the file; only local files will be re-validated, derived files are ignored
        final IFile iFile = getFileFromStorageMapper(uriMapper, uri);
        if (iFile == null) {
            // no storage mapping found for this URI
            continue;
        }
        if (resourceValidator == null) {
            // $NON-NLS-1$
            LOGGER.error(MessageFormat.format("Could not validate {0}: no resource validator found", iFile.getName()));
        } else if (iFile != null) {
            // $NON-NLS-1$
            monitor.subTask("loading " + iFile.getName());
            // Don't try to evaluate resource set before it has been checked that the storage provider contains a mapping
            // for current uri
            final ResourceSet resourceSet = getResourceSet(uriMapper, uri);
            // Load the corresponding resource
            boolean loaded = false;
            Resource eResource = null;
            try {
                eResource = resourceSet.getResource(uri, false);
                if ((eResource == null) || (eResource != null && !eResource.isLoaded())) {
                    // if the resource does not exist in the resource set, or is not loaded yet
                    // load it.
                    eResource = resourceSet.getResource(uri, true);
                    loaded = true;
                }
                monitor.worked(1);
            // CHECKSTYLE:OFF
            } catch (final RuntimeException e) {
                // CHECKSTYLE:ON
                // $NON-NLS-1$
                LOGGER.error(MessageFormat.format("{0} could not be validated.", iFile.getName()), e);
            } finally {
                if (eResource != null) {
                    validateAndCreateMarkers(resourceValidator, markerCreator, iFile, eResource, monitor);
                    // $NON-NLS-1$
                    LOGGER.debug("Validated " + uri);
                    if (loaded) {
                        // NOPMD
                        // unload any resource that was previously loaded as part of this loop.
                        eResource.unload();
                    }
                }
            }
        }
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IFile(org.eclipse.core.resources.IFile) IResourceValidator(org.eclipse.xtext.validation.IResourceValidator) IStorage2UriMapper(org.eclipse.xtext.ui.resource.IStorage2UriMapper) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) MarkerCreator(org.eclipse.xtext.ui.editor.validation.MarkerCreator)

Aggregations

MarkerCreator (org.eclipse.xtext.ui.editor.validation.MarkerCreator)6 IFile (org.eclipse.core.resources.IFile)4 Issue (org.eclipse.xtext.validation.Issue)4 List (java.util.List)3 XtextResource (org.eclipse.xtext.resource.XtextResource)3 IResource (org.eclipse.core.resources.IResource)2 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 IssueResolutionProvider (org.eclipse.xtext.ui.editor.quickfix.IssueResolutionProvider)2 AnnotationIssueProcessor (org.eclipse.xtext.ui.editor.validation.AnnotationIssueProcessor)2 IResourceValidator (org.eclipse.xtext.validation.IResourceValidator)2 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IMarker (org.eclipse.core.resources.IMarker)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 URI (org.eclipse.emf.common.util.URI)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)1