Search in sources :

Example 6 with ChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getAssists.

public List<ChangeCorrectionProposal> getAssists(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations) throws CoreException {
    ASTNode coveringNode = context.getCoveringNode();
    if (coveringNode != null) {
        // ArrayList<ASTNode> coveredNodes = getFullyCoveredNodes(context, coveringNode);
        ArrayList<ChangeCorrectionProposal> resultingCollections = new ArrayList<>();
        // quick assists that show up also if there is an error/warning
        // getRenameLocalProposals(context, coveringNode, locations, resultingCollections);
        // getRenameRefactoringProposal(context, coveringNode, locations, resultingCollections);
        // getAssignToVariableProposals(context, coveringNode, locations, resultingCollections);
        getAssignParamToFieldProposals(context, coveringNode, resultingCollections);
        getAssignAllParamsToFieldsProposals(context, coveringNode, resultingCollections);
        // getInferDiamondArgumentsProposal(context, coveringNode, locations, resultingCollections);
        // getGenerateForLoopProposals(context, coveringNode, locations, resultingCollections);
        // boolean noErrorsAtLocation = noErrorsAtLocation(locations);
        // if (noErrorsAtLocation) {
        // boolean problemsAtLocation = locations.length != 0;
        // getCatchClauseToThrowsProposals(context, coveringNode, resultingCollections);
        // getPickoutTypeFromMulticatchProposals(context, coveringNode, coveredNodes, resultingCollections);
        // getConvertToMultiCatchProposals(context, coveringNode, resultingCollections);
        // getUnrollMultiCatchProposals(context, coveringNode, resultingCollections);
        // getUnWrapProposals(context, coveringNode, resultingCollections);
        // getJoinVariableProposals(context, coveringNode, resultingCollections);
        // getSplitVariableProposals(context, coveringNode, resultingCollections);
        // getAddFinallyProposals(context, coveringNode, resultingCollections);
        // getAddElseProposals(context, coveringNode, resultingCollections);
        // getAddBlockProposals(context, coveringNode, resultingCollections);
        // getInvertEqualsProposal(context, coveringNode, resultingCollections);
        // getArrayInitializerToArrayCreation(context, coveringNode, resultingCollections);
        // getCreateInSuperClassProposals(context, coveringNode, resultingCollections);
        // getConvertLocalToFieldProposal(context, coveringNode, resultingCollections);
        // getChangeLambdaBodyToBlockProposal(context, coveringNode, resultingCollections);
        // getChangeLambdaBodyToExpressionProposal(context, coveringNode, resultingCollections);
        // getAddInferredLambdaParameterTypes(context, coveringNode, resultingCollections);
        getConvertMethodReferenceToLambdaProposal(context, coveringNode, resultingCollections);
        getConvertLambdaToMethodReferenceProposal(context, coveringNode, resultingCollections);
        // getFixParenthesesInLambdaExpression(context, coveringNode, resultingCollections);
        // if (!getConvertForLoopProposal(context, coveringNode, resultingCollections)) {
        // getConvertIterableLoopProposal(context, coveringNode, resultingCollections);
        // }
        // getConvertEnhancedForLoopProposal(context, coveringNode, resultingCollections);
        // getRemoveBlockProposals(context, coveringNode, resultingCollections);
        // getConvertStringConcatenationProposals(context, resultingCollections);
        // getMissingCaseStatementProposals(context, coveringNode, resultingCollections);
        // }
        getAddMethodDeclaration(context, coveringNode, resultingCollections);
        getTryWithResourceProposals(locations, context, coveringNode, resultingCollections);
        getConvertToSwitchExpressionProposals(context, coveringNode, resultingCollections);
        return resultingCollections;
    }
    return Collections.emptyList();
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal)

Example 7 with ChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class CodeActionHandler method getCodeActionCommands.

