Search in sources :

Example 31 with Command

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

the class GenerateAccessorsActionTest method testGenerateAccessorsForRecordEnabled.

@Test
public void testGenerateAccessorsForRecordEnabled() throws Exception {
    importProjects("eclipse/java16");
    IProject project = WorkspaceHelper.getProject("java16");
    IJavaProject javaProject = JavaCore.create(project);
    IPackageFragmentRoot root = javaProject.findPackageFragmentRoot(javaProject.getPath().append("src").append("main").append("java"));
    assertNotNull(root);
    IPackageFragment packageFragment = root.createPackageFragment("p", true, null);
    // @formatter:off
    ICompilationUnit unit = packageFragment.createCompilationUnit("A.java", "package p;\r\n" + "\r\n" + "public record A(String name, int age) {\r\n" + "}", true, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "A");
    List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> generateAccessorsAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS);
    Assert.assertNotNull(generateAccessorsAction);
    Command generateAccessorsCommand = CodeActionHandlerTest.getCommand(generateAccessorsAction);
    Assert.assertNotNull(generateAccessorsCommand);
    Assert.assertEquals(SourceAssistProcessor.COMMAND_ID_ACTION_GENERATEACCESSORSPROMPT, generateAccessorsCommand.getCommand());
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJavaProject(org.eclipse.jdt.core.IJavaProject) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) IProject(org.eclipse.core.resources.IProject) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Test(org.junit.Test)

Example 32 with Command

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

the class GenerateAccessorsActionTest method testAdvancedGenerateAccessorsEnabled.

@Test
public void testAdvancedGenerateAccessorsEnabled() throws JavaModelException {
    // @formatter:off
    ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + "\r\n" + "public class A {\r\n" + "	static String name;\r\n" + "	String address;\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> generateAccessorsAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS);
    Assert.assertNotNull(generateAccessorsAction);
    Command generateAccessorsCommand = CodeActionHandlerTest.getCommand(generateAccessorsAction);
    Assert.assertNotNull(generateAccessorsCommand);
    Assert.assertEquals(SourceAssistProcessor.COMMAND_ID_ACTION_GENERATEACCESSORSPROMPT, generateAccessorsCommand.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 33 with Command

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

the class GenerateConstructorsActionTest method testGenerateConstructorsQuickAssist.

@Test
public void testGenerateConstructorsQuickAssist() 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
    // test for field declaration
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String name");
    List<Either<Command, CodeAction>> codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> constructorAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.QUICK_ASSIST);
    Assert.assertNotNull(constructorAction);
    Command constructorCommand = CodeActionHandlerTest.getCommand(constructorAction);
    Assert.assertNotNull(constructorCommand);
    Assert.assertEquals(SourceAssistProcessor.COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, constructorCommand.getCommand());
    // test for type declaration
    params = CodeActionUtil.constructCodeActionParams(unit, "A");
    codeActions = server.codeAction(params).join();
    Assert.assertNotNull(codeActions);
    List<Either<Command, CodeAction>> quickAssistActions = CodeActionHandlerTest.findActions(codeActions, JavaCodeActionKind.QUICK_ASSIST);
    Assert.assertTrue(CodeActionHandlerTest.commandExists(quickAssistActions, SourceAssistProcessor.COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT));
}
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 34 with Command

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

the class GenerateConstructorsActionTest method testGenerateConstructorsEnabled.

@Test
public void testGenerateConstructorsEnabled() 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> constructorAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS);
    Assert.assertNotNull(constructorAction);
    Command constructorCommand = CodeActionHandlerTest.getCommand(constructorAction);
    Assert.assertNotNull(constructorCommand);
    Assert.assertEquals(SourceAssistProcessor.COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, constructorCommand.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 35 with Command

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

the class GenerateToStringActionTest method testGenerateToStringEnabled_emptyFields.

@Test
public void testGenerateToStringEnabled_emptyFields() throws JavaModelException {
    // @formatter:off
    ICompilationUnit unit = fPackageP.createCompilationUnit("A.java", "package p;\r\n" + "\r\n" + "public class A {\r\n" + "	private static 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> toStringAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.SOURCE_GENERATE_TO_STRING);
    Assert.assertNotNull(toStringAction);
    Command toStringCommand = CodeActionHandlerTest.getCommand(toStringAction);
    Assert.assertNotNull(toStringCommand);
    Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, toStringCommand.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)

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