org.eclipse.xtext.resource.IResourceDescription Java Examples

The following examples show how to use org.eclipse.xtext.resource.IResourceDescription. 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: OrderedResourceDescriptionsData.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected void registerDescription(final IResourceDescription description,
		final Map<QualifiedName, Object> target) {

	for (final IEObjectDescription object : description.getExportedObjects()) {
		final QualifiedName lowerCase = object.getName().toLowerCase();
		final Object existing = target.put(lowerCase, description);
		if (existing != null && existing != description) {
			Set<IResourceDescription> set = null;
			if (existing instanceof IResourceDescription) {
				// The linked hash set is the difference comparing to the super class.
				set = Sets.newLinkedHashSetWithExpectedSize(2);
				set.add((IResourceDescription) existing);
			} else {
				set = (Set<IResourceDescription>) existing;
			}
			set.add(description);
			target.put(lowerCase, set);
		}
	}
}
 
Example #2
Source File: ResourceSetBasedResourceDescriptions.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IResourceDescription getResourceDescription(URI uri) {
	if (data != null) {
		return data.getResourceDescription(uri);
	}
	Resource resource = resourceSet.getResource(uri, false);
	if (resource == null)
		return null;
	IResourceServiceProvider resourceServiceProvider = registry.getResourceServiceProvider(uri);
	if (resourceServiceProvider == null)
		return null;
	Manager manager = resourceServiceProvider.getResourceDescriptionManager();
	if (manager == null)
		return null;
	return manager.getResourceDescription(resource);
}
 
Example #3
Source File: ResourceSetBasedResourceDescriptions.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Iterable<IResourceDescription> getAllResourceDescriptions() {
	if (data != null) {
		return data.getAllResourceDescriptions();
	}
	return Iterables.filter(new Iterable<IResourceDescription>() {
		@Override
		public Iterator<IResourceDescription> iterator() {
			return new AbstractIterator<IResourceDescription>() {
				int index = 0;
				List<Resource> resources = resourceSet.getResources();

				@Override
				protected IResourceDescription computeNext() {
					if (resources.size() <= index)
						return endOfData();
					Resource resource = resources.get(index);
					index++;
					return getResourceDescription(resource.getURI());
				}
			};
		}
	}, Predicates.notNull());

}
 
Example #4
Source File: EcoreResourceDescriptionManagerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testPerformance() throws Exception {
		GenericResourceDescriptionManager manager = getEmfResourceDescriptionsManager();
		Collection<String> uris = ImmutableList.copyOf(EPackage.Registry.INSTANCE.keySet());
		for(String uri: uris) {
			EPackage pack = EPackage.Registry.INSTANCE.getEPackage(uri);
			IResourceDescription description = manager.getResourceDescription(pack.eResource());
			assertNotNull(description);
			for(int i = 0; i < 10; i++) {
				Iterator<EObject> iter = EcoreUtil.getAllProperContents(pack, true);
				while(iter.hasNext()) {
					EObject next = iter.next();
					if (next instanceof ENamedElement) {
						String name = ((ENamedElement) next).getName();
//						Iterable<IEObjectDescription> objects = 
						description.getExportedObjects(EcorePackage.Literals.EOBJECT, QualifiedName.create(name), false);
//						assertFalse(name + " - " + uri + " - " + next, Iterables.isEmpty(objects));
					}
				}
			}
		}
	}
 
Example #5
Source File: WorkspaceSymbolService.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public List<? extends SymbolInformation> getSymbols(
	String query,
	IResourceAccess resourceAccess,
	IResourceDescriptions indexData,
	CancelIndicator cancelIndicator
) {
	List<SymbolInformation> result = new LinkedList<>();
	for (IResourceDescription resourceDescription : indexData.getAllResourceDescriptions()) {
		operationCanceledManager.checkCanceled(cancelIndicator);
		IResourceServiceProvider resourceServiceProvider = registry.getResourceServiceProvider(resourceDescription.getURI());
		if (resourceServiceProvider != null) {
			DocumentSymbolService documentSymbolService = resourceServiceProvider.get(DocumentSymbolService.class);
			if (documentSymbolService != null) {
				result.addAll(documentSymbolService.getSymbols(resourceDescription, query, resourceAccess, cancelIndicator));
			}
		}
	}
	return result;
}
 
