Search in sources :

Example 1 with CUCorrectionProposal

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

the class OrganizeImportsCommand method organizeImportsInCompilationUnit.

public void organizeImportsInCompilationUnit(ICompilationUnit unit, WorkspaceEdit rootEdit) {
    try {
        InnovationContext context = new InnovationContext(unit, 0, unit.getBuffer().getLength() - 1);
        CUCorrectionProposal proposal = new CUCorrectionProposal("OrganizeImports", unit, IProposalRelevance.ORGANIZE_IMPORTS) {

            @Override
            protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException {
                CompilationUnit astRoot = context.getASTRoot();
                OrganizeImportsOperation op = new OrganizeImportsOperation(unit, astRoot, true, false, true, null);
                editRoot.addChild(op.createTextEdit(null));
            }
        };
        addWorkspaceEdit(unit, proposal, rootEdit);
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem organize imports ", e);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) OrganizeImportsOperation(org.eclipse.jdt.core.manipulation.OrganizeImportsOperation) CUCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) TextEdit(org.eclipse.text.edits.TextEdit) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) IDocument(org.eclipse.jface.text.IDocument)

Example 2 with CUCorrectionProposal

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

the class QuickAssistProcessor method getAssists.

public CUCorrectionProposal[] getAssists(IInvocationContext context, IProblemLocation[] locations) throws CoreException {
    ASTNode coveringNode = context.getCoveringNode();
    if (coveringNode != null) {
        ArrayList<ASTNode> coveredNodes = getFullyCoveredNodes(context, coveringNode);
        ArrayList<CUCorrectionProposal> resultingCollections = new ArrayList<>();
        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);
            getExtractVariableProposal(context, problemsAtLocation, resultingCollections);
            getExtractMethodProposal(context, coveringNode, problemsAtLocation, resultingCollections);
        // getInlineLocalProposal(context, coveringNode, resultingCollections);
        // getConvertLocalToFieldProposal(context, coveringNode, resultingCollections);
        // getConvertAnonymousToNestedProposal(context, coveringNode, resultingCollections);
        // getConvertAnonymousClassCreationsToLambdaProposals(context, coveringNode, resultingCollections);
        // getConvertLambdaToAnonymousClassCreationsProposals(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);
        // getMakeVariableDeclarationFinalProposals(context, resultingCollections);
        // getConvertStringConcatenationProposals(context, resultingCollections);
        // getMissingCaseStatementProposals(context, coveringNode, resultingCollections);
        }
        return resultingCollections.toArray(new CUCorrectionProposal[resultingCollections.size()]);
    }
    return new CUCorrectionProposal[0];
}
Also used : CUCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ArrayList(java.util.ArrayList)

Example 3 with CUCorrectionProposal

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

the class CodeActionHandler method getCodeActionCommands.

/**
 * @param params
 * @return
 */
public List<Command> getCodeActionCommands(CodeActionParams params, IProgressMonitor monitor) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null) {
        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(getASTRoot(unit));
    IProblemLocation[] locations = this.getProblemLocations(unit, params.getContext().getDiagnostics());
    List<Command> $ = new ArrayList<>();
    try {
        CUCorrectionProposal[] corrections = this.quickFixProcessor.getCorrections(context, locations);
        Arrays.sort(corrections, new CUCorrectionProposalComparator());
        for (CUCorrectionProposal proposal : corrections) {
            Command command = this.getCommandFromProposal(proposal);
            $.add(command);
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem resolving code actions", e);
    }
    try {
        CUCorrectionProposal[] corrections = this.quickAssistProcessor.getAssists(context, locations);
        Arrays.sort(corrections, new CUCorrectionProposalComparator());
        for (CUCorrectionProposal proposal : corrections) {
            Command command = this.getCommandFromProposal(proposal);
            $.add(command);
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.logException("Problem resolving code actions", e);
    }
    return $;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CUCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) IProblemLocation(org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation)

Aggregations

CUCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.CUCorrectionProposal)3 ArrayList (java.util.ArrayList)2 CoreException (org.eclipse.core.runtime.CoreException)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 InnovationContext (org.eclipse.jdt.ls.core.internal.corrections.InnovationContext)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)1 OrganizeImportsOperation (org.eclipse.jdt.core.manipulation.OrganizeImportsOperation)1 IProblemLocation (org.eclipse.jdt.ls.core.internal.corrections.IProblemLocation)1 IDocument (org.eclipse.jface.text.IDocument)1 Command (org.eclipse.lsp4j.Command)1 TextEdit (org.eclipse.text.edits.TextEdit)1