Search in sources :

Example 81 with Command

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

the class GenerateDelegateMethodsActionTest method testGenerateDelegateMethodsEnabled.

@Test
public void testGenerateDelegateMethodsEnabled() throws JavaModelException {
    // @formatter:off
    ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + "\r\n" + "public class A {\r\n" + "	String name;\r\n" + "}", true, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String name");
    List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> delegateMethodsAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_DELEGATE_METHODS);
    Assert.assertNotNull(delegateMethodsAction);
    Command delegateMethodsCommand = CodeActionHandlerTest.getCommand(delegateMethodsAction);
    Assert.assertNotNull(delegateMethodsCommand);
    Assert.assertEquals(SourceAssistProcessor.COMMAND_ID_ACTION_GENERATEDELEGATEMETHODSPROMPT, delegateMethodsCommand.getCommand());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test)

Example 82 with Command

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

the class CodeActionResolveHandlerTest method testResolveCodeAction_QuickAssists.

@Test
public void testResolveCodeAction_QuickAssists() throws Exception {
    when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
    StringBuilder buf = new StringBuilder();
    buf.append("public class E {\n");
    buf.append("    public  E(int count) {\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    Range range = CodeActionUtil.getRange(unit, "count");
    params.setRange(range);
    CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(JavaCodeActionKind.QUICK_ASSIST));
    params.setContext(context);
    List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    Assert.assertFalse("No quickassist actions were found", codeActions.isEmpty());
    for (Either<Command, CodeAction> codeAction : codeActions) {
        Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
        Assert.assertNotNull("Should preserve the data property for the unresolved code action", codeAction.getRight().getData());
    }
    Optional<Either<Command, CodeAction>> assignToNewFieldResponse = codeActions.stream().filter(codeAction -> {
        return "Assign parameter to new field".equals(codeAction.getRight().getTitle());
    }).findFirst();
    Assert.assertTrue("Should return the quick assist 'Convert to method reference'", assignToNewFieldResponse.isPresent());
    CodeAction unresolvedCodeAction = assignToNewFieldResponse.get().getRight();
    CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
    Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
    buf = new StringBuilder();
    buf.append("public class E {\n");
    buf.append("    private int count;\n");
    buf.append("\n");
    buf.append("    public  E(int count) {\n");
    buf.append("        this.count = count;\n");
    buf.append("    }\n");
    buf.append("}\n");
    Assert.assertEquals(buf.toString(), actual);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 83 with Command

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

the class CodeActionResolveHandlerTest method testResolveCodeAction_Refactors.

@Test
public void testResolveCodeAction_Refactors() throws Exception {
    when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
    StringBuilder buf = new StringBuilder();
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    public void bar(I i) {}\n");
    buf.append("    public void foo() {\n");
    buf.append("        bar(new I() {\n");
    buf.append("            @Override\n");
    buf.append("            /*[*/public void method() {\n");
    buf.append("                System.out.println();\n");
    buf.append("            }/*]*/\n");
    buf.append("        });\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit unit = defaultPackage.createCompilationUnit("E.java", buf.toString(), false, null);
    CodeActionParams params = new CodeActionParams();
    params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
    final Range range = CodeActionUtil.getRange(unit);
    params.setRange(range);
    CodeActionContext context = new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Refactor));
    params.setContext(context);
    List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    Assert.assertFalse("No refactor actions were found", codeActions.isEmpty());
    for (Either<Command, CodeAction> codeAction : codeActions) {
        Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
        Assert.assertNotNull("Should preserve the data property for the unresolved code action", codeAction.getRight().getData());
    }
    Optional<Either<Command, CodeAction>> convertToLambdaResponse = codeActions.stream().filter(codeAction -> {
        return "Convert to lambda expression".equals(codeAction.getRight().getTitle());
    }).findFirst();
    Assert.assertTrue("Should return the refactor action 'Convert to lambda expression'", convertToLambdaResponse.isPresent());
    CodeAction unresolvedCodeAction = convertToLambdaResponse.get().getRight();
    CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
    Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
    buf = new StringBuilder();
    buf.append("interface I {\n");
    buf.append("    void method();\n");
    buf.append("}\n");
    buf.append("public class E {\n");
    buf.append("    public void bar(I i) {}\n");
    buf.append("    public void foo() {\n");
    buf.append("        bar(() -> System.out.println());\n");
    buf.append("    }\n");
    buf.append("}\n");
    Assert.assertEquals(buf.toString(), actual);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 84 with Command

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

the class CodeActionResolveHandlerTest method testResolveCodeAction_QuickFixes.

@Test
public void testResolveCodeAction_QuickFixes() throws Exception {
    when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
    StringBuilder buf = new StringBuilder();
    buf.append("public class Foo {\n");
    buf.append("    void foo() {\n");
    buf.append("        String bar = \"astring\";");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit unit = defaultPackage.createCompilationUnit("Foo.java", buf.toString(), false, null);
    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 = server.codeAction(params).join();
    Assert.assertNotNull(quickfixActions);
    Assert.assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
    for (Either<Command, CodeAction> codeAction : quickfixActions) {
        Assert.assertNull("Should defer the edit property to the resolveCodeAction request", codeAction.getRight().getEdit());
        Assert.assertNotNull("Should preserve the data property for the unresolved code action", codeAction.getRight().getData());
    }
    Optional<Either<Command, CodeAction>> removeUnusedResponse = quickfixActions.stream().filter(codeAction -> {
        return "Remove 'bar' and all assignments".equals(codeAction.getRight().getTitle());
    }).findFirst();
    Assert.assertTrue("Should return the quickfix \"Remove 'bar' and all assignments\"", removeUnusedResponse.isPresent());
    CodeAction unresolvedCodeAction = removeUnusedResponse.get().getRight();
    CodeAction resolvedCodeAction = server.resolveCodeAction(unresolvedCodeAction).join();
    Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
    String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
    buf = new StringBuilder();
    buf.append("public class Foo {\n");
    buf.append("    void foo() {    }\n");
    buf.append("}\n");
    Assert.assertEquals(buf.toString(), actual);
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Arrays(java.util.Arrays) Mock(org.mockito.Mock) JavaCodeActionKind(org.eclipse.jdt.ls.core.internal.JavaCodeActionKind) RunWith(org.junit.runner.RunWith) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) IProject(org.eclipse.core.resources.IProject) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Position(org.eclipse.lsp4j.Position) LanguageServerWorkingCopyOwner(org.eclipse.jdt.ls.core.internal.LanguageServerWorkingCopyOwner) JavaClientConnection(org.eclipse.jdt.ls.core.internal.JavaClientConnection) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Before(org.junit.Before) CodeAction(org.eclipse.lsp4j.CodeAction) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) IJavaProject(org.eclipse.jdt.core.IJavaProject) WorkspaceHelper(org.eclipse.jdt.ls.core.internal.WorkspaceHelper) Assert.assertNotNull(org.junit.Assert.assertNotNull) JavaCore(org.eclipse.jdt.core.JavaCore) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) IType(org.eclipse.jdt.core.IType) List(java.util.List) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) Command(org.eclipse.lsp4j.Command) CodeActionUtil(org.eclipse.jdt.ls.core.internal.CodeActionUtil) Assert.assertFalse(org.junit.Assert.assertFalse) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Optional(java.util.Optional) TestOptions(org.eclipse.jdt.ls.core.internal.correction.TestOptions) IProblem(org.eclipse.jdt.core.compiler.IProblem) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) CodeAction(org.eclipse.lsp4j.CodeAction) Range(org.eclipse.lsp4j.Range) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Command(org.eclipse.lsp4j.Command) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Test(org.junit.Test) AbstractQuickFixTest(org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)