Example #6
Source File: SimpleResourceDescriptionsBasedContainerManager.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IContainer getContainer(IResourceDescription desc, IResourceDescriptions resourceDescriptions) {
	if (delegate.shouldUseProjectDescriptionBasedContainers(resourceDescriptions)) {
		return delegate.getContainer(desc, resourceDescriptions);
	}
	ResourceDescriptionsBasedContainer result = new ResourceDescriptionsBasedContainer(resourceDescriptions) {
		// this used to be the default implementation, which is wrong.
		// we fixed the orginal and moved the wrong impl here since old clients might see much worse performance with the new impl. 
		@Override
		public boolean hasResourceDescription(URI uri) {
			return true;
		}
	};
	result.setUriToDescriptionCacheEnabled(false);
	return result;
}
 
Example #7
Source File: XBuildManager.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Run a full build on the workspace
 *
 * @return the delta.
 */
public List<IResourceDescription.Delta> doInitialBuild(List<ProjectDescription> projects,
		CancelIndicator indicator) {

	lspLogger.log("Initial build ...");

	ProjectBuildOrderInfo projectBuildOrderInfo = projectBuildOrderInfoProvider.get();
	ProjectBuildOrderIterator pboIterator = projectBuildOrderInfo.getIterator(projects);
	printBuildOrder();

	List<IResourceDescription.Delta> result = new ArrayList<>();

	while (pboIterator.hasNext()) {
		ProjectDescription description = pboIterator.next();
		String projectName = description.getName();
		XProjectManager projectManager = workspaceManager.getProjectManager(projectName);
		XBuildResult partialresult = projectManager.doInitialBuild(indicator);
		result.addAll(partialresult.getAffectedResources());
	}

	lspLogger.log("... initial build done.");

	return result;
}
 
Example #8
Source File: StatefulResourceDescription.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected ImmutableList<IEObjectDescription> copyExportedObjects(IResourceDescription original) {
	return ImmutableList.copyOf(Iterables.filter(Iterables.transform(original.getExportedObjects(), new Function<IEObjectDescription, IEObjectDescription>() {
		@Override
		public IEObjectDescription apply(IEObjectDescription from) {
			if (from == null)
				return null;
			EObject proxy = from.getEObjectOrProxy();
			if (proxy == null)
				return null;
			if (proxy.eIsProxy())
				return from;
			InternalEObject result = (InternalEObject) EcoreUtil.create(from.getEClass());
			result.eSetProxyURI(EcoreUtil.getURI(from.getEObjectOrProxy()));
			Map<String, String> userData = null;
			for(String key: from.getUserDataKeys()) {
				if (userData == null) {
					userData = Maps.newHashMapWithExpectedSize(2);
				}
				userData.put(key, from.getUserData(key));
			}
			return EObjectDescription.create(from.getName(), result, userData);
		}
	}), Predicates.notNull()));
}
 
Example #9
Source File: OpenFileContext.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
protected IResourceDescription createResourceDescription() {
	IResourceDescription.Manager resourceDescriptionManager = getResourceDescriptionManager(mainURI);
	IResourceDescription newDesc = resourceDescriptionManager.getResourceDescription(mainResource);
	// sanitize resource description
	// NOTE: it seems that resource descriptions created by the resource description manager may contain mutable
	// state (e.g. user data implemented as a ForwardingMap with lazily initialized content) and hold references to
	// the resource they were created from (i.e. 'mainResource' in this case); this means they are (1) not thread
	// safe and (2) may leak EObjects from one open file context into another or to the outside. The following line
	// seems to fix that, but requires access to restricted Xtext API:
	SerializableResourceDescription newDesc2 = SerializableResourceDescription.createCopy(newDesc);
	return newDesc2;
}
 
