Search in sources :

Example 1 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class ImportHelper method createImportDescriptorFromAST.

/**
 * Creates an {@link ImportDescriptor} for a given existing import in the AST.
 *
 * @param importDecl
 *            the import declaration. May not be <code>null</code>.
 * @param importSpec
 *            the import specification in case of a named or namespace import; <code>null</code> denotes a bare
 *            import.
 * @param originalIndex
 *            the original index to set in the newly created {@link ImportDescriptor}. This should usually reflect
 *            the original order of imports in the AST.
 * @return a newly created import descriptor.
 */
public ImportDescriptor createImportDescriptorFromAST(ImportDeclaration importDecl, ImportSpecifier importSpec, int originalIndex) {
    Objects.requireNonNull(importDecl);
    if (importSpec != null && !importSpec.isFlaggedUsedInCode()) {
        throw new IllegalArgumentException("must not create an ImportDescriptor for unused imports");
    }
    if ((importSpec == null && ImportSpecifiersUtil.isBrokenImport(importDecl)) || (importSpec != null && ImportSpecifiersUtil.isBrokenImport(importSpec))) {
        throw new IllegalArgumentException("must not create an ImportDescriptor for broken imports");
    }
    AbstractModule module = importDecl.getModule();
    Optional<N4JSPackageName> targetProjectName = module instanceof TModule ? Optional.of(new N4JSPackageName(((TModule) module).getPackageName())) : // module declaration), so they do not have a targetProjectName:
    Optional.absent();
    QualifiedName targetModule = qualifiedNameConverter.toQualifiedName(module.getQualifiedName());
    String moduleSpecifier = importDecl.getModuleSpecifierAsText();
    if (importDecl.isBare()) {
        return ImportDescriptor.createBareImport(moduleSpecifier, targetProjectName, targetModule, originalIndex);
    } else {
        if (importSpec instanceof NamedImportSpecifier) {
            NamedImportSpecifier importSpecCasted = (NamedImportSpecifier) importSpec;
            if (importSpecCasted.isDefaultImport()) {
                String localName = importSpecCasted.getAlias();
                return ImportDescriptor.createDefaultImport(localName, moduleSpecifier, targetProjectName, targetModule, originalIndex);
            } else {
                String elementName = importSpecCasted.getImportedElementAsText();
                String alias = importSpecCasted.getAlias();
                return ImportDescriptor.createNamedImport(elementName, alias, moduleSpecifier, targetProjectName, targetModule, originalIndex);
            }
        } else if (importSpec instanceof NamespaceImportSpecifier) {
            String localNamespaceName = ((NamespaceImportSpecifier) importSpec).getAlias();
            boolean isDynamic = ((NamespaceImportSpecifier) importSpec).isDeclaredDynamic();
            return ImportDescriptor.createNamespaceImport(localNamespaceName, isDynamic, moduleSpecifier, targetProjectName, targetModule, originalIndex);
        } else if (importSpec != null) {
            throw new IllegalArgumentException("unknown subclass of ImportSpecifier: " + importSpec.getClass().getSimpleName());
        } else {
            throw new IllegalArgumentException("importSpec may be null only if importDecl is a bare import");
        }
    }
}
Also used : NamedImportSpecifier(org.eclipse.n4js.n4JS.NamedImportSpecifier) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) NamespaceImportSpecifier(org.eclipse.n4js.n4JS.NamespaceImportSpecifier) QualifiedName(org.eclipse.xtext.naming.QualifiedName) TModule(org.eclipse.n4js.ts.types.TModule) AbstractModule(org.eclipse.n4js.ts.types.AbstractModule)

Example 2 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class ReferenceResolutionFinder method getImportToBeAdded.

private ImportDescriptor getImportToBeAdded(ReferenceResolutionCandidate rrc) {
    NameAndAlias requestedImport = rrc.addedImportNameAndAlias;
    if (requestedImport == null) {
        return null;
    }
    QualifiedName qualifiedName = requestedImport.name;
    String optionalAlias = requestedImport.alias;
    N4JSPackageName projectName = rrc.candidateProjectOrNull != null ? rrc.candidateProjectOrNull.getN4JSPackageName() : null;
    if (projectName == null) {
        return null;
    }
    QualifiedName moduleName = qualifiedName.skipLast(1);
    String moduleSpecifier = qualifiedNameConverter.toString(moduleName);
    ImportDescriptor importDesc;
    if (N4JSLanguageUtils.isDefaultExport(qualifiedName)) {
        String localName = optionalAlias != null ? optionalAlias : N4JSLanguageUtils.lastSegmentOrDefaultHost(qualifiedName);
        importDesc = ImportDescriptor.createDefaultImport(localName, moduleSpecifier, Optional.of(projectName), moduleName, Integer.MAX_VALUE);
    } else {
        importDesc = ImportDescriptor.createNamedImport(qualifiedName.getLastSegment(), optionalAlias, moduleSpecifier, Optional.of(projectName), moduleName, Integer.MAX_VALUE);
    }
    return importDesc;
}
Also used : N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName) QualifiedName(org.eclipse.xtext.naming.QualifiedName)

