use of org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getConvertLambdaToAnonymousClassCreationsProposals.
private static boolean getConvertLambdaToAnonymousClassCreationsProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
if (resultingCollections == null) {
return true;
}
LambdaExpression lambda;
if (covering instanceof LambdaExpression) {
lambda = (LambdaExpression) covering;
} else if (covering.getLocationInParent() == LambdaExpression.BODY_PROPERTY) {
lambda = (LambdaExpression) covering.getParent();
} else {
return false;
}
IProposableFix fix = LambdaExpressionsFixCore.createConvertToAnonymousClassCreationsFix(lambda);
if (fix == null) {
return false;
}
// add correction proposal
Map<String, String> options = new HashMap<>();
options.put(CleanUpConstants.CONVERT_FUNCTIONAL_INTERFACES, CleanUpOptionsCore.TRUE);
options.put(CleanUpConstants.USE_ANONYMOUS_CLASS_CREATION, CleanUpOptionsCore.TRUE);
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new LambdaExpressionsCleanUpCore(options), IProposalRelevance.CONVERT_TO_ANONYMOUS_CLASS_CREATION, context, CodeActionKind.Refactor);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal 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;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method addFinalModifierWherePossibleAction.
private Optional<Either<Command, CodeAction>> addFinalModifierWherePossibleAction(IInvocationContext context) {
IProposableFix fix = (IProposableFix) VariableDeclarationFixCore.createCleanUp(context.getASTRoot(), true, true, true);
if (fix == null) {
return Optional.empty();
}
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, null, IProposalRelevance.MAKE_VARIABLE_DECLARATION_FINAL, context, JavaCodeActionKind.SOURCE_GENERATE_FINAL_MODIFIERS);
if (this.preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateFinalModifiersAction_label);
codeAction.setKind(proposal.getKind());
codeAction.setData(proposal);
codeAction.setDiagnostics(Collections.EMPTY_LIST);
return Optional.of(Either.forRight(codeAction));
} else {
WorkspaceEdit edit;
try {
edit = ChangeUtil.convertToWorkspaceEdit(proposal.getChange());
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
return Optional.empty();
}
if (!ChangeUtil.hasChanges(edit)) {
return Optional.empty();
}
Command command = new Command(ActionMessages.GenerateFinalModifiersAction_label, CodeActionHandler.COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(proposal.getKind())) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateFinalModifiersAction_label);
codeAction.setKind(proposal.getKind());
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.EMPTY_LIST);
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class RefactorProcessor method getConvertAnonymousClassCreationsToLambdaProposals.
private static boolean getConvertAnonymousClassCreationsToLambdaProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
ClassInstanceCreation cic = getClassInstanceCreation(covering);
if (cic == null) {
return false;
}
IProposableFix fix = LambdaExpressionsFixCore.createConvertToLambdaFix(cic);
if (fix == null) {
return false;
}
if (resultingCollections == null) {
return true;
}
Map<String, String> options = new HashMap<>();
options.put(CleanUpConstants.CONVERT_FUNCTIONAL_INTERFACES, CleanUpOptionsCore.TRUE);
options.put(CleanUpConstants.USE_LAMBDA, CleanUpOptionsCore.TRUE);
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, new LambdaExpressionsCleanUpCore(options), IProposalRelevance.CONVERT_TO_LAMBDA_EXPRESSION, context, CodeActionKind.Refactor);
resultingCollections.add(proposal);
return true;
}
use of org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal in project eclipse.jdt.ls by eclipse.
the class ModifierCorrectionSubProcessor method addMakeTypeAbstractProposal.
private static void addMakeTypeAbstractProposal(IInvocationContext context, TypeDeclaration parentTypeDecl, Collection<ChangeCorrectionProposal> proposals) {
MakeTypeAbstractOperation operation = new UnimplementedCodeFixCore.MakeTypeAbstractOperation(parentTypeDecl);
String label = Messages.format(CorrectionMessages.ModifierCorrectionSubProcessor_addabstract_description, BasicElementLabels.getJavaElementName(parentTypeDecl.getName().getIdentifier()));
UnimplementedCodeFixCore fix = new UnimplementedCodeFixCore(label, context.getASTRoot(), new CompilationUnitRewriteOperation[] { operation });
FixCorrectionProposal proposal = new FixCorrectionProposal(fix, null, IProposalRelevance.MAKE_TYPE_ABSTRACT_FIX, context);
proposals.add(proposal);
}
Aggregations