Search in sources :

Example 1 with RefactoringCorrectionCommandProposal

use of org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal 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)

Example 2 with RefactoringCorrectionCommandProposal

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

the class RefactorProcessor method getConvertAnonymousToNestedProposal.

public static RefactoringCorrectionProposal getConvertAnonymousToNestedProposal(CodeActionParams params, IInvocationContext context, final ASTNode node, boolean returnAsCommand) throws CoreException {
    String label = CorrectionMessages.QuickAssistProcessor_convert_anonym_to_nested;
    ClassInstanceCreation cic = getClassInstanceCreation(node);
    if (cic == null) {
        return null;
    }
    final AnonymousClassDeclaration anonymTypeDecl = cic.getAnonymousClassDeclaration();
    if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null) {
        return null;
    }
    final ConvertAnonymousToNestedRefactoring refactoring = new ConvertAnonymousToNestedRefactoring(anonymTypeDecl);
    if (!refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        return null;
    }
    if (returnAsCommand) {
        return new RefactoringCorrectionCommandProposal(label, CodeActionKind.Refactor, context.getCompilationUnit(), IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED, RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, Arrays.asList(CONVERT_ANONYMOUS_CLASS_TO_NESTED_COMMAND, params));
    }
    String extTypeName = ASTNodes.getTypeName(cic.getType());
    ITypeBinding anonymTypeBinding = anonymTypeDecl.resolveBinding();
    String className;
    if (anonymTypeBinding.getInterfaces().length == 0) {
        className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_interface, extTypeName);
    } else {
        className = Messages.format(CorrectionMessages.QuickAssistProcessor_name_extension_from_class, extTypeName);
    }
    String[][] existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className);
    int i = 1;
    while (existingTypes != null) {
        i++;
        existingTypes = ((IType) anonymTypeBinding.getJavaElement()).resolveType(className + i);
    }
    refactoring.setClassName(i == 1 ? className : className + i);
    LinkedProposalModelCore linkedProposalModel = new LinkedProposalModelCore();
    refactoring.setLinkedProposalModel(linkedProposalModel);
    final ICompilationUnit cu = context.getCompilationUnit();
    RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.Refactor, cu, refactoring, IProposalRelevance.CONVERT_ANONYMOUS_TO_NESTED);
    proposal.setLinkedProposalModel(linkedProposalModel);
    return proposal;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ConvertAnonymousToNestedRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) IType(org.eclipse.jdt.core.IType) LinkedProposalModelCore(org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) RefactoringCorrectionCommandProposal(org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal)

Aggregations

RefactoringCorrectionCommandProposal (org.eclipse.jdt.ls.core.internal.text.correction.RefactoringCorrectionCommandProposal)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IType (org.eclipse.jdt.core.IType)1 AnonymousClassDeclaration (org.eclipse.jdt.core.dom.AnonymousClassDeclaration)1 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)1 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)1 LinkedProposalModelCore (org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore)1 ConvertAnonymousToNestedRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring)1 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)1 AssignToVariableAssistCommandProposal (org.eclipse.jdt.ls.core.internal.text.correction.AssignToVariableAssistCommandProposal)1 CUCorrectionCommandProposal (org.eclipse.jdt.ls.core.internal.text.correction.CUCorrectionCommandProposal)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 Command (org.eclipse.lsp4j.Command)1 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)1