Java Code Examples for org.eclipse.xtext.resource.IResourceDescription#Manager

The following examples show how to use org.eclipse.xtext.resource.IResourceDescription#Manager . 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: XIndexer.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Compute deltas for resources affected by the given <code>newDeltas</code> and register them in the given
 * <code>newIndex</code>.
 *
 * @param index
 *            the current index; will be changed by this method.
 * @param remainingURIs
 *            set of URIs that were not processed yet.
 * @param newDeltas
 *            deltas representing the resources processed during the most recent build iteration.
 * @param allDeltas
 *            deltas representing all resources processed so far, including {@link XBuildRequest#getExternalDeltas()
 *            external deltas}.
 * @param context
 *            the build context.
 * @return list of deltas representing the affected resources.
 */
public List<Delta> computeAndIndexAffected(ResourceDescriptionsData index, Set<URI> remainingURIs,
		Collection<Delta> newDeltas, Collection<Delta> allDeltas, XBuildContext context) {

	ResourceDescriptionsData originalIndex = context.getOldState().getResourceDescriptions();
	List<URI> affectedURIs = new ArrayList<>();
	for (URI uri : remainingURIs) {
		IResourceServiceProvider resourceServiceProvider = context.getResourceServiceProvider(uri);
		IResourceDescription.Manager manager = resourceServiceProvider.getResourceDescriptionManager();
		IResourceDescription resourceDescription = originalIndex.getResourceDescription(uri);
		if (isAffected(resourceDescription, manager, newDeltas, allDeltas, index)) {
			affectedURIs.add(uri);
		}
	}

	List<Delta> affectedDeltas = getDeltasForChangedResources(affectedURIs, originalIndex, context);
	for (IResourceDescription.Delta delta : affectedDeltas) {
		index.register(delta);
	}
	return affectedDeltas;
}
 
Example 2
Source File: XIndexer.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Index the given resource.
 *
 * @param isPreIndexing
 *            can be evaluated to produce different index entries depending on the phase
 */
protected IResourceDescription.Delta addToIndex(Resource resource, boolean isPreIndexing,
		ResourceDescriptionsData oldIndex, XBuildContext context) {
	this.operationCanceledManager.checkCanceled(context.getCancelIndicator());
	if (context.getResourceSet() != resource.getResourceSet()) {
		// we are seeing an out-of-sequence resource - don't index it
		return null;
	}
	URI uri = resource.getURI();
	IResourceServiceProvider serviceProvider = context.getResourceServiceProvider(uri);
	IResourceDescription.Manager manager = serviceProvider.getResourceDescriptionManager();
	IResourceDescription newDescription = manager.getResourceDescription(resource);
	IResourceDescription toBeAdded = new XIndexer.XResolvedResourceDescription(newDescription);
	IResourceDescription.Delta delta = manager
			.createDelta(oldIndex != null ? oldIndex.getResourceDescription(uri) : null, toBeAdded);
	return delta;
}
 
Example 3
Source File: MonitoredClusteringBuilderState.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Add deltas for the removed resources.
 *
 * @param deletedUris
 *          URIs of the removed resources
 * @param deltas
 *          Deltas
 * @param savedDescriptions
 *          previously saved old resource descriptions
 * @param savedGeneratedObjectsInfo
 *          previously saved old generated objects info
 */
protected void addDeletedURIsToDeltas(final Set<URI> deletedUris, final Set<Delta> deltas, final Map<URI, IResourceDescription> savedDescriptions, final Map<URI, DerivedObjectAssociations> savedGeneratedObjectsInfo) {
  for (final URI uri : deletedUris) {
    final IResourceDescription oldDescription = getSavedResourceDescription(savedDescriptions, uri);
    if (oldDescription != null) {
      final IResourceDescription.Manager manager = getResourceDescriptionManager(uri);
      if (manager != null) {
        Delta delta = manager.createDelta(oldDescription, null);
        if (delta instanceof AbstractResourceDescriptionDelta) {
          // For languages that support extended delta, pass generated object info to builder participants using delta
          // All languages that manage life cycle of generated objects must support extended delta
          final DerivedObjectAssociations generatedObjectsInfo = getSavedDerivedObjectAssociations(savedGeneratedObjectsInfo, delta.getUri());
          ((AbstractResourceDescriptionDelta) delta).addExtensionData(DerivedObjectAssociations.class, generatedObjectsInfo);
        }
        deltas.add(delta);
      }
    }
  }
}
 