public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams params, IProgressMonitor monitor) {
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null || monitor.isCanceled()) {
        return Collections.emptyList();
    }
    Map<String, Object> formattingOptions = ConfigurationHandler.getFormattingOptions(params.getTextDocument().getUri());
    if (formattingOptions != null && !formattingOptions.isEmpty()) {
        Object tabSizeValue = formattingOptions.get(Preferences.JAVA_CONFIGURATION_TABSIZE);
        Object insertSpacesValue = formattingOptions.get(Preferences.JAVA_CONFIGURATION_INSERTSPACES);
        Map<String, String> customOptions = new HashMap<>();
        if (tabSizeValue != null) {
            try {
                int tabSize = Integer.parseInt(String.valueOf(tabSizeValue));
                if (tabSize > 0) {
                    customOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, Integer.toString(tabSize));
                }
            } catch (Exception ex) {
            // do nothing
            }
        }
        if (insertSpacesValue != null) {
            boolean insertSpaces = Boolean.parseBoolean(String.valueOf(insertSpacesValue));
            customOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, insertSpaces ? JavaCore.SPACE : JavaCore.TAB);
        }
        if (!customOptions.isEmpty()) {
            unit.setOptions(customOptions);
        }
    }
    CompilationUnit astRoot = getASTRoot(unit, monitor);
    if (astRoot == null || monitor.isCanceled()) {
        return Collections.emptyList();
    }
    int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
    int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
    InnovationContext context = new InnovationContext(unit, start, end - start);
    context.setASTRoot(astRoot);
    List<Diagnostic> diagnostics = params.getContext().getDiagnostics().stream().filter((d) -> {
        return JavaLanguageServerPlugin.SERVER_SOURCE_ID.equals(d.getSource());
    }).collect(Collectors.toList());
    IProblemLocationCore[] locations = getProblemLocationCores(unit, diagnostics);
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    List<String> codeActionKinds = new ArrayList<>();
    if (params.getContext().getOnly() != null && !params.getContext().getOnly().isEmpty()) {
        codeActionKinds.addAll(params.getContext().getOnly());
    } else {
        List<String> defaultCodeActionKinds = Arrays.asList(CodeActionKind.QuickFix, CodeActionKind.Refactor, JavaCodeActionKind.QUICK_ASSIST, CodeActionKind.Source);
        codeActionKinds.addAll(defaultCodeActionKinds);
    }
    List<Either<Command, CodeAction>> codeActions = new ArrayList<>();
    List<ChangeCorrectionProposal> proposals = new ArrayList<>();
    ChangeCorrectionProposalComparator comparator = new ChangeCorrectionProposalComparator();
    if (containsKind(codeActionKinds, CodeActionKind.QuickFix)) {
        try {
            codeActions.addAll(nonProjectFixProcessor.getCorrections(params, context, locations));
            List<ChangeCorrectionProposal> quickfixProposals = this.quickFixProcessor.getCorrections(context, locations);
            quickfixProposals.sort(comparator);
            proposals.addAll(quickfixProposals);
        } catch (CoreException e) {
            JavaLanguageServerPlugin.logException("Problem resolving quick fix code actions", e);
        }
    }
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    if (containsKind(codeActionKinds, CodeActionKind.Refactor)) {
        try {
            List<ChangeCorrectionProposal> refactorProposals = this.refactorProcessor.getProposals(params, context, locations);
            refactorProposals.sort(comparator);
            proposals.addAll(refactorProposals);
        } catch (CoreException e) {
            JavaLanguageServerPlugin.logException("Problem resolving refactor code actions", e);
        }
    }
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    if (containsKind(codeActionKinds, JavaCodeActionKind.QUICK_ASSIST)) {
        try {
            List<ChangeCorrectionProposal> quickassistProposals = this.quickAssistProcessor.getAssists(params, context, locations);
            quickassistProposals.sort(comparator);
            proposals.addAll(quickassistProposals);
        } catch (CoreException e) {
            JavaLanguageServerPlugin.logException("Problem resolving quick assist code actions", e);
        }
    }
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    try {
        for (ChangeCorrectionProposal proposal : proposals) {
            Optional<Either<Command, CodeAction>> codeActionFromProposal = getCodeActionFromProposal(proposal, params.getContext());
            if (codeActionFromProposal.isPresent() && !codeActions.contains(codeActionFromProposal.get())) {
                codeActions.add(codeActionFromProposal.get());
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
    }
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    if (containsKind(codeActionKinds, CodeActionKind.Source)) {
        codeActions.addAll(sourceAssistProcessor.getSourceActionCommands(params, context, locations, monitor));
    }
    if (monitor.isCanceled()) {
        return Collections.emptyList();
    }
    populateDataFields(codeActions);
    return codeActions;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) StringUtils(org.apache.commons.lang3.StringUtils) DefaultCodeFormatterConstants(org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants) QuickFixProcessor(org.eclipse.jdt.ls.core.internal.corrections.QuickFixProcessor) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) ProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.ProblemLocationCore) Collectors(java.util.stream.Collectors) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) QuickAssistProcessor(org.eclipse.jdt.ls.core.internal.text.correction.QuickAssistProcessor) IJavaModelMarker(org.eclipse.jdt.core.IJavaModelMarker) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) List(java.util.List) JsonArray(com.google.gson.JsonArray) Command(org.eclipse.lsp4j.Command) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Optional(java.util.Optional) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) RefactoringCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) HashMap(java.util.HashMap) Diagnostic(org.eclipse.lsp4j.Diagnostic) RefactorProcessor(org.eclipse.jdt.ls.core.internal.corrections.RefactorProcessor) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) ArrayList(java.util.ArrayList) SourceAssistProcessor(org.eclipse.jdt.ls.core.internal.text.correction.SourceAssistProcessor) JsonElement(com.google.gson.JsonElement) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CUCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) NonProjectFixProcessor(org.eclipse.jdt.ls.core.internal.text.correction.NonProjectFixProcessor) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) AssignToVariableAssistCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal) CoreASTProvider(org.eclipse.jdt.core.manipulation.CoreASTProvider) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ForkJoinPool(java.util.concurrent.ForkJoinPool) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Comparator(java.util.Comparator) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) IProblemLocationCore(org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) CoreException(org.eclipse.core.runtime.CoreException)

