Search in sources :

Example 6 with N4JSWorkspaceConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot 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 filter
 *            by default an instance of {@link N4JSCandidateFilter} will be provided here.
 */
public void lookupCrossReference(EObject model, EReference reference, ContentAssistContext context, IIdeContentProposalAcceptor acceptor, Predicate<IEObjectDescription> filter) {
    if (model == null) {
        return;
    }
    Resource resource = model.eResource();
    if (!(resource instanceof N4JSResource)) {
        return;
    }
    N4JSResource resourceCasted = (N4JSResource) resource;
    N4JSWorkspaceConfigSnapshot wc = workspaceAccess.getWorkspaceConfig(resourceCasted);
    ReferenceDescriptor referenceDesc = new ReferenceDescriptor(context.getPrefix(), model, reference, context.getCurrentNode());
    Predicate<String> conflictChecker = (proposalToCheck) -> conflictHelper.existsConflict(proposalToCheck, context);
    referenceResolutionFinder.findResolutions(wc, referenceDesc, false, false, conflictChecker, filter, new ResolutionToContentProposalAcceptor(resourceCasted, context, acceptor));
}
Also used : N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) N4JSCandidateFilter(org.eclipse.n4js.ide.editor.contentassist.N4JSIdeContentProposalProvider.N4JSCandidateFilter) Inject(com.google.inject.Inject) ReferenceDescriptor(org.eclipse.n4js.ide.imports.ReferenceDescriptor) ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) EObject(org.eclipse.emf.ecore.EObject) N4JSPackage(org.eclipse.n4js.n4JS.N4JSPackage) IdeContentProposalPriorities(org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalPriorities) IResolutionAcceptor(org.eclipse.n4js.ide.imports.ReferenceResolutionFinder.IResolutionAcceptor) IIdeContentProposalAcceptor(org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor) N4JSResource(org.eclipse.n4js.resource.N4JSResource) EClass(org.eclipse.emf.ecore.EClass) WorkspaceAccess(org.eclipse.n4js.workspace.WorkspaceAccess) Predicate(com.google.common.base.Predicate) TypesPackage(org.eclipse.n4js.ts.types.TypesPackage) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) Resource(org.eclipse.emf.ecore.resource.Resource) ReferenceResolution(org.eclipse.n4js.ide.imports.ReferenceResolution) ImportHelper(org.eclipse.n4js.ide.imports.ImportHelper) EReference(org.eclipse.emf.ecore.EReference) ReferenceResolutionFinder(org.eclipse.n4js.ide.imports.ReferenceResolutionFinder) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) IProposalConflictHelper(org.eclipse.xtext.ide.editor.contentassist.IProposalConflictHelper) N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) N4JSResource(org.eclipse.n4js.resource.N4JSResource) Resource(org.eclipse.emf.ecore.resource.Resource) N4JSResource(org.eclipse.n4js.resource.N4JSResource) ReferenceDescriptor(org.eclipse.n4js.ide.imports.ReferenceDescriptor)

Example 7 with N4JSWorkspaceConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot in project n4js by eclipse.

the class ImportHelper method findResolutionsForUnresolvedReference.

/**
 * Iff the given AST node contains an unresolved reference, this method returns all possible resolutions of this
 * reference. Returns an empty list if the given AST node does not contain an unresolved reference or if no
 * resolutions are found.
 *
 * @param astNode
 *            the AST node possibly containing an unresolved reference.
 * @return valid resolutions for the unresolved reference. May be empty but never <code>null</code>.
 */
public List<ReferenceResolution> findResolutionsForUnresolvedReference(EObject astNode, CancelIndicator cancelIndicator) {
    triggerProxyResolution(astNode, cancelIndicator);
    N4JSWorkspaceConfigSnapshot wc = workspaceAccess.getWorkspaceConfig(astNode);
    ReferenceDescriptor reference = getUnresolvedReferenceForASTNode(astNode);
    if (reference == null) {
        return Collections.emptyList();
    }
    operationCanceledManager.checkCanceled(cancelIndicator);
    ResolutionAcceptor acceptor = new ResolutionAcceptor(-1, cancelIndicator);
    referenceResolutionFinder.findResolutions(wc, reference, true, true, Predicates.alwaysFalse(), Predicates.alwaysTrue(), acceptor);
    return acceptor.getResolutions();
}
Also used : N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) IResolutionAcceptor(org.eclipse.n4js.ide.imports.ReferenceResolutionFinder.IResolutionAcceptor)

Example 8 with N4JSWorkspaceConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot in project n4js by eclipse.

the class N4JSResourceDescriptionManager method isAffected.

/**
 * {@inheritDoc}
 *
 * This marks {@code n4js} as affected if the manifest of the project changes. In turn, they will be revalidated and
 * taken into consideration for the code generation step.
 */
