Search in sources :

Example 66 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class ImportsAwareReferenceProposalCreator method lookupCrossReference.

/**
 * Retrieves possible reference targets from scope, including erroneous solutions (e.g., not visible targets). This
 * list is further filtered here. This is a general pattern: Do not change or modify scoping for special content
 * assist requirements, instead filter here.
 *
 * @param proposalFactory
 *            usually this will be an instance of
 *            {@link AbstractJavaBasedContentProposalProvider.DefaultProposalCreator DefaultProposalCreator}.
 * @param filter
 *            by default an instance of {@link N4JSCandidateFilter} will be provided here.
 */
@SuppressWarnings("javadoc")
public void lookupCrossReference(EObject model, EReference reference, ContentAssistContext context, ICompletionProposalAcceptor acceptor, Predicate<IEObjectDescription> filter, Function<IEObjectDescription, ICompletionProposal> proposalFactory) {
    if (model != null) {
        final IScope scope = ((IContentAssistScopeProvider) scopeProvider).getScopeForContentAssist(model, reference);
        // iterate over candidates, filter them, and create ICompletionProposals for them
        final Iterable<IEObjectDescription> candidates = scope.getAllElements();
        // don't use candidates.forEach since we want an early exit
        for (IEObjectDescription candidate : candidates) {
            if (!acceptor.canAcceptMoreProposals())
                return;
            if (filter.apply(candidate)) {
                QualifiedName qfn = candidate.getQualifiedName();
                String tmodule = null;
                if (qfn.getSegmentCount() >= 2) {
                    tmodule = qfn.getSegment(qfn.getSegmentCount() - 2);
                }
                // In case of main module, adjust the qualified name, e.g. index.Element -> react.Element
                IN4JSProject project = n4jsCore.findProject(candidate.getEObjectURI()).orNull();
                QualifiedName candidateName;
                if (project != null && tmodule != null && tmodule.equals(project.getMainModule())) {
                    candidateName = QualifiedName.create(project.getProjectId(), candidate.getQualifiedName().getLastSegment().toString());
                } else {
                    candidateName = candidate.getQualifiedName();
                }
                final ICompletionProposal proposal = getProposal(candidate, model, scope, reference, context, filter, proposalFactory);
                if (proposal instanceof ConfigurableCompletionProposal && candidate.getName().getSegmentCount() > 1) {
                    ConfigurableCompletionProposal castedProposal = (ConfigurableCompletionProposal) proposal;
                    castedProposal.setAdditionalData(FQNImporter.KEY_QUALIFIED_NAME, candidateName);
                    // Original qualified name is the qualified name before adjustment
                    castedProposal.setAdditionalData(FQNImporter.KEY_ORIGINAL_QUALIFIED_NAME, candidate.getQualifiedName());
                }
                acceptor.accept(proposal);
            }
        }
    }
}
Also used : ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IContentAssistScopeProvider(org.eclipse.n4js.scoping.IContentAssistScopeProvider) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 67 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class ExternalLibraryTreeContentProvider method updateElement.

@Override
public void updateElement(final Object parent, final int index) {
    if (treeViewerRef.isPresent()) {
        final TreeViewer treeViewer = treeViewerRef.get();
        if (parent instanceof Iterable) {
            final Object child = Iterables.get((Iterable<?>) parent, index);
            treeViewer.replace(parent, index, child);
            if (child instanceof URI) {
                treeViewer.setChildCount(child, Iterables.size(getProjects((URI) child)));
            }
        } else if (parent instanceof URI) {
            final IN4JSProject child = Iterables.get(getProjects((URI) parent), index);
            treeViewer.replace(parent, index, child);
        }
    }
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) TreeViewer(org.eclipse.jface.viewers.TreeViewer) URI(java.net.URI)

Example 68 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class ProjectCompareHelper method createEntries.

// may be made public later
private ProjectComparisonEntry createEntries(ProjectComparison root, IN4JSProject api, IN4JSProject[] impls, ResourceSet resourceSet, IResourceDescriptions index) {
    final ProjectComparisonEntry entry = new ProjectComparisonEntry(root, api, impls);
    for (IN4JSSourceContainer currSrcConti : api.getSourceContainers()) {
        for (URI uri : currSrcConti) {
            final String uriStr = uri.toString();
            if (uriStr.endsWith("." + N4JSGlobals.N4JS_FILE_EXTENSION) || uriStr.endsWith("." + N4JSGlobals.N4JSD_FILE_EXTENSION)) {
                final IResourceDescription resDesc = index.getResourceDescription(uri);
                final TModule moduleApi = getModuleFrom(resourceSet, resDesc);
                if (moduleApi != null) {
                    final TModule[] moduleImpls = new TModule[impls.length];
                    for (int idx = 0; idx < impls.length; idx++) {
                        final IN4JSProject projectImpl = impls[idx];
                        if (projectImpl != null)
                            moduleImpls[idx] = findImplementation(moduleApi, projectImpl, resourceSet, index);
                        else
                            moduleImpls[idx] = null;
                    }
                    createEntries(entry, -1, moduleApi, moduleImpls, false);
                }
            }
        }
    }
    return entry;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) TModule(org.eclipse.n4js.ts.types.TModule) URI(org.eclipse.emf.common.util.URI) IN4JSSourceContainer(org.eclipse.n4js.projectModel.IN4JSSourceContainer)