Example 4
Source File: AbstractPolymorphicScopeProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Return the visible containers given a resource.
 *
 * @param resource
 *          the resource. Must not be {@code null}
 * @return The list of visible containers.
 */
protected List<IContainer> getVisibleContainers(final XtextResource resource) {
  URI uri = resource.getURI();
  List<IContainer> result = null;
  final ResourceCache<URI, List<IContainer>> cache = CacheManager.getInstance().getOrCreateResourceCache("AbstractPolymorphicScopeProvider#visibleContainers", resource); //$NON-NLS-1$
  if (cache != null) {
    result = cache.get(uri);
    if (result != null) {
      return result;
    }
  }

  final IResourceServiceProvider resourceServiceProvider = resource.getResourceServiceProvider();
  final IResourceDescription.Manager descriptionManager = resourceServiceProvider.getResourceDescriptionManager();
  final IContainer.Manager containerManager = resourceServiceProvider.getContainerManager();

  final IResourceDescription description = descriptionManager.getResourceDescription(resource);
  final IResourceDescriptions resourceDescriptions = getResourceDescriptions(resource);
  result = containerManager.getVisibleContainers(description, resourceDescriptions);
  if (cache != null) {
    cache.set(uri, result);
  }
  return result;
}
 
Example 5
Source File: DocumentBasedDirtyResource.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void initiallyProcessResource(XtextResource resource) {
	IResourceServiceProvider serviceProvider = resource.getResourceServiceProvider();
	if (serviceProvider != null) {
		IResourceDescription.Manager descriptionManager = serviceProvider.getResourceDescriptionManager();
		if (descriptionManager != null) {
			final IResourceDescription description = descriptionManager.getResourceDescription(resource);
			if (description != null)
				copyState(description);							
		}
	}
}
 
Example 6
Source File: DefaultReferenceFinder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Deprecated
protected Map<EObject, URI> createExportedElementsMap(final Resource resource) {
	return new ForwardingMap<EObject, URI>() {

		private Map<EObject, URI> delegate;
		
		@Override
		protected Map<EObject, URI> delegate() {
			if (delegate != null) {
				return delegate;
			}
			URI uri = EcoreUtil2.getPlatformResourceOrNormalizedURI(resource);
			IResourceServiceProvider resourceServiceProvider = getServiceProviderRegistry().getResourceServiceProvider(uri);
			if (resourceServiceProvider == null) {
				return delegate = Collections.emptyMap();
			}
			IResourceDescription.Manager resourceDescriptionManager = resourceServiceProvider.getResourceDescriptionManager();
			if (resourceDescriptionManager == null) {
				return delegate = Collections.emptyMap();
			}
			IResourceDescription resourceDescription = resourceDescriptionManager.getResourceDescription(resource);
			Map<EObject, URI> exportedElementMap = newIdentityHashMap();
			if (resourceDescription != null) {
				for (IEObjectDescription exportedEObjectDescription : resourceDescription.getExportedObjects()) {
					EObject eObject = resource.getEObject(exportedEObjectDescription.getEObjectURI().fragment());
					if (eObject != null)
						exportedElementMap.put(eObject, exportedEObjectDescription.getEObjectURI());
				}
			}
			return delegate = exportedElementMap;
		}

		
	};
}
 
