Search in sources :

Example 1 with LinkedProposalModel

use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getExtractMethodProposal.

private static boolean getExtractMethodProposal(IInvocationContext context, ASTNode coveringNode, boolean problemsAtLocation, Collection<CUCorrectionProposal> proposals) throws CoreException {
    if (!(coveringNode instanceof Expression) && !(coveringNode instanceof Statement) && !(coveringNode instanceof Block)) {
        return false;
    }
    if (coveringNode instanceof Block) {
        List<Statement> statements = ((Block) coveringNode).statements();
        int startIndex = getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1) {
            return false;
        }
        int endIndex = getIndex(context.getSelectionOffset() + context.getSelectionLength(), statements);
        if (endIndex == -1 || endIndex <= startIndex) {
            return false;
        }
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    final ExtractMethodRefactoring extractMethodRefactoring = new ExtractMethodRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    String uniqueMethodName = getUniqueMethodName(coveringNode, "extracted");
    extractMethodRefactoring.setMethodName(uniqueMethodName);
    if (extractMethodRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        String label = CorrectionMessages.QuickAssistProcessor_extractmethod_description;
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractMethodRefactoring.setLinkedProposalModel(linkedProposalModel);
        int relevance = problemsAtLocation ? IProposalRelevance.EXTRACT_METHOD_ERROR : IProposalRelevance.EXTRACT_METHOD;
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractMethodRefactoring, relevance);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    return true;
}
Also used : ExtractMethodRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LinkedProposalModel(org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel) Block(org.eclipse.jdt.core.dom.Block) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)

Example 2 with LinkedProposalModel

use of org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getExtractVariableProposal.

private static boolean getExtractVariableProposal(IInvocationContext context, boolean problemsAtLocation, Collection<CUCorrectionProposal> proposals) throws CoreException {
    ASTNode node = context.getCoveredNode();
    if (!(node instanceof Expression)) {
        if (context.getSelectionLength() != 0) {
            return false;
        }
        node = context.getCoveringNode();
        if (!(node instanceof Expression)) {
            return false;
        }
    }
    final Expression expression = (Expression) node;
    ITypeBinding binding = expression.resolveTypeBinding();
    if (binding == null || Bindings.isVoidType(binding)) {
        return false;
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        extractTempRefactoring.setReplaceAllOccurrences(true);
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractTempRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoring.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_all_description;
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoring, relevance) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
    if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractTempRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoringSelectedOnly.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_description;
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoringSelectedOnly, relevance) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    ExtractConstantRefactoring extractConstRefactoring = new ExtractConstantRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    if (extractConstRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractConstRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractConstRefactoring.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_constant_description;
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_CONSTANT_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_CONSTANT_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_CONSTANT;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractConstRefactoring, relevance) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractConstantRefactoring etr = (ExtractConstantRefactoring) refactoring;
                // expensive
                etr.setConstantName(etr.guessConstantName());
            }
        };
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) LinkedProposalModel(org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ExtractTempRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) ExtractConstantRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring) ExtractTempRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring) ExtractMethodRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 Expression (org.eclipse.jdt.core.dom.Expression)2 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)2 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)2 LinkedProposalModel (org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel)2 ExtractMethodRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)2 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Block (org.eclipse.jdt.core.dom.Block)1 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)1 Statement (org.eclipse.jdt.core.dom.Statement)1 TryStatement (org.eclipse.jdt.core.dom.TryStatement)1 ExtractConstantRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring)1 ExtractTempRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring)1 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)1