Search in sources :

Example 1 with ExtractTempRefactoring

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring in project eclipse.jdt.ls by eclipse.

the class InferSelectionHandler method inferSelectionsForRefactor.

public static List<SelectionInfo> inferSelectionsForRefactor(InferSelectionParams params) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.context.getTextDocument().getUri());
    if (unit == null) {
        return null;
    }
    int start = DiagnosticsHelper.getStartOffset(unit, params.context.getRange());
    int end = DiagnosticsHelper.getEndOffset(unit, params.context.getRange());
    InnovationContext context = new InnovationContext(unit, start, end - start);
    List<SelectionInfo> selectionCandidates = new ArrayList<SelectionInfo>();
    ASTNode parent = context.getCoveringNode();
    try {
        if (RefactorProposalUtility.EXTRACT_METHOD_COMMAND.equals(params.command)) {
            while (parent != null && parent instanceof Expression) {
                if (parent instanceof ParenthesizedExpression) {
                    parent = parent.getParent();
                    continue;
                }
                ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
                if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                    selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength()));
                }
                parent = parent.getParent();
            }
        } else if (RefactorProposalUtility.EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND.equals(params.command) || RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND.equals(params.command)) {
            while (parent != null && parent instanceof Expression) {
                if (parent instanceof ParenthesizedExpression) {
                    parent = parent.getParent();
                    continue;
                }
                ExtractTempRefactoring refactoring = new ExtractTempRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
                if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                    selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength()));
                }
                parent = parent.getParent();
            }
        } else if (RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND.equals(params.command)) {
            while (parent != null && parent instanceof Expression) {
                if (parent instanceof ParenthesizedExpression) {
                    parent = parent.getParent();
                    continue;
                }
                ExtractConstantRefactoring refactoring = new ExtractConstantRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
                if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                    selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength()));
                }
                parent = parent.getParent();
            }
        } else if (RefactorProposalUtility.EXTRACT_FIELD_COMMAND.equals(params.command)) {
            while (parent != null && parent instanceof Expression) {
                if (parent instanceof ParenthesizedExpression) {
                    parent = parent.getParent();
                    continue;
                }
                ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
                if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                    List<String> scopes = RefactorProposalUtility.getInitializeScopes(refactoring);
                    if (!scopes.isEmpty()) {
                        selectionCandidates.add(new SelectionInfo(parent.toString(), parent.getStartPosition(), parent.getLength(), scopes));
                    }
                }
                parent = parent.getParent();
            }
        }
    } catch (CoreException e) {
    // do nothing.
    }
    if (selectionCandidates.size() == 0) {
        return null;
    }
    return selectionCandidates;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ArrayList(java.util.ArrayList) ExtractTempRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ExtractFieldRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring) ExtractMethodRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) ExtractConstantRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring)

Example 2 with ExtractTempRefactoring

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring 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)

Example 3 with ExtractTempRefactoring

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring in project eclipse.jdt.ls by eclipse.

the class RefactorProposalUtility method getExtractVariableProposal.

private static CUCorrectionProposal getExtractVariableProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
    final ICompilationUnit cu = context.getCompilationUnit();
    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;
    }
    if (inferSelectionSupport && context.getSelectionLength() == 0) {
        ASTNode parent = context.getCoveringNode();
        while (parent != null && parent instanceof Expression) {
            if (parent instanceof ParenthesizedExpression) {
                parent = parent.getParent();
                continue;
            }
            ExtractTempRefactoring refactoring = new ExtractTempRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
            if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_COMMAND, params));
            }
            parent = parent.getParent();
        }
        return null;
    }
    ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
    extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
    if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        if (returnAsCommand) {
            return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_COMMAND, params));
        }
        LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
        extractTempRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoringSelectedOnly.setCheckResultForCompileProblems(false);
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, extractTempRefactoringSelectedOnly, relevance) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setLinkedProposalModel(linkedProposalModel);
        return proposal;
    }
    return null;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModelCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) IntroduceParameterRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring) PromoteTempToFieldRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring) ExtractTempRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring) ExtractFieldRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring) ExtractMethodRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)

Example 4 with ExtractTempRefactoring

use of org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring in project eclipse.jdt.ls by eclipse.

the class RefactorProposalUtility method getExtractVariableAllOccurrenceProposal.

private static CUCorrectionProposal getExtractVariableAllOccurrenceProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
    final ICompilationUnit cu = context.getCompilationUnit();
    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;
    }
    if (inferSelectionSupport && context.getSelectionLength() == 0) {
        ASTNode parent = context.getCoveringNode();
        while (parent != null && parent instanceof Expression) {
            if (parent instanceof ParenthesizedExpression) {
                parent = parent.getParent();
                continue;
            }
            ExtractTempRefactoring refactoring = new ExtractTempRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength());
            if (refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params));
            }
            parent = parent.getParent();
        }
        return null;
    }
    ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formatterOptions);
    if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        if (returnAsCommand) {
            return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_VARIABLE_ALL_OCCURRENCE_COMMAND, params));
        }
        extractTempRefactoring.setReplaceAllOccurrences(true);
        LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
        extractTempRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoring.setCheckResultForCompileProblems(false);
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_VARIABLE, cu, extractTempRefactoring, relevance) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setLinkedProposalModel(linkedProposalModel);
        return proposal;
    }
    return null;
}
Also used : ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModelCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore) Expression(org.eclipse.jdt.core.dom.Expression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) 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) IntroduceParameterRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring) PromoteTempToFieldRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring) ExtractTempRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring) ExtractFieldRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring) ExtractMethodRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 Expression (org.eclipse.jdt.core.dom.Expression)4 ExtractConstantRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring)4 ExtractMethodRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)4 ExtractTempRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring)4 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)3 ExtractFieldRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring)3 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)3 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)3 LinkedProposalModelCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore)2 PromoteTempToFieldRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring)2 IntroduceParameterRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1