Example #10
Source File: ResourceDescriptionsData.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public void addDescription(URI uri, IResourceDescription newDescription) {
	removeDescription(uri);
	if (newDescription != null) {
		resourceDescriptionMap.put(uri, newDescription);
		registerDescription(newDescription, lookupMap);
	}
}
 
Example #11
Source File: DeltaConverter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 * @deprecated 
 * @see #convertNewType(URI, IType, List)
 */
@Deprecated
protected void convertNewType(IType type, List<IResourceDescription.Delta> result) {
	if (!isDerived(type)) {
		result.add(createContentChangeDelta(null, createTypeResourceDescription(type.getFullyQualifiedName())));
	}
}
 
Example #12
Source File: LazyLinkingResourceTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
 	public IResourceDescription getResourceDescription(URI uri) {
 		final IResourceDescription resourceDescription = super.getResourceDescription(uri);
 		Iterable<IEObjectDescription> objects = resourceDescription.getExportedObjects();
 		for (IEObjectDescription ieObjectDescription : objects) {
	EObject eObject = ieObjectDescription.getEClass().getEPackage().getEFactoryInstance().create(ieObjectDescription.getEClass());
	((InternalEObject)eObject).eSetProxyURI(ieObjectDescription.getEObjectURI());
	try {
		Field field = ieObjectDescription.getClass().getDeclaredField("element");
		field.setAccessible(true);
		field.set(ieObjectDescription, eObject);
	} catch (Exception e) {}
}
return resourceDescription;
 	}
 
Example #13
Source File: OpenFilesManager.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
protected synchronized void updateSharedDirtyState(IResourceDescription newDesc) {
	// update my dirty state instance
	URI newDescURI = newDesc.getURI();
	sharedDirtyState.addDescription(newDescURI, newDesc);
	// update dirty state instances in the context of each open file (except the one that caused the change)
	for (URI currURI : openFiles.keySet()) {
		if (currURI.equals(newDescURI)) {
			continue;
		}
		runInOpenFileContextVoid(currURI, "updateSharedDirtyState in open file", (ofc, ci) -> {
			ofc.onDirtyStateChanged(newDesc, ci);
		});
	}
}
 
Example #14
Source File: DoUpdateImplementation.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private void removeDeleted() {
	if (!toBeDeleted.isEmpty()) {
		for (final URI uri : toBeDeleted) {
			newData.removeDescription(uri);
			final IResourceDescription oldDescription = state.getResourceDescription(uri);
			if (oldDescription != null) {
				allDeltas.add(new DefaultResourceDescriptionDelta(oldDescription, null));
			}
		}
	}
}
 
Example #15
Source File: ValidationJobSchedulerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IResourceDescription getResourceDescription(URI normalizedURI) {
	if (noDocumentDescription)
		return null;
	if (documentURI.equals(normalizedURI))
		return documentResource;
	throw new UnsupportedOperationException();
}
 
Example #16
Source File: JdtQueuedBuildData.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean namesIntersect(IResourceDescription resourceDescription, Set<QualifiedName> names) {
	if (resourceDescription == null) {
		return false;
	}
	for (IEObjectDescription objectDescription : resourceDescription.getExportedObjects()) {
		if (names.contains(objectDescription.getQualifiedName())) {
			return true;
		}
	}
	return false;
}
 
Example #17
Source File: DefaultResourceDescriptionManager.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public boolean isAffected(Delta delta, IResourceDescription candidate) throws IllegalArgumentException {
	if (!hasChanges(delta, candidate))
		return false;
	Set<QualifiedName> names = Sets.newHashSet();
	addExportedNames(names,delta.getOld());
	addExportedNames(names,delta.getNew());
	return !Collections.disjoint(names, getImportedNames(candidate));
}
 
