Search in sources :

Example 1 with PlainAccessOfNamespacedImportDescription

use of org.eclipse.n4js.scoping.imports.PlainAccessOfNamespacedImportDescription in project n4js by eclipse.

the class FQNImporter method apply.

/**
 * Modify the document and start linked editing if necessary.
 *
 * Imports will be added if necessary.
 *
 * @see #applyWithImport(QualifiedName, String, IDocument, ConfigurableCompletionProposal)
 */
@Override
public void apply(IDocument document, ConfigurableCompletionProposal proposal) throws BadLocationException {
    final String syntacticReplacementString = proposal.getReplacementString();
    // does it even happen? check logs if first and/or second check passes
    {
        String actualSyntacticReplacementString = getActualReplacementString(proposal);
        // there is an import statement - apply computed replacementString
        if (!syntacticReplacementString.equals(actualSyntacticReplacementString)) {
            QualifiedName shortQualifiedName = applyValueConverter(actualSyntacticReplacementString);
            if (shortQualifiedName.getSegmentCount() == 1) {
                simpleApply(document, actualSyntacticReplacementString, proposal);
                return;
            }
        }
    }
    final QualifiedName qualifiedName = (QualifiedName) proposal.getAdditionalData(KEY_QUALIFIED_NAME);
    final QualifiedName originalQualifiedName = (QualifiedName) proposal.getAdditionalData(KEY_ORIGINAL_QUALIFIED_NAME);
    if (qualifiedName == null) {
        super.apply(document, proposal);
        return;
    }
    // we could create an import statement if there is no conflict
    if (qualifiedName.getSegmentCount() == 1) {
        // type name is a simple name - no need to hassle with imports
        simpleApply(document, syntacticReplacementString, proposal);
        return;
    }
    // Globally available elements should not generate imports
    if (qualifiedName.getSegmentCount() == 2 && N4TSQualifiedNameProvider.GLOBAL_NAMESPACE_SEGMENT.equals(qualifiedName.getFirstSegment())) {
        // type name is a simple name from global Namespace - no need to hassle with imports
        simpleApply(document, syntacticReplacementString, proposal);
        return;
    }
    String alias = null;
    String shortQName = lastSegmentOrDefaultHost(originalQualifiedName);
    IEObjectDescription descriptionFullQN = scope.getSingleElement(QualifiedName.create(shortQName));
    // element is PlainAccessOfAliasedImportDescription imported via namespace
    if (descriptionFullQN instanceof PlainAccessOfNamespacedImportDescription) {
        simpleApply(document, ((PlainAccessOfNamespacedImportDescription) descriptionFullQN).getNamespacedName(), proposal);
        return;
    }
    if (descriptionFullQN != null) {
        if (descriptionFullQN.getEObjectOrProxy() instanceof ModuleNamespaceVirtualType) {
            // accessing default export via already imported namespace
            simpleApply(document, qualifiedName.toString(), proposal);
            return;
        }
        // the simple name is already reachable - another import is present
        // try to use an alias
        IEObjectDescription description = scope.getSingleElement(originalQualifiedName);
        IEObjectDescription existingAliased = findApplicableDescription(description.getEObjectOrProxy(), qualifiedName, false);
        // there is already an alias, but the FQN version was picked - insert FQN
        if (existingAliased != null) {
            simpleApply(document, syntacticReplacementString, proposal);
            return;
        }
        // trying to detect namespace access without PlainAccessOfNamespacedImportDescription being accessible
        // no alias used, yet - add an alias and insert that one
        alias = "Alias" + shortQName;
    }
    applyWithImport(qualifiedName, alias, document, proposal);
}
Also used : PlainAccessOfNamespacedImportDescription(org.eclipse.n4js.scoping.imports.PlainAccessOfNamespacedImportDescription) QualifiedName(org.eclipse.xtext.naming.QualifiedName) ModuleNamespaceVirtualType(org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

PlainAccessOfNamespacedImportDescription (org.eclipse.n4js.scoping.imports.PlainAccessOfNamespacedImportDescription)1 ModuleNamespaceVirtualType (org.eclipse.n4js.ts.types.ModuleNamespaceVirtualType)1 QualifiedName (org.eclipse.xtext.naming.QualifiedName)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1