use of org.eclipse.jdt.core.manipulation.CleanUpContextCore 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;
}
Aggregations