Search in sources :

Example 11 with LinkedProposalModelCore

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

the class RefactorProposalUtility method getExtractFieldProposal.

private static CUCorrectionProposal getExtractFieldProposal(CodeActionParams params, IInvocationContext context, boolean problemsAtLocation, Map formatterOptions, String initializeIn, boolean returnAsCommand, boolean inferSelectionSupport) throws CoreException {
    if (!supportsExtractVariable(context)) {
        return null;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    String label = CorrectionMessages.QuickAssistProcessor_extract_to_field_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 (context.getSelectionLength() == 0 && inferSelectionSupport) {
        ASTNode parent = context.getCoveringNode();
        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()) {
                InitializeScope scope = InitializeScope.fromName(initializeIn);
                if (scope != null) {
                    refactoring.setInitializeIn(scope.ordinal());
                }
                List<String> scopes = getInitializeScopes(refactoring);
                if (!scopes.isEmpty()) {
                    return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_FIELD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_FIELD_COMMAND, params));
                }
            }
            parent = parent.getParent();
        }
        return null;
    }
    ExtractFieldRefactoring extractFieldRefactoringSelectedOnly = new ExtractFieldRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    extractFieldRefactoringSelectedOnly.setFormatterOptions(formatterOptions);
    if (extractFieldRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        InitializeScope scope = InitializeScope.fromName(initializeIn);
        if (scope != null) {
            extractFieldRefactoringSelectedOnly.setInitializeIn(scope.ordinal());
        }
        if (returnAsCommand) {
            List<String> scopes = getInitializeScopes(extractFieldRefactoringSelectedOnly);
            return new CUCorrectionCommandProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_FIELD, cu, relevance, APPLY_REFACTORING_COMMAND_ID, Arrays.asList(EXTRACT_FIELD_COMMAND, params, new ExtractFieldInfo(scopes)));
        }
        LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
        extractFieldRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, JavaCodeActionKind.REFACTOR_EXTRACT_FIELD, cu, extractFieldRefactoringSelectedOnly, relevance) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractFieldRefactoring etr = (ExtractFieldRefactoring) refactoring;
                // expensive
                etr.setFieldName(etr.guessFieldName());
            }
        };
        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) ExtractFieldRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) 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) 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 12 with LinkedProposalModelCore

use of org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore 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

LinkedProposalModelCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)9 ASTNode (org.eclipse.jdt.core.dom.ASTNode)7 IntroduceParameterRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring)7 Expression (org.eclipse.jdt.core.dom.Expression)6 ExtractMethodRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)6 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)5 PromoteTempToFieldRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring)5 ExtractConstantRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring)5 ExtractFieldRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring)5 ExtractTempRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring)5 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)5 LinkedCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.LinkedCorrectionProposal)3 ArrayList (java.util.ArrayList)2 Block (org.eclipse.jdt.core.dom.Block)2 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2