Search in sources :

Example 11 with N4JSSourceFolderSnapshot

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

the class TestDiscoveryHelper method isTestable.

/**
 * Checks if the resource at the given location can be executed as a test. The location may point to an N4JS project
 * or a folder or file within an N4JS project.
 * <p>
 * This method is intended as a very first check to decide if the user should be given the option to launch a test
 * or not; for example, to enable or disable a "Run as test" context menu item). This method does not check all
 * details because many checks are better handled at later stages when meaningful error messages can be provided to
 * the user.
 */
public boolean isTestable(N4JSWorkspaceConfigSnapshot ws, final URI location) {
    if (null == location) {
        return false;
    }
    if (isProject(ws, location)) {
        // location points an N4JS project
        // --> must contain at least 1 source container of type "test"
        // (do *not* check if the folder contains test classes (for performance reasons and
        // to show better error messages; same behavior as in JUnit support in Eclipse))
        final N4JSProjectConfigSnapshot p = ws.findProjectByPath(location);
        return p.getSourceFolders().stream().anyMatch(N4JSSourceFolderSnapshot::isTest);
    } else {
        // then extract the fragment and check the location for the module.
        if (location.hasFragment()) {
            return isTestable(ws, location.trimFragment());
        }
        // (2) location must lie in a source container of type "test"
        final N4JSSourceFolderSnapshot c = ws.findSourceFolderContaining(location);
        if (c == null || !c.isTest())
            return false;
        // (3) if the location points to an n4js-file, it must contain at least one test class
        final ResourceSet resourceSet = workspaceAccess.createResourceSet();
        final IResourceDescriptions index = workspaceAccess.getXtextIndex(resourceSet).get();
        final IResourceDescription rdesc = index.getResourceDescription(location);
        if (rdesc != null) {
            return stream(rdesc.getExportedObjectsByType(T_CLASS)).anyMatch(desc -> hasTestMethods(resourceSet, desc));
        } else {
            // to show better error messages; same behavior as in JUnit support in Eclipse))
            return true;
        }
    }
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) IResourceDescriptions(org.eclipse.xtext.resource.IResourceDescriptions) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 12 with N4JSSourceFolderSnapshot

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

the class N4JSLanguageHelper method isDirectoryWithPackageJson.

private boolean isDirectoryWithPackageJson(IResourceDescriptions index, TModule targetModule, QualifiedName targetQN) {
    // NOTE: the following approach would be a more elegant implementation of this method, but would require a
    // different computation of FQNs for package.json files in source folders (in N4JSQualifiedNameProvider):
    // @formatter:off
    // Iterable<IEObjectDescription> matchingPackageJsonDesc = index.getExportedObjects(
    // JSONPackage.Literals.JSON_DOCUMENT,
    // targetQN.append(N4JSQualifiedNameProvider.PACKAGE_JSON_SEGMENT), false);
    // if (matchingPackageJsonDesc.iterator().hasNext()) {
    // return true;
    // }
    // @formatter:on
    N4JSProjectConfigSnapshot targetProject = replaceDefinitionProjectByDefinedProject(targetModule, workspaceAccess.findProjectContaining(targetModule), true);
    if (targetProject == null) {
        return false;
    }
    int segCount = targetQN.getSegments().size();
    String[] segments = new String[segCount + 1];
    for (int i = 0; i < segCount; i++) {
        segments[i] = targetQN.getSegments().get(i);
    }
    segments[segCount] = N4JSGlobals.PACKAGE_JSON;
    for (N4JSSourceFolderSnapshot srcFolder : targetProject.getSourceFolders()) {
        if (srcFolder instanceof N4JSSourceFolderSnapshotForPackageJson) {
            continue;
        }
        FileURI packageJsonURI = srcFolder.getPathAsFileURI().appendSegments(segments);
        if (index.getResourceDescription(packageJsonURI.toURI()) != null) {
            return true;
        }
    }
    return false;
}
Also used : FileURI(org.eclipse.n4js.workspace.locations.FileURI) N4JSSourceFolderSnapshotForPackageJson(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshotForPackageJson) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)

Example 13 with N4JSSourceFolderSnapshot

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

the class StaticPolyfillHelper method findStaticPolyfiller.

/**
 * Find the corresponding static-polyfill to this {@code @@PolyfillAware} resource in the same project. returns null
 * if not found or this resource has no {@code @@PolyfillAware} annotation.
 */
public SafeURI<?> findStaticPolyfiller(Resource resource) {
    // ensure right resource
    if (resource instanceof N4JSResource) {
        final N4JSResource res = (N4JSResource) resource;
        if (!isContainedInStaticPolyfillAware(res.getScript()))
            return null;
        final QualifiedName qnFilled = qualifiedNameConverter.toQualifiedName(res.getModule().getQualifiedName());
        final N4JSProjectConfigSnapshot project = projectResolver.resolveProject(res, res.getURI());
        final QualifiedName fqn = qnFilled;
        // see Req.155#4: "Both extensions are equal."
        final Optional<String> fileExtension = Optional.of(URIUtils.fileExtension(res.getURI()));
        final N4JSSourceFolderSnapshot filledSrcContainer = workspaceAccess.findSourceFolderContaining(res, res.getURI());
        for (N4JSSourceFolderSnapshot srcConti : project.getSourceFolders()) {
            if (!Objects.equals(filledSrcContainer, srcConti)) {
                final SafeURI<?> uri = findArtifactHelper.findArtifact(srcConti, fqn, fileExtension);
                if (uri != null) {
                    return uri;
                }
            }
        }
    }
    return null;
}
Also used : QualifiedName(org.eclipse.xtext.naming.QualifiedName) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) N4JSResource(org.eclipse.n4js.resource.N4JSResource) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)

Aggregations

N4JSSourceFolderSnapshot (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot)13 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)8 URI (org.eclipse.emf.common.util.URI)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 N4JSResource (org.eclipse.n4js.resource.N4JSResource)4 FileURI (org.eclipse.n4js.workspace.locations.FileURI)4 Script (org.eclipse.n4js.n4JS.Script)3 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)3 ProjectDescription (org.eclipse.n4js.packagejson.projectDescription.ProjectDescription)2 Type (org.eclipse.n4js.ts.types.Type)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 TModule (org.eclipse.n4js.ts.types.TModule)1 TVariable (org.eclipse.n4js.ts.types.TVariable)1 N4JSSourceFolderSnapshotForPackageJson (org.eclipse.n4js.workspace.N4JSSourceFolderSnapshotForPackageJson)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 IResourceDescriptions (org.eclipse.xtext.resource.IResourceDescriptions)1