Example 69 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class ProjectCompareHelper method createComparison.

/**
 * First step in comparing API and implementation projects. This method will collect all API projects, the types
 * declared there and their members and relate them to the corresponding types and members in all available
 * implementation projects.
 * <p>
 * The second step of comparison is to compare an individual type/member to a corresponding type/member of a
 * particular implementation via method {@link #compareApiImpl(ProjectComparisonEntry, int)}.
 * <p>
 * Usually, this second step is performed lazily when method <code>#compareApiImpl()</code> is invoked; however, by
 * setting <code>fullCompare</code> to <code>true</code>, this is performed up-front here causing all results to be
 * stored in a cache in the entries, and then subsequent calls to <code>#compareApiImpl()</code> will just read from
 * that cache.
 * <p>
 * Returns <code>null</code> in case of error and adds human-readable error messages to the given list.
 */
public ProjectComparison createComparison(boolean fullCompare, List<String> addErrorMessagesHere) {
    final ResourceSet resourceSet = n4jsCore.createResourceSet(Optional.absent());
    final IResourceDescriptions index = n4jsCore.getXtextIndex(resourceSet);
    final ApiImplMapping mapping = ApiImplMapping.of(n4jsCore);
    if (mapping.hasErrors()) {
        if (addErrorMessagesHere != null)
            addErrorMessagesHere.addAll(mapping.getErrorMessages());
        return null;
    }
    final List<String> allImplIds = mapping.getAllImplIds();
    final int implCount = allImplIds.size();
    final ProjectComparison root = new ProjectComparison(allImplIds.toArray(new String[implCount]));
    for (String currApiId : mapping.getApiIds()) {
        final IN4JSProject currApi = mapping.getApi(currApiId);
        final IN4JSProject[] currImpls = new IN4JSProject[implCount];
        for (int idx = 0; idx < implCount; idx++) {
            final String currImplId = allImplIds.get(idx);
            currImpls[idx] = mapping.getImpl(currApiId, currImplId);
        }
        createEntries(root, currApi, currImpls, resourceSet, index);
    }
    // compare all implementations in all entries and store in cache (if requested)
    if (fullCompare) {
        root.getAllEntries().forEach(currE -> {
            for (int implIdx = 0; implIdx < implCount; implIdx++) compareApiImpl(currE, implIdx);
        });
    }
    return root;
}
Also used : IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 70 with IN4JSProject

use of org.eclipse.n4js.projectModel.IN4JSProject in project n4js by eclipse.

the class ApiImplMapping method getErrorMessages.

/**
 * In {@link #hasErrors() case of error}, returns human-readable error messages. Otherwise, an empty list will be
 * returned.
 */
public List<String> getErrorMessages() {
    final List<String> msgs = new ArrayList<>();
    for (IN4JSProject p : projectsWithUndefImplIds) {
        msgs.add("project '" + p.getProjectId() + "' does not define an ImplementationId in its manifest");
    }
    for (Map.Entry<Pair<String, String>, Set<IN4JSProject>> currConflict : conflicts.entrySet()) {
        final String apiId = currConflict.getKey().getKey();
        final String implId = currConflict.getKey().getValue();
        final Set<IN4JSProject> culprits = currConflict.getValue();
        final String culpritsStr = " - " + culprits.stream().map(c -> c.getProjectId()).collect(Collectors.joining("\n - "));
        msgs.add("several projects define an implementation for API project '" + apiId + "' with implementation ID '" + implId + "':\n" + culpritsStr);
    }
    return msgs;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) IN4JSProject(org.eclipse.n4js.projectModel.IN4JSProject) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(org.eclipse.xtext.xbase.lib.Pair)

Aggregations

IN4JSProject (org.eclipse.n4js.projectModel.IN4JSProject)93 URI (org.eclipse.emf.common.util.URI)32 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)13 List (java.util.List)11 Inject (com.google.inject.Inject)9 Map (java.util.Map)9 IProject (org.eclipse.core.resources.IProject)9 IN4JSCore (org.eclipse.n4js.projectModel.IN4JSCore)9 Optional (com.google.common.base.Optional)7 File (java.io.File)7 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 Set (java.util.Set)6 Collectors (java.util.stream.Collectors)6 IOException (java.io.IOException)5 IN4JSArchive (org.eclipse.n4js.projectModel.IN4JSArchive)5 IN4JSSourceContainer (org.eclipse.n4js.projectModel.IN4JSSourceContainer)5 FluentIterable.from (com.google.common.collect.FluentIterable.from)4