Example 3 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class PackageJsonModificationUtils method setVersionOfDependenciesInPackageJsonString.

/**
 * Changes the version of all dependencies and devDependencies to packages with a name contained in
 * {@code projectNames} to the given version. Names given in the set that are not among the dependencies in the
 * package.json file will be ignored (i.e. this method won't add any dependencies); dependencies in the package.json
 * file that are not denoted by the names in the set will remain unchanged.
 *
 * @return the <code>package.json</code> string after all replacements were done, or {@link Optional#absent()
 *         absent} in case the JSON string was successfully parsed, but this operation did not lead to any changes.
 * @throws IllegalArgumentException
 *             in case of parse errors.
 */
public static Optional<String> setVersionOfDependenciesInPackageJsonString(String packageJson, Set<N4JSPackageName> projectNames, String versionConstraintToSet) {
    List<ElementWithRegion> deps = findDependenciesWithRegion(packageJson);
    if (deps.isEmpty()) {
        return Optional.absent();
    }
    StringBuilder result = null;
    Collections.sort(deps, (dep1, dep2) -> Integer.compare(dep2.offset, dep1.offset));
    for (ElementWithRegion dep : deps) {
        if (projectNames.contains(new N4JSPackageName(dep.elementName))) {
            if (result == null) {
                result = new StringBuilder(packageJson);
            }
            result.replace(dep.offset + 1, dep.offset + dep.length - 1, versionConstraintToSet);
        }
    }
    return result != null ? Optional.of(result.toString()) : Optional.absent();
}
Also used : N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName)

Example 4 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class ProjectImportEnablingScope method getElements.

@Override
public Iterable<IEObjectDescription> getElements(QualifiedName name) {
    ModuleSpecifierForm moduleSpecifierForm = computeImportType(name, this.contextProject);
    storeModuleSpecifierFormInAST(moduleSpecifierForm);
    switch(moduleSpecifierForm) {
        case PROJECT:
            {
                final N4JSPackageName firstSegment = new N4JSPackageName(name.getFirstSegment());
                return findModulesInProject(Optional.absent(), firstSegment);
            }
        case COMPLETE:
            {
                final N4JSPackageName firstSegment = new N4JSPackageName(name.getFirstSegment());
                return findModulesInProject(Optional.of(name.skipFirst(1)), firstSegment);
            }
        case PLAIN:
            {
                return parent.getElements(name);
            }
        default:
            return Collections.emptyList();
    }
}
Also used : ModuleSpecifierForm(org.eclipse.n4js.n4JS.ModuleSpecifierForm) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName)

Example 5 with N4JSPackageName

use of org.eclipse.n4js.workspace.utils.N4JSPackageName in project n4js by eclipse.

the class N4jsLibsAccess method installN4jsLibs.

/**
 * Same as {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...)} but will install the
 * n4js-libs specified as (dev-)dependencies in the given package.json file. Will throw an exception if the given
 * package.json file has dependencies that are not n4js-libs packages.
 *
 * @param targetPath
 *            see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
 * @param includeDependencies
 *            see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
 * @param useSymbolicLinks
 *            see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
 * @param deleteOnExit
 *            see {@link #installN4jsLibs(Path, boolean, boolean, boolean, N4JSPackageName...) here}.
 * @param packageJsonFile
 *            the package.json file to scan for [dev]Dependencies.
 * @param includeDevDependencies
 *            whether also the devDependencies of the given package.json file should be installed.
 */
public static Map<N4JSPackageName, Path> installN4jsLibs(Path targetPath, boolean includeDependencies, boolean useSymbolicLinks, boolean deleteOnExit, Path packageJsonFile, boolean includeDevDependencies) throws IOException {
    if (!packageJsonFile.getFileName().toString().equals(N4JSGlobals.PACKAGE_JSON)) {
        throw new IllegalArgumentException("path does not denote a package.json file: " + packageJsonFile);
    }
    Path n4jsLibsLocation = findN4jsLibsLocation();
    Map<N4JSPackageName, Path> toBeInstalled = new HashMap<>();
    List<N4JSPackageName> deps = loadDepenencies(packageJsonFile.getParent(), includeDevDependencies);
    for (N4JSPackageName projectName : deps) {
        Path projectPath = findN4jsLib(n4jsLibsLocation, projectName, false);
        toBeInstalled.put(projectName, projectPath);
    }
    return installN4jsLibs(targetPath, includeDependencies, useSymbolicLinks, deleteOnExit, toBeInstalled);
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) N4JSPackageName(org.eclipse.n4js.workspace.utils.N4JSPackageName)

Aggregations

N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)23 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)9 Path (java.nio.file.Path)8 File (java.io.File)5 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 HashMap (java.util.HashMap)3 Entry (java.util.Map.Entry)3 Set (java.util.Set)3 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)3 Optional (com.google.common.base.Optional)2 FluentIterable (com.google.common.collect.FluentIterable)2 Multimap (com.google.common.collect.Multimap)2 Sets (com.google.common.collect.Sets)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 FileTime (java.nio.file.attribute.FileTime)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2