Search in sources :

Example 1 with ModuleSpecifierForm

use of org.eclipse.n4js.n4JS.ModuleSpecifierForm in project n4js by eclipse.

the class ImportDeclarationImpl method setModuleSpecifierForm.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setModuleSpecifierForm(ModuleSpecifierForm newModuleSpecifierForm) {
    ModuleSpecifierForm oldModuleSpecifierForm = moduleSpecifierForm;
    moduleSpecifierForm = newModuleSpecifierForm == null ? MODULE_SPECIFIER_FORM_EDEFAULT : newModuleSpecifierForm;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, N4JSPackage.IMPORT_DECLARATION__MODULE_SPECIFIER_FORM, oldModuleSpecifierForm, moduleSpecifierForm));
}
Also used : ModuleSpecifierForm(org.eclipse.n4js.n4JS.ModuleSpecifierForm) ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl)

Example 2 with ModuleSpecifierForm

use of org.eclipse.n4js.n4JS.ModuleSpecifierForm 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 3 with ModuleSpecifierForm

use of org.eclipse.n4js.n4JS.ModuleSpecifierForm in project n4js by eclipse.

the class ProjectImportEnablingScope method getSingleElement.

@Override
public IEObjectDescription getSingleElement(QualifiedName name) {
    final List<IEObjectDescription> result = Lists.newArrayList(getElements(name));
    int size = result.size();
    // handle combination of .js / .cjs / .mjs files with same base name
    if (size > 1) {
        removeSuperfluousPlainJsFiles(result);
        size = result.size();
    }
    if (size == 1) {
        // main case
        return result.get(0);
    }
    // use sorted entries and linked map for determinism in error message
    Collections.sort(result, Comparator.comparing(IEObjectDescription::getEObjectURI, Comparator.comparing(URI::toString)));
    final Map<IEObjectDescription, N4JSProjectConfigSnapshot> descriptionsToProject = new LinkedHashMap<>();
    for (IEObjectDescription objDescr : result) {
        URI uri = objDescr.getEObjectURI();
        N4JSProjectConfigSnapshot n4jsdProject = workspaceConfigSnapshot.findProjectContaining(uri);
        descriptionsToProject.put(objDescr, n4jsdProject);
    }
    IEObjectDescription overwritingModule = handleCollisions(result, descriptionsToProject);
    if (overwritingModule != null) {
        return overwritingModule;
    }
    // if no import declaration was given, we skip the advanced error reporting
    if (!importDeclaration.isPresent()) {
        return null;
    }
    // handle special defaults
    IEObjectDescription defaultModule = handleDefaults(descriptionsToProject);
    if (defaultModule != null) {
        return defaultModule;
    }
    // handle error cases to help user fix the issue
    StringBuilder sbErrrorMessage = new StringBuilder("Cannot resolve ");
    ModuleSpecifierForm importType = computeImportType(name, this.contextProject);
    switch(importType) {
        case PROJECT:
            sbErrrorMessage.append("project import");
            break;
        case COMPLETE:
            sbErrrorMessage.append("complete module specifier (with project name as first segment)");
            break;
        case PLAIN:
            sbErrrorMessage.append("plain module specifier (without project name as first segment)");
            break;
        case PROJECT_NO_MAIN:
            sbErrrorMessage.append("project import: target project does not define a main module");
            break;
        default:
            sbErrrorMessage.append("module specifier");
            break;
    }
    if (!importType.equals(ModuleSpecifierForm.PROJECT_NO_MAIN)) {
        sbErrrorMessage.append(": ");
        if (size == 0) {
            sbErrrorMessage.append("no matching module found");
        } else {
            sbErrrorMessage.append("multiple matching modules found: ");
            String matchingModules = "";
            for (IEObjectDescription descr : descriptionsToProject.keySet()) {
                if (!matchingModules.isEmpty()) {
                    matchingModules += ", ";
                }
                if (descr.getEObjectURI() != null) {
                    URI uri = descr.getEObjectURI().trimFragment();
                    URI relUri = uri.deresolve(workspaceConfigSnapshot.getPath());
                    Path relPath = Path.of(relUri.toFileString());
                    relPath = relPath.subpath(1, relPath.getNameCount());
                    matchingModules += relPath.toString();
                } else {
                    matchingModules += descriptionsToProject.get(descr).getPackageName() + "/" + descr.getQualifiedName().toString();
                }
            }
            sbErrrorMessage.append(matchingModules);
        }
    }
    sbErrrorMessage.append('.');
    final EObject originalProxy = (EObject) this.importDeclaration.get().eGet(N4JSPackage.eINSTANCE.getImportDeclaration_Module(), false);
    return new IssueCodeBasedEObjectDescription(EObjectDescription.create("impDecl", originalProxy), sbErrrorMessage.toString(), IssueCodes.IMP_UNRESOLVED);
}
Also used : Path(java.nio.file.Path) URI(org.eclipse.emf.common.util.URI) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) LinkedHashMap(java.util.LinkedHashMap) ModuleSpecifierForm(org.eclipse.n4js.n4JS.ModuleSpecifierForm) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) EObject(org.eclipse.emf.ecore.EObject)

Aggregations

ModuleSpecifierForm (org.eclipse.n4js.n4JS.ModuleSpecifierForm)3 Path (java.nio.file.Path)1 LinkedHashMap (java.util.LinkedHashMap)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)1 N4JSProjectConfigSnapshot (org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot)1 N4JSPackageName (org.eclipse.n4js.workspace.utils.N4JSPackageName)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1