Search in sources :

Example 41 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class SourceAssistProcessor method getGenerateToStringAction.

private Optional<Either<Command, CodeAction>> getGenerateToStringAction(CodeActionParams params, String kind) {
    if (!preferenceManager.getClientPreferences().isGenerateToStringPromptSupported()) {
        return Optional.empty();
    }
    Command command = new Command(ActionMessages.GenerateToStringAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATETOSTRINGPROMPT, Collections.singletonList(params));
    if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_TO_STRING)) {
        CodeAction codeAction = new CodeAction(ActionMessages.GenerateToStringAction_ellipsisLabel);
        codeAction.setKind(kind);
        codeAction.setCommand(command);
        codeAction.setDiagnostics(Collections.emptyList());
        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)

Example 42 with Command

use of org.eclipse.lsp4j.Command 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 43 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class SourceAssistProcessor method getCodeActionFromProposal.

private Optional<Either<Command, CodeAction>> getCodeActionFromProposal(CodeActionContext context, ICompilationUnit cu, String name, String kind, CodeActionProposal proposal) {
    if (preferenceManager.getClientPreferences().isResolveCodeActionSupported()) {
        CodeAction codeAction = new CodeAction(name);
        codeAction.setKind(kind);
        codeAction.setData(proposal);
        codeAction.setDiagnostics(Collections.EMPTY_LIST);
        return Optional.of(Either.forRight(codeAction));
    }
    try {
        WorkspaceEdit edit = proposal.resolveEdit(new NullProgressMonitor());
        if (!ChangeUtil.hasChanges(edit)) {
            return Optional.empty();
        }
        Command command = new Command(name, CodeActionHandler.COMMAND_ID_APPLY_EDIT, Collections.singletonList(edit));
        if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(kind)) {
            CodeAction codeAction = new CodeAction(name);
            codeAction.setKind(kind);
            codeAction.setCommand(command);
            codeAction.setDiagnostics(context.getDiagnostics());
            return Optional.of(Either.forRight(codeAction));
        } else {
            return Optional.of(Either.forLeft(command));
        }
    } catch (OperationCanceledException | CoreException e) {
        JavaLanguageServerPlugin.logException("Problem converting proposal to code actions", e);
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit)

Example 44 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class CodeActionHandlerTest method testCodeAction_organizeImportsSourceActionOnly.

@Test
public void testCodeAction_organizeImportsSourceActionOnly() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.util.List;\n" + "public class Foo {\n" + "	void foo() {\n" + "		String bar = \"astring\";" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "bar");
    params.setRange(range);
    CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)), Collections.singletonList(CodeActionKind.SourceOrganizeImports));
    params.setContext(context);
    List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
    Assert.assertNotNull(codeActions);
    Assert.assertFalse("No organize imports actions were found", codeActions.isEmpty());
    for (Either<Command, CodeAction> codeAction : codeActions) {
        Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.SourceOrganizeImports));
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Example 45 with Command

use of org.eclipse.lsp4j.Command in project eclipse.jdt.ls by eclipse.

the class CodeActionHandlerTest method testCodeAction_quickfixActionsOnly.

@Test
public void testCodeAction_quickfixActionsOnly() throws Exception {
    ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "public class Foo {\n" + "	void foo() {\n" + "		String bar = \"astring\";" + "	}\n" + "}\n");
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit, "bar");
    params.setRange(range);
    CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.LocalVariableIsNeverUsed), range)), Collections.singletonList(CodeActionKind.QuickFix));
    params.setContext(context);
    List<Either<Command, CodeAction>> quickfixActions = getCodeActions(params);
    Assert.assertNotNull(quickfixActions);
    Assert.assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
    for (Either<Command, CodeAction> codeAction : quickfixActions) {
        Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.QuickFix));
    }
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) Test(org.junit.Test)

Aggregations

Command (org.eclipse.lsp4j.Command)95 CodeAction (org.eclipse.lsp4j.CodeAction)58 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 Test (org.junit.Test)46 Range (org.eclipse.lsp4j.Range)36 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)35 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)26 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)23 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 List (java.util.List)22 Position (org.eclipse.lsp4j.Position)22 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)21 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)17 Diagnostic (org.eclipse.lsp4j.Diagnostic)17 Arrays (java.util.Arrays)14 Collections (java.util.Collections)14 JDTUtils (org.eclipse.jdt.ls.core.internal.JDTUtils)13 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)13 Optional (java.util.Optional)12