Search in sources :

Example 1 with ExtractMethodRefactoring

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

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

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

the class RefactorProposalUtility method getExtractMethodProposal.

private static CUCorrectionProposal getExtractMethodProposal(CodeActionParams params, IInvocationContext context, ASTNode coveringNode, boolean problemsAtLocation, Map formattingOptions, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
    if (!(coveringNode instanceof Expression) && !(coveringNode instanceof Statement) && !(coveringNode instanceof Block)) {
        return null;
    }
    if (coveringNode instanceof Block) {
        List<Statement> statements = ((Block) coveringNode).statements();
        int startIndex = getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1) {
            return null;
        }
        int endIndex = getIndex(context.getSelectionOffset() + context.getSelectionLength(), statements);
        if (endIndex == -1 || endIndex <= startIndex) {
            return null;
        }
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    final ExtractMethodRefactoring extractMethodRefactoring = new ExtractMethodRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength(), formattingOptions);
    String uniqueMethodName = getUniqueMethodName(coveringNode, "extracted");
    extractMethodRefactoring.setMethodName(uniqueMethodName);
    String label = CorrectionMessages.QuickAssistProcessor_extractmethod_description;
    int relevance = problemsAtLocation ? IProposalRelevance.EXTRACT_METHOD_ERROR : IProposalRelevance.EXTRACT_METHOD;
    if (context.getSelectionLength() == 0) {
        if (!inferSelectionSupport) {
            return null;
        }
        ASTNode parent = coveringNode;
        while (parent != null && parent instanceof Expression) {
            if (parent instanceof ParenthesizedExpression) {
                parent = parent.getParent();
                continue;
            }
            ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(context.getASTRoot(), parent.getStartPosition(), parent.getLength(), formattingOptions);
            if (refactoring.checkInferConditions(new NullProgressMonitor()).isOK()) {
                return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_METHOD_COMMAND, params));
            }
            parent = parent.getParent();
        }
        return null;
    } else if (extractMethodRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        if (returnAsCommand) {
            return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_METHOD_COMMAND, params));
        }
        LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
        extractMethodRefactoring.setLinkedProposalModel(linkedProposalModel);
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD, cu, extractMethodRefactoring, relevance);
        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) Statement(org.eclipse.jdt.core.dom.Statement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) ExtractMethodRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring) 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) Block(org.eclipse.jdt.core.dom.Block)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Expression (org.eclipse.jdt.core.dom.Expression)3 ExtractMethodRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 Block (org.eclipse.jdt.core.dom.Block)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 Statement (org.eclipse.jdt.core.dom.Statement)2 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)1 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1 TryStatement (org.eclipse.jdt.core.dom.TryStatement)1 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)1 LinkedProposalModelCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore)1 LinkedProposalModel (org.eclipse.jdt.ls.core.internal.corext.fix.LinkedProposalModel)1