Example 7
Source File: N4ClusteringBuilderState.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
protected IResourceDescription.Manager getResourceDescriptionManager(URI uri) {
	IResourceServiceProvider resourceServiceProvider = managerRegistry.getResourceServiceProvider(uri);
	if (resourceServiceProvider == null) {
		return null;
	}
	return resourceServiceProvider.getResourceDescriptionManager();
}
 
Example 8
Source File: ClusteringBuilderState.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected IResourceDescription.Manager getResourceDescriptionManager(URI uri) {
    IResourceServiceProvider resourceServiceProvider = managerRegistry.getResourceServiceProvider(uri);
    if (resourceServiceProvider == null) {
        return null;
    }
    return resourceServiceProvider.getResourceDescriptionManager();
}
 
Example 9
Source File: OpenFileContext.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
protected void updateIndex(Collection<? extends IResourceDescription> changedDescs, Set<URI> removedURIs,
		ContainerStructureSnapshot newContainerStructure, CancelIndicator cancelIndicator) {

	// update my cached state

	List<IResourceDescription.Delta> allDeltas = createDeltas(changedDescs, removedURIs);

	allDeltas.forEach(index::register);

	ContainerStructureSnapshot oldContainerStructure = containerStructure;
	containerStructure = newContainerStructure;

	// refresh if I am affected by the changes

	boolean isAffected = !containerStructure.equals(oldContainerStructure);

	if (!isAffected) {
		IResourceDescription.Manager rdm = getResourceDescriptionManager(mainURI);
		if (rdm == null) {
			return;
		}
		IResourceDescription candidateDesc = index.getResourceDescription(mainURI);
		if (rdm instanceof AllChangeAware) {
			isAffected = ((AllChangeAware) rdm).isAffectedByAny(allDeltas, candidateDesc, index);
		} else {
			List<IResourceDescription.Delta> changedDeltas = allDeltas.stream()
					.filter(d -> d.haveEObjectDescriptionsChanged())
					.collect(Collectors.toList());
			isAffected = rdm.isAffected(changedDeltas, candidateDesc, index);
		}
	}

	if (isAffected) {
		refreshOpenFile(cancelIndicator);
	}
}
 
Example 10
Source File: DirtyStateEditorSupport.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
protected IResourceDescription.Manager getResourceDescriptionManagerIfOwnLanguage(XtextResource resource) {
	IResourceServiceProvider rsp = resourceServiceProviderRegistry.getResourceServiceProvider(resource.getURI());
	if (rsp == null)
		return null;
	String uriLanguageName = rsp.get(LanguageInfo.class).getLanguageName();
	String resourceLanguageName = resource.getLanguageName();
	if (!uriLanguageName.equals(resourceLanguageName))
		return null;
	return getResourceDescriptionManager(resource.getURI());
}
 
Example 11
Source File: GlobalResourceDescriptionProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public IResourceDescription getResourceDescription(Resource resource) {
	IResourceServiceProvider resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(resource.getURI());
	if (resourceServiceProvider == null)
		return null;
	IResourceDescription.Manager manager = resourceServiceProvider.getResourceDescriptionManager();
	if (manager == null)
		return null;
	IResourceDescription description = manager.getResourceDescription(resource);
	return description;
}
 
Example 12
Source File: DefaultResourceServiceProvider.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public IResourceDescription.Manager getResourceDescriptionManager() {
	return resourceDescriptionManager;
}
 
Example 13
Source File: DirtyStateEditorSupport.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.7
 */
protected IResourceDescription.Manager getResourceDescriptionManager(URI resourceURI) {
	return resourceServiceProviderRegistry.getResourceServiceProvider(resourceURI).get(DirtyStateResourceDescription.Manager.class);
}
 
Example 14
Source File: ClusteringBuilderState.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Create new resource descriptions for a set of resources given by their URIs.
 *
 * @param buildData
 *            The underlying data for the write operation.
 * @param oldState
 *            The old index
 * @param newState
 *            The new index
 * @param monitor
 *            The progress monitor used for user feedback
 */