@Override
public boolean isAffected(Collection<IResourceDescription.Delta> deltas, IResourceDescription candidate, IResourceDescriptions context, WorkspaceConfigSnapshot workspaceConfig) {
    if (workspaceConfig == null) {
        // will throw exception
        isAffected(deltas, candidate, context);
    }
    N4JSWorkspaceConfigSnapshot workspaceConfigCasted = (N4JSWorkspaceConfigSnapshot) workspaceConfig;
    URI candidateURI = candidate.getURI();
    // Thus, they will never be affected by any change.
    if (N4JSLanguageUtils.isOpaqueModule(workspaceConfigCasted, candidateURI)) {
        return false;
    }
    boolean result = basicIsAffected(deltas, candidate, workspaceConfigCasted);
    if (!result) {
        for (IResourceDescription.Delta delta : deltas) {
            URI uri = delta.getUri();
            // if uri looks like a N4JS project description file (i.e. package.json)
            if (N4JSGlobals.PACKAGE_JSON.equalsIgnoreCase(uri.lastSegment())) {
                URI prefixURI = uri.trimSegments(1).appendSegment("");
                if (candidateURI.replacePrefix(prefixURI, prefixURI) != null) {
                    return true;
                }
            }
        }
    }
    return result;
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) Delta(org.eclipse.xtext.resource.IResourceDescription.Delta) URI(org.eclipse.emf.common.util.URI)

Example 9 with N4JSWorkspaceConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot 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 implementation 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, N4JSPackageName implementationID, final boolean includePolyfills) {
    Resource resource = module.eResource();
    N4JSWorkspaceConfigSnapshot wc = workspaceAccess.getWorkspaceConfig(resource);
    N4JSProjectConfigSnapshot project = workspaceAccess.findProjectContaining(resource);
    if (project == null) {
        return null;
    }
    N4JSProjectConfigSnapshot implProject = null;
    N4JSProjectConfigSnapshot apiProject = null;
    TModule apiModule = null;
    TModule apiImplModule = null;
    if (project.getImplementationId() == null) {
        // this is NOT an implementation project, so assume we have the api
        // need to load the correct implementation. Since there might be multiple implementors
        ApiImplMapping mapping = ApiImplMapping.of(wc);
        implProject = mapping.getImpl(project.getN4JSPackageName(), 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 = workspaceAccess.getXtextIndex(module).orNull();
            IResourceDescription resourceDescription = xtextIndex != null ? xtextIndex.getResourceDescription(impUri) : null;
            if (resourceDescription != null) {
                apiImplModule = workspaceAccess.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(new N4JSPackageName(project.getImplementationId()))) {
            implProject = project;
            apiImplModule = module;
            List<N4JSProjectConfigSnapshot> apiProjects = new ArrayList<>();
            for (ProjectReference apiProjectRef : implProject.getProjectDescription().getImplementedProjects()) {
                N4JSProjectConfigSnapshot currApiProject = wc.findProjectByID(apiProjectRef.getPackageName());
                if (currApiProject != null) {
                    apiProjects.add(currApiProject);
                }
            }
            labelA: for (N4JSProjectConfigSnapshot ap : apiProjects) {
                URI apiURI = artifactHelper.findArtifact(ap, apiImplModule.getQualifiedName(), Optional.of(N4JSGlobals.N4JSD_FILE_EXTENSION));
                if (apiURI != null) {
                    IResourceDescriptions xtextIndex = workspaceAccess.getXtextIndex(apiImplModule).orNull();
                    IResourceDescription resourceDescription = xtextIndex.getResourceDescription(apiURI);
                    if (resourceDescription != null) {
                        apiModule = workspaceAccess.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;
    }
}
Also used : ProjectReference(org.eclipse.n4js.packagejson.projectDescription.ProjectReference) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) Resource(org.eclipse.emf.ecore.resource.Resource) ArrayList(java.util.ArrayList) TModule(org.eclipse.n4js.ts.types.TModule) URI(org.eclipse.emf.common.util.URI)

Example 10 with N4JSWorkspaceConfigSnapshot

use of org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot 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 = workspaceAccess.createResourceSet();
    final N4JSWorkspaceConfigSnapshot wc = workspaceAccess.getWorkspaceConfig(resourceSet);
    final IResourceDescriptions index = workspaceAccess.getXtextIndex(resourceSet).orNull();
    if (index == null) {
        addErrorMessagesHere.add("failed to create a new resource set with properly configured workspace configuration and index");
        return null;
    }
    final ApiImplMapping mapping = ApiImplMapping.of(wc);
    if (mapping.hasErrors()) {
        if (addErrorMessagesHere != null)
            addErrorMessagesHere.addAll(mapping.getErrorMessages());
        return null;
    }
    final List<N4JSPackageName> allImplIds = mapping.getAllImplIds();
    final int implCount = allImplIds.size();
    final ProjectComparison root = new ProjectComparison(allImplIds.toArray(new N4JSPackageName[implCount]));
    for (N4JSPackageName currApiId : mapping.getApiIds()) {
        final N4JSProjectConfigSnapshot currApi = mapping.getApi(currApiId);
        final N4JSProjectConfigSnapshot[] currImpls = new N4JSProjectConfigSnapshot[implCount];
        for (int idx = 0; idx < implCount; idx++) {
            final N4JSPackageName 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 : N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Aggregations

N4JSWorkspaceConfigSnapshot (org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot)11 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)7 URI (org.eclipse.emf.common.util.URI)4 ArrayList (java.util.ArrayList)3 IResolutionAcceptor (org.eclipse.n4js.ide.imports.ReferenceResolutionFinder.IResolutionAcceptor)3 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 N4JSResource (org.eclipse.n4js.resource.N4JSResource)2 N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)2 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)2 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)2 Predicate (com.google.common.base.Predicate)1 Inject (com.google.inject.Inject)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 EClass (org.eclipse.emf.ecore.EClass)1