Example #18
Source File: AbstractResourceDescriptionChangeEventSource.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.5
 */
@Override
public void notifyListeners(IResourceDescription.Event event) {
	if (event == null)
		throw new IllegalArgumentException("event may not be null");
	for(IResourceDescription.Event.Listener listener: listenerList) {
		listener.descriptionsChanged(event);
	}
}
 
Example #19
Source File: N4JSDirtyStateEditorSupport.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private Set<URI> collectDeltaURIs(Event event) {
	Set<URI> deltaURIs = Sets.newHashSet();
	for (IResourceDescription.Delta delta : event.getDeltas()) {
		deltaURIs.add(delta.getUri());
	}
	return deltaURIs;
}
 
Example #20
Source File: ChunkedResourceDescriptions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
	int numChunks = in.readInt();
	for (int i = 0; i < numChunks; i++) {
		String chunkName = in.readUTF();
		int numDescriptions = in.readInt();
		List<IResourceDescription> descriptions = new ArrayList<IResourceDescription>(numDescriptions);
		for (int j = 0; j < numDescriptions; j++)
			descriptions.add((IResourceDescription) in.readObject());
		chunk2resourceDescriptions.put(chunkName, new ResourceDescriptionsData(descriptions));
	}
}
 
Example #21
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IResourceDescription createResourceDescription(Resource testResource) {
	DefaultResourceDescriptionStrategy strategy = new DefaultResourceDescriptionStrategy();
	strategy.setQualifiedNameProvider(new IQualifiedNameProvider.AbstractImpl() {
		@Override
		public QualifiedName getFullyQualifiedName(EObject obj) {
			String name = SimpleAttributeResolver.NAME_RESOLVER.apply(obj);
			return (name != null) ? QualifiedName.create(name) : null;
		}
	});
	strategy.setLazyURIEncoder(new LazyURIEncoder());
	IResourceDescription resourceDescription = new DefaultResourceDescription(testResource, strategy);
	return resourceDescription;
}
 
Example #22
Source File: DirtyStateManager.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.22
 */
protected void announceDirtyStateChanged(IResourceDescription prevDirtyDescription, IDirtyResource dirtyResource) {
	// avoid putting a dirtyResource into the map that wasn't managed before
	if (managedResources.replace(dirtyResource.getURI(), dirtyResource) != null) {
		notifyListeners(prevDirtyDescription, dirtyResource);
	}
}
 
Example #23
Source File: AbstractContainer.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<IEObjectDescription> getExportedObjectsByObject(final EObject object) {
	URI resourceURI = EcoreUtil2.getPlatformResourceOrNormalizedURI(object).trimFragment();
	IResourceDescription description = getResourceDescription(resourceURI);
	if (description == null)
		return Collections.emptyList();
	return description.getExportedObjectsByObject(object);
}
 
Example #24
Source File: EagerResourceSetBasedResourceDescriptions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private IResourceDescription computeResourceDescription(URI uri) {
	Resource resource = resourceSet.getResource(uri, false);
	if (resource == null)
		return null;
	IResourceServiceProvider resourceServiceProvider = registry.getResourceServiceProvider(uri);
	if (resourceServiceProvider == null)
		return null;
	Manager manager = resourceServiceProvider.getResourceDescriptionManager();
	if (manager == null)
		return null;
	return manager.getResourceDescription(resource);
}
 
Example #25
Source File: ResourceDescriptionProviderTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testEmptyFile() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("//package my.pack;");
  _builder.newLine();
  final Procedure1<IResourceDescription> _function = (IResourceDescription it) -> {
    Assert.assertTrue(IterableExtensions.isEmpty(it.getExportedObjects()));
  };
  this.resultsIn(_builder, _function);
}
 