protected void writeNewResourceDescriptions(
        BuildData buildData,
        IResourceDescriptions oldState,
        CurrentDescriptions newState,
        final IProgressMonitor monitor) {
    int index = 0;
    ResourceSet resourceSet = buildData.getResourceSet();
    Set<URI> toBeUpdated = buildData.getToBeUpdated();
    final SubMonitor subMonitor = SubMonitor.convert(monitor, "Write new resource descriptions", toBeUpdated.size() + 1); // TODO: NLS
    IProject currentProject = getBuiltProject(buildData);
    LoadOperation loadOperation = null;
    try {
    	compilerPhases.setIndexing(resourceSet, true);
        loadOperation = globalIndexResourceLoader.create(resourceSet, currentProject);
        loadOperation.load(toBeUpdated);

        while (loadOperation.hasNext()) {
            if (subMonitor.isCanceled()) {
                loadOperation.cancel();
                throw new OperationCanceledException();
            }

            if (!clusteringPolicy.continueProcessing(resourceSet, null, index)) {
                clearResourceSet(resourceSet);
            }

            URI uri = null;
            Resource resource = null;
            try {
                LoadResult loadResult = loadOperation.next();
                uri = loadResult.getUri();
                resource = addResource(loadResult.getResource(), resourceSet);
                subMonitor.subTask("Writing new resource description " + resource.getURI().lastSegment());
                if (LOGGER.isDebugEnabled()) {
                	LOGGER.debug("Writing new resource description " + uri);
                }

                final IResourceDescription.Manager manager = getResourceDescriptionManager(uri);
                if (manager != null) {
                    // We don't care here about links, we really just want the exported objects so that we can link in the
                    // next phase.
                    final IResourceDescription description = manager.getResourceDescription(resource);
                    final IResourceDescription copiedDescription = new CopiedResourceDescription(description);
                    // We also don't care what kind of Delta we get here; it's just a temporary transport vehicle. That interface
                    // could do with some clean-up, too, because all we actually want to do is register the new resource
                    // description, not the delta.
                    newState.register(new DefaultResourceDescriptionDelta(oldState.getResourceDescription(uri), copiedDescription));
                    buildData.queueURI(uri);
                }
            } catch (final RuntimeException ex) {
                if(ex instanceof LoadOperationException) {
                    uri = ((LoadOperationException) ex).getUri();
                }
                if (uri == null) {
                    LOGGER.error("Error loading resource", ex); //$NON-NLS-1$
                } else {
                    if (resourceSet.getURIConverter().exists(uri, Collections.emptyMap())) {
                        LOGGER.error("Error loading resource from: " + uri.toString(), ex); //$NON-NLS-1$
                    }
                    if (resource != null) {
                        resourceSet.getResources().remove(resource);
                    }
                    final IResourceDescription oldDescription = oldState.getResourceDescription(uri);
                    if (oldDescription != null) {
                        newState.register(new DefaultResourceDescriptionDelta(oldDescription, null));
                    }
                }
                // If we couldn't load it, there's no use trying again: do not add it to the queue
            }
            index++;
            subMonitor.split(1);
        }
    } finally {
    	compilerPhases.setIndexing(resourceSet, false);
        if(loadOperation != null) loadOperation.cancel();
    }
}
 
Example 15
Source File: DirtyStateResourceDescription.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Inject
public Manager(IResourceDescription.Manager delegate) {
	this.delegate = delegate;
}
 
Example 16
Source File: CommandRegistryTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public IResourceDescription.Manager getResourceDescriptionManager() {
	return noImpl.getResourceDescriptionManager();
}
 
Example 17
Source File: N4ClusteringBuilderState.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
protected IResourceDescription.Manager getResourceDescriptionManager(Resource resource, URI uri) {
	if (resource instanceof XtextResource) {
		return ((XtextResource) resource).getResourceServiceProvider().getResourceDescriptionManager();
	}
	return getResourceDescriptionManager(uri);
}
 
