Search in sources :

Example 1 with AssignToVariableAssistCommandProposal

use of org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal in project eclipse.jdt.ls by eclipse.

the class CodeActionHandler method getCodeActionFromProposal.

private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(ChangeCorrectionProposal proposal, CodeActionContext context) throws CoreException {
    String name = proposal.getName();
    Command command = null;
    if (proposal instanceof CUCorrectionCommandProposal) {
        CUCorrectionCommandProposal commandProposal = (CUCorrectionCommandProposal) proposal;
        command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments());
    } else if (proposal instanceof RefactoringCorrectionCommandProposal) {
        RefactoringCorrectionCommandProposal commandProposal = (RefactoringCorrectionCommandProposal) proposal;
        command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments());
    } else if (proposal instanceof AssignToVariableAssistCommandProposal) {
        AssignToVariableAssistCommandProposal commandProposal = (AssignToVariableAssistCommandProposal) proposal;
        command = new Command(name, commandProposal.getCommand(), commandProposal.getCommandArguments());
    } else {
        if (!this.preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
            WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(proposal.getChange());
            if (!ChangeUtil.hasChanges(edit)) {
                return Optional.empty();
            }
            command = new Command(name, COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
        }
    }
    if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(proposal.getKind())) {
        // TODO: Should set WorkspaceEdit directly instead of Command
        CodeAction codeAction = new CodeAction(name);
        codeAction.setKind(proposal.getKind());
        if (command == null) {
            // lazy resolve the edit.
            codeAction.setData(proposal);
        } else {
            codeAction.setCommand(command);
        }
        codeAction.setDiagnostics(context.getDiagnostics());
        return Optional.of(Either.forRight(codeAction));
    } else {
        return Optional.of(Either.forLeft(command));
    }
}
Also used : Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) CUCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal) RefactoringCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) AssignToVariableAssistCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal)

Aggregations

AssignToVariableAssistCommandProposal (org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal)1 CUCorrectionCommandProposal (org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal)1 RefactoringCorrectionCommandProposal (org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 Command (org.eclipse.lsp4j.Command)1 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)1