Search in sources :

Example 1 with AddImportCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method addSimilarTypeProposals.

private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
    SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);
    // try to resolve type in context -> highest severity
    String resolvedTypeName = null;
    ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
    if (binding != null) {
        ITypeBinding simpleBinding = binding;
        if (simpleBinding.isArray()) {
            simpleBinding = simpleBinding.getElementType();
        }
        simpleBinding = simpleBinding.getTypeDeclaration();
        if (!simpleBinding.isRecovered()) {
            resolvedTypeName = simpleBinding.getQualifiedName();
            CUCorrectionProposal proposal = createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
            proposals.add(proposal);
            if (proposal instanceof AddImportCorrectionProposal)
                proposal.setRelevance(relevance + elements.length + 2);
            if (binding.isParameterizedType() && (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType) && !(node.getParent().getParent() instanceof Type)) {
                proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
            }
        }
    } else {
        ASTNode normalizedNode = ASTNodes.getNormalizedNode(node);
        if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
            ITypeBinding normBinding = ASTResolving.guessBindingForTypeReference(normalizedNode);
            if (normBinding != null && !normBinding.isRecovered()) {
                proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
            }
        }
    }
    // add all similar elements
    for (int i = 0; i < elements.length; i++) {
        SimilarElement elem = elements[i];
        if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
            String fullName = elem.getName();
            if (!fullName.equals(resolvedTypeName)) {
                proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
            }
        }
    }
}
Also used : AddImportCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal) SimpleType(org.eclipse.jdt.core.dom.SimpleType) IType(org.eclipse.jdt.core.IType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) CUCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Example 2 with AddImportCorrectionProposal

use of org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal in project che by eclipse.

the class UnresolvedElementsSubProcessor method createTypeRefChangeProposal.

private static CUCorrectionProposal createTypeRefChangeProposal(ICompilationUnit cu, String fullName, Name node, int relevance, int maxProposals) {
    ImportRewrite importRewrite = null;
    String simpleName = fullName;
    String packName = Signature.getQualifier(fullName);
    if (packName.length() > 0) {
        // no imports for primitive types, type variables
        importRewrite = StubUtility.createImportRewrite((CompilationUnit) node.getRoot(), true);
        // can be null in package-info.java
        BodyDeclaration scope = ASTResolving.findParentBodyDeclaration(node);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(scope != null ? scope : node, importRewrite);
        simpleName = importRewrite.addImport(fullName, context);
    }
    if (!isLikelyTypeName(simpleName)) {
        relevance -= 2;
    }
    ASTRewriteCorrectionProposal proposal;
    if (importRewrite != null && node.isSimpleName() && simpleName.equals(((SimpleName) node).getIdentifier())) {
        // import only
        // import only
        String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description, arg);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
        int boost = QualifiedTypeNameHistory.getBoost(fullName, 0, maxProposals);
        proposal = new AddImportCorrectionProposal(label, cu, relevance + 100 + boost, image, packName, simpleName, (SimpleName) node);
        proposal.setCommandId(ADD_IMPORT_ID);
    } else {
        String label;
        if (packName.length() == 0) {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_nopack_description, BasicElementLabels.getJavaElementName(simpleName));
        } else {
            String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_description, arg);
        }
        ASTRewrite rewrite = ASTRewrite.create(node.getAST());
        rewrite.replace(node, rewrite.createStringPlaceholder(simpleName, ASTNode.SIMPLE_TYPE), null);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);
    }
    if (importRewrite != null) {
        proposal.setImportRewrite(importRewrite);
    }
    return proposal;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AddImportCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Aggregations

AddImportCorrectionProposal (org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IType (org.eclipse.jdt.core.IType)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 ArrayType (org.eclipse.jdt.core.dom.ArrayType)1 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 SimpleType (org.eclipse.jdt.core.dom.SimpleType)1 Type (org.eclipse.jdt.core.dom.Type)1 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)1 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)1 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)1 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)1 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)1 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)1 Image (org.eclipse.swt.graphics.Image)1