use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.
the class PolyfillValidatorFragment method holdsSinglePolyfillSource.
/**
* Constraints 129 (Applying Polyfills) No member must be filled by more than one polyfill.
*/
private boolean holdsSinglePolyfillSource(PolyfillValidationState state) {
EList<TMember> myPolyMember = state.polyType.getOwnedMembers();
// a) find references to the filled type
// b) check, that they are in the same Project
// c) search for clashing contributions.
XtextResource res = (XtextResource) state.polyType.eResource();
IResourceDescriptions index = resourceDescriptionsProvider.getResourceDescriptions(res);
// a+b) all polyfills to same calssifier in same project:
IContainer container = containerManager.getContainer(res.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(res), index);
// Iterable over all exported Polyfills
Iterable<IEObjectDescription> iterEObj = container.getExportedObjects(TypesPackage.Literals.TCLASSIFIER, N4TSQualifiedNameProvider.getPolyfillFQN(state.filledType, qualifiedNameProvider), false);
// collection of involved TModules for each Member.
ListMultimap<TMember, TModule> clashProviders = LinkedListMultimap.create();
for (IEObjectDescription pivotObjectDescription : iterEObj) {
EObject eob = pivotObjectDescription.getEObjectOrProxy();
// Resolve
if (eob.eIsProxy()) {
eob = EcoreUtil.resolve(eob, res);
}
if (eob == state.polyType) {
// saw myself .-.
continue;
}
EList<TMember> pivotPolyMember = ((TClassifier) eob).getOwnedMembers();
ListMultimap<TMember, TMember> clashing = findClashingMembersByName(myPolyMember, pivotPolyMember);
for (TMember myMember : clashing.keySet()) {
// only interested in the module, so first is sufficient
clashProviders.put(myMember, clashing.get(myMember).get(0).getContainingModule());
}
}
List<TMember> sortedMembers = clashProviders.keySet().stream().sorted().collect(Collectors.toList());
for (TMember myMember : sortedMembers) {
// Combine list of Modules involved in the polyfill clash.
String uris = Stream.concat(Stream.of(myMember.getContainingModule()), clashProviders.get(myMember).stream()).map(u -> u.getQualifiedName().toString()).sorted().reduce("", (a, b) -> a + PREFIX_LIST + b);
if (uris.startsWith(PREFIX_LIST))
uris = uris.substring(PREFIX_LIST.length());
int lastPrefix_idx = uris.lastIndexOf(PREFIX_LIST);
if (lastPrefix_idx >= 0) {
StringBuffer sb = new StringBuffer(uris);
uris = sb.replace(lastPrefix_idx, lastPrefix_idx + PREFIX_LIST.length(), " and ").toString();
}
// give Qualified name filled in Property.
String memberAxis = myMember.getContainingType().getName() + "." + myMember.getName();
// Issue on filled Member-name declaration:
String msg = IssueCodes.getMessageForCLF_POLYFILL_MULTIPOLYFILLS_MEMBER_CONFLICT(uris, memberAxis);
state.host.addIssue(msg, myMember.getAstElement(), N4JSPackage.Literals.PROPERTY_NAME_OWNER__DECLARED_NAME, IssueCodes.CLF_POLYFILL_MULTIPOLYFILLS_MEMBER_CONFLICT);
}
return true;
}
use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.
the class FindReferenceHelper method findReferences.
/**
* @return all references to the given declaration. Respect editor states.
*/
public List<EObject> findReferences(EObject declaration) {
declaration = getDeclaration(declaration);
TargetURIs targets = getTargets(declaration);
Resource eResource = declaration.eResource();
SimpleResourceAccess resourceAccess = new SimpleResourceAccess(eResource.getResourceSet());
IResourceDescriptions index = resourceDescriptionsProvider.getResourceDescriptions(eResource);
ReferenceAcceptor acceptor = new ReferenceAcceptor();
referenceFinder.findAllReferences(targets, resourceAccess, index, acceptor, null);
return acceptor.results;
}
use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.
the class ProjectCompareHelper method compareModules.
/**
* Get the comparison for a module in a specific implementation specified by it's ID.
*
* @param module
* can be either Implementation or API
* @param implementationID
* the current context to compare with
*
* @param includePolyfills
* {@code true} if polyfills should be considered as the part of the API and the implementation.
* Otherwise {@code false}.
* @return a comparison between API and implementation with implementationID, or <code>null</code> if no project
* could be found, the module is not part of an API/IMPL ...
*/
public ProjectComparisonEntry compareModules(TModule module, String implementationID, final boolean includePolyfills) {
Optional<? extends IN4JSProject> opt = n4jsCore.findProject(module.eResource().getURI());
if (!opt.isPresent()) {
return null;
}
IN4JSProject project = opt.get();
IN4JSProject implProject = null;
IN4JSProject apiProject = null;
TModule apiModule = null;
TModule apiImplModule = null;
if (!project.getImplementationId().isPresent()) {
// this is NOT an implementation project, so assume we have the api
// need to load the correct implementation. Since there might be multiple implementors
// TODO replace with central instance
final ApiImplMapping mapping = ApiImplMapping.of(n4jsCore);
implProject = mapping.getImpl(project.getProjectId(), implementationID);
if (implProject == null) {
// no implementation found.
return null;
}
apiProject = project;
apiModule = module;
URI impUri = artifactHelper.findArtifact(implProject, apiModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JS_FILE_EXTENSION));
if (impUri != null) {
IResourceDescriptions xtextIndex = n4jsCore.getXtextIndex(module.eResource().getResourceSet());
IResourceDescription resourceDescription = xtextIndex.getResourceDescription(impUri);
if (resourceDescription != null) {
apiImplModule = n4jsCore.loadModuleFromIndex(module.eResource().getResourceSet(), resourceDescription, false);
} else {
if (logger.isDebugEnabled()) {
logger.debug("...ouch nothing in index for " + impUri);
}
Resource implResource = module.eResource().getResourceSet().getResource(impUri, true);
apiImplModule = (TModule) implResource.getContents().get(1);
// .get(1);
if (logger.isDebugEnabled()) {
logger.debug("TModule loaded from Resource: " + apiImplModule);
}
}
} else {
// No implementation present.
if (logger.isDebugEnabled()) {
logger.debug("No implementation given. For " + apiModule.getQualifiedName());
}
}
} else {
// check that the implementation ID matches.
if (implementationID.equals(project.getImplementationId().get())) {
implProject = project;
apiImplModule = module;
ImmutableList<? extends IN4JSProject> apiProjects = implProject.getImplementedProjects();
labelA: for (IN4JSProject ap : apiProjects) {
URI apiURI = artifactHelper.findArtifact(ap, apiImplModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JSD_FILE_EXTENSION));
if (apiURI != null) {
IResourceDescriptions xtextIndex = n4jsCore.getXtextIndex(apiImplModule.eResource().getResourceSet());
IResourceDescription resourceDescription = xtextIndex.getResourceDescription(apiURI);
if (resourceDescription != null) {
apiModule = n4jsCore.loadModuleFromIndex(apiImplModule.eResource().getResourceSet(), resourceDescription, false);
if (apiModule != null)
break labelA;
}
}
}
} else {
return null;
}
}
if (apiModule != null) {
return compareModules(apiProject, apiModule, implProject, apiImplModule, includePolyfills);
} else {
// no apiModule --> this is not an implementation of API.
return null;
}
}
use of org.eclipse.xtext.resource.IResourceDescriptions in project n4js by eclipse.
the class TestDiscoveryHelper method collectTestLocations.
/**
* Most clients should use method {@link #collectTests(URI...)} instead!
* <p>
* Low-level method to collect all test modules, i.e. N4JS files containing classes containing at least one method
* annotated with @Test, as {@link IResourceDescription}s.
*/
private Stream<URI> collectTestLocations(final IResourceDescriptions index, final ResourceSet resSet, final URI location) {
if (null == location) {
return Stream.empty();
}
// does location point to an N4JS project?
if (isProject(location)) {
// yes
// --> collect all test modules (files containing test classes) located in source containers of type "test"
final IN4JSProject p = n4jsCore.create(location);
return p.getSourceContainers().stream().filter(IN4JSSourceContainer::isTest).flatMap(// note: IN4JSSourceContainer is an Iterable<URI>
TestDiscoveryHelper::stream).filter(// filter out everything but N4JS files.
uri -> isTestFile(uri)).filter(uri -> isTestModule(resSet, index.getResourceDescription(uri)));
}
// does location point to an n4js file?
final IResourceDescription resDesc = index.getResourceDescription(location.trimFragment());
if (resDesc != null) {
// yes --> is it a test module? (i.e. does it contain test classes and the class is not abstract?)
if (isTestModule(resSet, resDesc)) {
// yes --> is it contained in a source container of type "test"?
final IN4JSSourceContainer srcContainer = n4jsCore.findN4JSSourceContainer(location.trimFragment()).orNull();
if (srcContainer != null && srcContainer.isTest()) {
// return location with fragment! (if any)
return Stream.of(location);
}
}
return Stream.empty();
}
// does location point to a source container (or sub-folder)?
final IN4JSSourceContainer srcContainer = n4jsCore.findN4JSSourceContainer(location).orNull();
if (srcContainer != null) {
// yes --> is this a source container of type "test"?
if (srcContainer.isTest()) {
// yes --> collect all test modules (files containing test classes) in this source container
final String locationStr = location.toString();
return // note: IN4JSSourceContainer is an Iterable<URI>
stream(srcContainer).filter(// TODO improve?
uri -> uri.toString().startsWith(locationStr)).filter(uri -> isTestModule(resSet, index.getResourceDescription(uri)));
}
return Stream.empty();
}
// invalid location URI
return Stream.empty();
}
use of org.eclipse.xtext.resource.IResourceDescriptions in project statecharts by Yakindu.
the class STextGlobalScopeProvider method getScope.
/**
* Overidden to avoid scope nesting which is not required and slows down because
* of shadowing testing.
*/
@Override
protected IScope getScope(Resource resource, boolean ignoreCase, EClass type, Predicate<IEObjectDescription> filter) {
final LinkedHashSet<URI> uniqueImportURIs = getImportedUris(resource);
IResourceDescriptions descriptions = getResourceDescriptions(resource, uniqueImportURIs);
List<URI> urisAsList = Lists.newArrayList(uniqueImportURIs);
Collections.reverse(urisAsList);
List<IEObjectDescription> objectDescriptions = new ArrayList<IEObjectDescription>();
for (URI uri : urisAsList) {
IScope scope = createLazyResourceScope(IScope.NULLSCOPE, uri, descriptions, type, filter, ignoreCase);
Iterables.addAll(objectDescriptions, scope.getAllElements());
}
return MapBasedScope.createScope(IScope.NULLSCOPE, objectDescriptions);
}
Aggregations