Example 18
Source File: N4ClusteringBuilderState.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Put all resources that depend on some changes onto the queue of resources to be processed. Updates notInDelta by
 * removing all URIs put into the queue.
 *
 * @param allRemainingURIs
 *            URIs that were not considered by prior operations.
 * @param oldState
 *            State before the build
 * @param newState
 *            The current state
 * @param changedDeltas
 *            the deltas that have changed {@link IEObjectDescription}s
 * @param allDeltas
 *            All deltas
 * @param buildData
 *            the underlying data for this build run.
 * @param monitor
 *            The progress monitor used for user feedback
 */
protected void queueAffectedResources(
		Set<URI> allRemainingURIs,
		IResourceDescriptions oldState,
		CurrentDescriptions newState,
		Collection<Delta> changedDeltas,
		Collection<Delta> allDeltas,
		BuildData buildData,
		final IProgressMonitor monitor) {
	if (allDeltas.isEmpty()) {
		return;
	}
	final SubMonitor progress = SubMonitor.convert(monitor, allRemainingURIs.size());
	Iterator<URI> iter = allRemainingURIs.iterator();
	while (iter.hasNext()) {
		if (progress.isCanceled()) {
			throw new OperationCanceledException();
		}
		final URI candidateURI = iter.next();
		final IResourceDescription candidateDescription = oldState.getResourceDescription(candidateURI);
		final IResourceDescription.Manager manager = getResourceDescriptionManager(candidateURI);
		if (candidateDescription == null || manager == null) {
			// If there is no description in the old state, there's no need to re-check this over and over.
			iter.remove();
		} else {
			boolean affected;
			if ((manager instanceof IResourceDescription.Manager.AllChangeAware)) {
				affected = ((AllChangeAware) manager).isAffectedByAny(allDeltas, candidateDescription, newState);
			} else {
				if (changedDeltas.isEmpty()) {
					affected = false;
				} else {
					affected = manager.isAffected(changedDeltas, candidateDescription, newState);
				}
			}
			if (affected) {
				buildData.queueURI(candidateURI);
				// since the candidate is affected by any of the currently changed resources, we disable
				// the module data of the candidate to ensure that no code will see it later on by accident
				// Related tests:
				// - IncrementalBuilderCornerCasesPluginTest#testMissingReloadBug()
				// - ReproduceInvalidIndexPluginTest
				if (!N4JSGlobals.PACKAGE_JSON.equals(candidateURI.lastSegment())) {
					ResourceDescriptionWithoutModuleUserData noModuleData = new ResourceDescriptionWithoutModuleUserData(
							candidateDescription);
					newState.register(manager.createDelta(candidateDescription, noModuleData));
					// also we ensure that we do run a subsequent build.
					buildData.requestRebuild();
				}
				iter.remove();
			}
		}
		progress.worked(1);
	}
}
 
Example 19
Source File: N4JSRuntimeModule.java    From n4js with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Customized so that it produces {@link N4JSResourceDescription}.
 *
 * @return Class<{@link N4JSResourceDescriptionManager}>
 */
public Class<? extends IResourceDescription.Manager> bindIResourceDescriptionManager() {
	return N4JSResourceDescriptionManager.class;
}
 
Example 20
Source File: AbstractScopingTest.java    From dsl-devkit with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Gets the resource description for a given Xtext resource.
 *
 * @param resource
 *          the resource
 * @return the resource description
 */
protected final IResourceDescription getResourceDescription(final XtextResource resource) {
  final IResourceServiceProvider resourceServiceProvider = resource.getResourceServiceProvider();
  final IResourceDescription.Manager descriptionManager = resourceServiceProvider.getResourceDescriptionManager();
  return descriptionManager.getResourceDescription(resource);
}