Search in sources :

Example 1 with ICleanUpCore

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

the class RefactorProcessor method getConvertForLoopProposal.

private static boolean getConvertForLoopProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
    ForStatement forStatement = getEnclosingForStatementHeader(node);
    if (forStatement == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    IProposableFix fix = ConvertLoopFixCore.createConvertForLoopToEnhancedFix(context.getASTRoot(), forStatement);
    if (fix == null) {
        return false;
    }
    Map<String, String> options = new HashMap<>();
    options.put(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED, CleanUpOptionsCore.TRUE);
    ICleanUpCore cleanUp = new AbstractCleanUpCore(options) {

        @Override
        public CleanUpRequirementsCore getRequirementsCore() {
            return new CleanUpRequirementsCore(isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED), false, false, null);
        }

        @Override
        public ICleanUpFixCore createFixCore(CleanUpContextCore context) throws CoreException {
            CompilationUnit compilationUnit = context.getAST();
            if (compilationUnit == null) {
                return null;
            }
            boolean convertForLoops = isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED);
            boolean checkIfLoopVarUsed = isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_ONLY_IF_LOOP_VAR_USED);
            return ConvertLoopFixCore.createCleanUp(compilationUnit, convertForLoops, convertForLoops, isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES), checkIfLoopVarUsed);
        }

        @Override
        public String[] getStepDescriptions() {
            List<String> result = new ArrayList<>();
            if (isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED)) {
                result.add(MultiFixMessages.Java50CleanUp_ConvertToEnhancedForLoop_description);
                if (isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_ONLY_IF_LOOP_VAR_USED)) {
                    result.add(MultiFixMessages.Java50CleanUp_ConvertLoopOnlyIfLoopVarUsed_description);
                }
            }
            return result.toArray(new String[result.size()]);
        }

        @Override
        public String getPreview() {
            StringBuilder buf = new StringBuilder();
            if (isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED)) {
                // $NON-NLS-1$
                buf.append("for (int element : ids) {\n");
                // $NON-NLS-1$
                buf.append("    double value= element / 2; \n");
                // $NON-NLS-1$
                buf.append("    System.out.println(value);\n");
                // $NON-NLS-1$
                buf.append("}\n");
            } else {
                // $NON-NLS-1$
                buf.append("for (int i = 0; i < ids.length; i++) {\n");
                // $NON-NLS-1$
                buf.append("    double value= ids[i] / 2; \n");
                // $NON-NLS-1$
                buf.append("    System.out.println(value);\n");
                // $NON-NLS-1$
                buf.append("}\n");
            }
            if (isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_TO_ENHANCED) && !isEnabled(CleanUpConstants.CONTROL_STATMENTS_CONVERT_FOR_LOOP_ONLY_IF_LOOP_VAR_USED)) {
                // $NON-NLS-1$
                buf.append("for (int id : ids) {\n");
                // $NON-NLS-1$
                buf.append("    System.out.println(\"here\");\n");
                // $NON-NLS-1$
                buf.append("}\n");
            } else {
                // $NON-NLS-1$
                buf.append("for (int i = 0; i < ids.length; i++) {\n");
                // $NON-NLS-1$
                buf.append("    System.out.println(\"here\");\n");
                // $NON-NLS-1$
                buf.append("}\n");
            }
            return buf.toString();
        }
    };
    FixCorrectionProposal proposal = new FixCorrectionProposal(fix, cleanUp, IProposalRelevance.CONVERT_FOR_LOOP_TO_ENHANCED, context, CodeActionKind.Refactor);
    resultingCollections.add(proposal);
    return true;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CleanUpContextCore(org.eclipse.jdt.core.manipulation.CleanUpContextCore) HashMap(java.util.HashMap) CleanUpRequirementsCore(org.eclipse.jdt.core.manipulation.CleanUpRequirementsCore) ArrayList(java.util.ArrayList) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) ICleanUpCore(org.eclipse.jdt.internal.corext.fix.ICleanUpCore) FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) AbstractCleanUpCore(org.eclipse.jdt.internal.ui.fix.AbstractCleanUpCore) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)1 ForStatement (org.eclipse.jdt.core.dom.ForStatement)1 CleanUpContextCore (org.eclipse.jdt.core.manipulation.CleanUpContextCore)1 CleanUpRequirementsCore (org.eclipse.jdt.core.manipulation.CleanUpRequirementsCore)1 ICleanUpCore (org.eclipse.jdt.internal.corext.fix.ICleanUpCore)1 IProposableFix (org.eclipse.jdt.internal.corext.fix.IProposableFix)1 AbstractCleanUpCore (org.eclipse.jdt.internal.ui.fix.AbstractCleanUpCore)1 FixCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal)1