Example #26
Source File: AbstractCachingResourceDescriptionManager.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns all resolved imported names from a given resource description.
 *
 * @param candidate
 *          the resource description
 * @return a collection of names
 */
protected Pair<Collection<QualifiedName>, Collection<QualifiedName>> getResolvedAndUnresolvedImportedNames(final IResourceDescription candidate) {
  Iterable<QualifiedName> importedNames = candidate.getImportedNames();
  Set<QualifiedName> resolved = Sets.newHashSetWithExpectedSize(Iterables.size(importedNames));
  Set<QualifiedName> unresolved = Sets.newHashSet();
  for (QualifiedName name : importedNames) {
    if (QualifiedNames.isUnresolvedName(name)) {
      unresolved.add(name);
    } else {
      resolved.add(name);
    }
  }
  return Tuples.<Collection<QualifiedName>, Collection<QualifiedName>> pair(resolved, unresolved);
}
 
Example #27
Source File: LoadOnDemandResourceDescriptions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<IResourceDescription> getAllResourceDescriptions() {
	return Iterables.filter(Iterables.transform(validUris, new Function<URI, IResourceDescription>() {
		@Override
		public IResourceDescription apply(URI from) {
			return getResourceDescription(from);
		}
	}), Predicates.notNull());
}
 
Example #28
Source File: ResourceDescriptionsUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns all URIs in the given resource descriptions object.
 *
 * @param descriptions
 *          {@link IResourceDescriptions} to get the URIs for
 * @return set of all URIs
 */
public static Set<URI> getAllURIs(final IResourceDescriptions descriptions) {
  Set<URI> allURIs = Sets.newHashSetWithExpectedSize(Iterables.size(descriptions.getAllResourceDescriptions()));
  for (IResourceDescription desc : descriptions.getAllResourceDescriptions()) {
    allURIs.add(desc.getURI());
  }
  return allURIs;
}
 
Example #29
Source File: StorageAwareResourceDescriptionManager.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IResourceDescription getResourceDescription(Resource resource) {
	if (resource instanceof StorageAwareResource) {
		IResourceDescription result = ((StorageAwareResource) resource).getResourceDescription();
		if (result != null) {
			return result;
		}
	}
	return super.getResourceDescription(resource);
}
 
Example #30
Source File: BuilderParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.7
 */
protected void doBuild(List<IResourceDescription.Delta> deltas, 
		Map<String, OutputConfiguration> outputConfigurations,
		Map<OutputConfiguration, Iterable<IMarker>> generatorMarkers, IBuildContext context,
		EclipseResourceFileSystemAccess2 access, IProgressMonitor progressMonitor) throws CoreException {
	final int numberOfDeltas = deltas.size();
	SubMonitor subMonitor = SubMonitor.convert(progressMonitor, numberOfDeltas);
	SubMonitor currentMonitor = null;
	int clusterIndex = 0;
	for (int i = 0; i < numberOfDeltas; i++) {
		IResourceDescription.Delta delta = deltas.get(i);
		
		if (subMonitor.isCanceled()) {
			throw new OperationCanceledException();
		}
		currentMonitor = subMonitor.split(1);
		access.setMonitor(currentMonitor);
		if (logger.isDebugEnabled()) {
			logger.debug("Compiling " + delta.getUri() + " (" + i + " of " + numberOfDeltas + ")");
		}
		if (delta.getNew() != null && !clusteringPolicy.continueProcessing(context.getResourceSet(), delta.getUri(), clusterIndex)) {
			clearResourceSet(context.getResourceSet());
			clusterIndex = 0;
		}

		Set<IFile> derivedResources = getDerivedResources(delta, outputConfigurations, generatorMarkers);
		access.setPostProcessor(getPostProcessor(delta, context, derivedResources));
		
		if (doGenerate(delta, context, access)) {
			clusterIndex++;
			access.flushSourceTraces();
		}
		
		cleanDerivedResources(delta, derivedResources, context, access, currentMonitor);
	}
}