Example 85 with Command

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

the class ConvertMethodReferenceToLambaTest method testLambdaToMethodReference.

@Test
public void testLambdaToMethodReference() throws Exception {
    setIgnoredCommands("Assign statement to new field", "Extract to constant", "Extract to field", "Extract to local variable (replace all occurrences)", "Extract to local variable", "Introduce Parameter...", ActionMessages.GenerateConstructorsAction_ellipsisLabel, ActionMessages.GenerateConstructorsAction_label);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("import java.util.stream.Stream;\n");
    buf.append("public class E {\n");
    buf.append("    private Stream<String> asHex(Stream<Integer> stream) {\n");
    buf.append("        return stream.map(t -> Integer.toHexString(t));\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range range = new Range(new Position(4, 39), new Position(4, 39));
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, range);
    assertEquals(1, codeActions.size());
    Either<Command, CodeAction> codeAction = codeActions.get(0);
    CodeAction action = codeAction.getRight();
    assertEquals(CodeActionKind.QuickFix, action.getKind());
    assertEquals("Convert to method reference", action.getTitle());
    Command c = action.getCommand();
    assertEquals("java.apply.workspaceEdit", c.getCommand());
    assertNotNull(c.getArguments());
    assertEquals("Convert to method reference", c.getTitle());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Range(org.eclipse.lsp4j.Range) 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