Search in sources :

Example 1 with ExtractFieldRefactoring

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

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

the class ExtractFieldTest method failHelper.

protected void failHelper(ICompilationUnit cu) throws OperationCanceledException, CoreException {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    Range range = getRange(cu, problems);
    int start = DiagnosticsHelper.getStartOffset(cu, range);
    int end = DiagnosticsHelper.getEndOffset(cu, range);
    ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(astRoot, start, end - start);
    RefactoringStatus result = refactoring.checkInitialConditions(new NullProgressMonitor());
    assertNotNull("precondition was supposed to fail", result);
    assertEquals("precondition was supposed to fail", false, result.isOK());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Range(org.eclipse.lsp4j.Range) ExtractFieldRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 3 with ExtractFieldRefactoring

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

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

the class ExtractFieldTest method helper.

protected boolean helper(ICompilationUnit cu, InitializeScope initializeIn, String expected) throws Exception {
    CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
    IProblem[] problems = astRoot.getProblems();
    Range range = getRange(cu, problems);
    int start = DiagnosticsHelper.getStartOffset(cu, range);
    int end = DiagnosticsHelper.getEndOffset(cu, range);
    ExtractFieldRefactoring refactoring = new ExtractFieldRefactoring(astRoot, start, end - start);
    refactoring.setFieldName(refactoring.guessFieldName());
    RefactoringStatus activationResult = refactoring.checkInitialConditions(new NullProgressMonitor());
    assertTrue("activation was supposed to be successful", activationResult.isOK());
    if (initializeIn != null) {
        if (initializeIn == InitializeScope.CURRENT_METHOD && !refactoring.canEnableSettingDeclareInMethod()) {
            return false;
        } else if (initializeIn == InitializeScope.CLASS_CONSTRUCTORS && !refactoring.canEnableSettingDeclareInConstructors()) {
            return false;
        } else if (initializeIn == InitializeScope.FIELD_DECLARATION && !refactoring.canEnableSettingDeclareInFieldDeclaration()) {
            return false;
        }
        refactoring.setInitializeIn(initializeIn.ordinal());
    }
    RefactoringStatus checkInputResult = refactoring.checkFinalConditions(new NullProgressMonitor());
    assertTrue("precondition was supposed to pass but was " + checkInputResult.toString(), checkInputResult.isOK());
    Change change = refactoring.createChange(new NullProgressMonitor());
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertNotNull(change);
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(edit);
    assertEquals(expected, actual);
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Change(org.eclipse.ltk.core.refactoring.Change) Range(org.eclipse.lsp4j.Range) ExtractFieldRefactoring(org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Aggregations

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 ExtractFieldRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractFieldRefactoring)4 IProblem (org.eclipse.jdt.core.compiler.IProblem)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 Expression (org.eclipse.jdt.core.dom.Expression)2 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)2 ExtractConstantRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractConstantRefactoring)2 ExtractMethodRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractMethodRefactoring)2 ExtractTempRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.ExtractTempRefactoring)2 Range (org.eclipse.lsp4j.Range)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 LinkedProposalModelCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore)1 PromoteTempToFieldRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring)1 IntroduceParameterRefactoring (org.eclipse.jdt.ls.core.internal.corext.refactoring.code.IntroduceParameterRefactoring)1 InnovationContext (org.eclipse.jdt.ls.core.internal.corrections.InnovationContext)1