Example 8 with ChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class RefactorProcessor method getProposals.

public List<ChangeCorrectionProposal> getProposals(CodeActionParams params, IInvocationContext context, IProblemLocationCore[] locations) throws CoreException {
    ASTNode coveringNode = context.getCoveringNode();
    if (coveringNode != null) {
        ArrayList<ChangeCorrectionProposal> proposals = new ArrayList<>();
        InvertBooleanUtility.getInverseConditionProposals(params, context, coveringNode, proposals);
        getInverseLocalVariableProposals(params, context, coveringNode, proposals);
        getMoveRefactoringProposals(params, context, coveringNode, proposals);
        boolean noErrorsAtLocation = noErrorsAtLocation(locations, coveringNode);
        if (noErrorsAtLocation) {
            boolean problemsAtLocation = locations.length != 0;
            getExtractVariableProposal(params, context, problemsAtLocation, proposals);
            getExtractMethodProposal(params, context, coveringNode, problemsAtLocation, proposals);
            getExtractFieldProposal(params, context, problemsAtLocation, proposals);
            getInlineProposal(context, coveringNode, proposals);
            getConvertAnonymousToNestedProposals(params, context, coveringNode, proposals);
            getConvertAnonymousClassCreationsToLambdaProposals(context, coveringNode, proposals);
            getConvertLambdaToAnonymousClassCreationsProposals(context, coveringNode, proposals);
            getConvertVarTypeToResolvedTypeProposal(context, coveringNode, proposals);
            getConvertResolvedTypeToVarTypeProposal(context, coveringNode, proposals);
            getAddStaticImportProposals(context, coveringNode, proposals);
            getConvertForLoopProposal(context, coveringNode, proposals);
            getAssignToVariableProposals(context, coveringNode, locations, proposals, params);
            getIntroduceParameterProposals(params, context, coveringNode, locations, proposals);
        }
        return proposals;
    }
    return Collections.emptyList();
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList) TypeChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal)

Example 9 with ChangeCorrectionProposal

use of org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal in project eclipse.jdt.ls by eclipse.

the class QuickAssistProcessor method getConvertToSwitchExpressionProposals.

private static boolean getConvertToSwitchExpressionProposals(IInvocationContext context, ASTNode covering, Collection<ChangeCorrectionProposal> resultingCollections) {
    if (covering instanceof Block) {
        List<Statement> statements = ((Block) covering).statements();
        int startIndex = QuickAssistProcessorUtil.getIndex(context.getSelectionOffset(), statements);
        if (startIndex == -1 || startIndex >= statements.size()) {
            return false;
        }
        covering = statements.get(startIndex);
    } else {
        while (covering instanceof SwitchCase || covering instanceof SwitchExpression) {
            covering = covering.getParent();
        }
    }
    SwitchStatement switchStatement;
    if (covering instanceof SwitchStatement) {
        switchStatement = (SwitchStatement) covering;
    } else {
        return false;
    }
    SwitchExpressionsFixCore fix = SwitchExpressionsFixCore.createConvertToSwitchExpressionFix(switchStatement);
    if (fix == null) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    // add correction proposal
    try {
        CompilationUnitChange change = fix.createChange(null);
        ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(fix.getDisplayString(), JavaCodeActionKind.QUICK_ASSIST, change, IProposalRelevance.CONVERT_TO_SWITCH_EXPRESSION);
        resultingCollections.add(proposal);
    } catch (CoreException e) {
    // continue
    }
    return true;
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) CoreException(org.eclipse.core.runtime.CoreException) SwitchExpressionsFixCore(org.eclipse.jdt.internal.corext.fix.SwitchExpressionsFixCore) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

ChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal)9 ArrayList (java.util.ArrayList)5 CoreException (org.eclipse.core.runtime.CoreException)5 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 IProblemLocationCore (org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore)3 TypeChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 IJavaModelMarker (org.eclipse.jdt.core.IJavaModelMarker)2 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)2 IMethod (org.eclipse.jdt.core.IMethod)2 JavaCore (org.eclipse.jdt.core.JavaCore)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 IBinding (org.eclipse.jdt.core.dom.IBinding)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2