Search in sources :

Example 1 with FixCorrectionProposal

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;
}
Also used : FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) HashMap(java.util.HashMap) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) LambdaExpressionsCleanUpCore(org.eclipse.jdt.internal.ui.fix.LambdaExpressionsCleanUpCore)

Example 2 with FixCorrectionProposal

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;
}
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)

Example 3 with FixCorrectionProposal

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));
        }
    }
}
Also used : FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit)

Example 4 with FixCorrectionProposal

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;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) HashMap(java.util.HashMap) IProposableFix(org.eclipse.jdt.internal.corext.fix.IProposableFix) LambdaExpressionsCleanUpCore(org.eclipse.jdt.internal.ui.fix.LambdaExpressionsCleanUpCore)

Example 5 with FixCorrectionProposal

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);
}
Also used : FixCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal) MakeTypeAbstractOperation(org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore.MakeTypeAbstractOperation) UnimplementedCodeFixCore(org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore)

Aggregations

FixCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.FixCorrectionProposal)5 IProposableFix (org.eclipse.jdt.internal.corext.fix.IProposableFix)4 HashMap (java.util.HashMap)3 LambdaExpressionsCleanUpCore (org.eclipse.jdt.internal.ui.fix.LambdaExpressionsCleanUpCore)2 ArrayList (java.util.ArrayList)1 CoreException (org.eclipse.core.runtime.CoreException)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)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 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)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 UnimplementedCodeFixCore (org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore)1 MakeTypeAbstractOperation (org.eclipse.jdt.internal.corext.fix.UnimplementedCodeFixCore.MakeTypeAbstractOperation)1 AbstractCleanUpCore (org.eclipse.jdt.internal.ui.fix.AbstractCleanUpCore)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 Command (org.eclipse.lsp4j.Command)1