Search in sources :

Example 51 with Command

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

the class CodeLensHandlerTest method testResolveImplementationsCodeLens.

@SuppressWarnings("unchecked")
@Test
public void testResolveImplementationsCodeLens() {
    String source = "src/java/IFoo.java";
    String payload = createCodeLensImplementationsRequest(source, 5, 17, 21);
    CodeLens lens = getParams(payload);
    Range range = lens.getRange();
    assertRange(5, 17, 21, range);
    CodeLens result = handler.resolve(lens, monitor);
    assertNotNull(result);
    // Check if command found
    Command command = result.getCommand();
    assertNotNull(command);
    assertEquals("2 implementations", command.getTitle());
    assertEquals("java.show.implementations", command.getCommand());
    // Check codelens args
    List<Object> args = command.getArguments();
    assertEquals(3, args.size());
    // Check we point to the Bar class
    String sourceUri = args.get(0).toString();
    assertTrue(sourceUri.endsWith("IFoo.java"));
    // CodeLens position
    Position p = (Position) args.get(1);
    assertEquals(5, p.getLine());
    assertEquals(17, p.getCharacter());
    // Reference location
    List<Location> locations = (List<Location>) args.get(2);
    assertEquals(2, locations.size());
    Location loc = locations.get(0);
    assertTrue(loc.getUri().endsWith("src/java/Foo2.java"));
    assertRange(5, 13, 17, loc.getRange());
}
Also used : CodeLens(org.eclipse.lsp4j.CodeLens) Command(org.eclipse.lsp4j.Command) Position(org.eclipse.lsp4j.Position) List(java.util.List) Range(org.eclipse.lsp4j.Range) Lsp4jAssertions.assertRange(org.eclipse.jdt.ls.core.internal.Lsp4jAssertions.assertRange) Location(org.eclipse.lsp4j.Location) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 52 with Command

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

the class AdvancedExtractTest method testMoveInstanceMethod.

@Test
public void testMoveInstanceMethod() throws Exception {
    when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    pack1.createCompilationUnit("Second.java", "package test1;\n" + "\n" + "public class Second {\n" + "    public void bar() {\n" + "    }\n" + "}", false, null);
    // @formatter:on
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    Second s;\n");
    buf.append("    public void print() {\n");
    buf.append("        /*[*//*]*/s.bar();\n");
    buf.append("    }\n");
    buf.append("}");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> moveAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.REFACTOR_MOVE);
    Assert.assertNotNull(moveAction);
    Command moveCommand = CodeActionHandlerTest.getCommand(moveAction);
    Assert.assertNotNull(moveCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, moveCommand.getCommand());
    Assert.assertNotNull(moveCommand.getArguments());
    Assert.assertEquals(3, moveCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.MOVE_INSTANCE_METHOD_COMMAND, moveCommand.getArguments().get(0));
    Assert.assertTrue(moveCommand.getArguments().get(2) instanceof RefactorProposalUtility.MoveMemberInfo);
    Assert.assertEquals("print()", ((RefactorProposalUtility.MoveMemberInfo) moveCommand.getArguments().get(2)).displayName);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorProposalUtility(org.eclipse.jdt.ls.core.internal.text.correction.RefactorProposalUtility) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest)

Example 53 with Command

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

the class AdvancedExtractTest method testExtractMethod.

@Test
public void testExtractMethod() throws Exception {
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    public int foo(boolean b1, boolean b2) {\n");
    buf.append("        int n = 0;\n");
    buf.append("        int i = 0;\n");
    buf.append("        /*[*/\n");
    buf.append("        if (b1)\n");
    buf.append("            i = 1;\n");
    buf.append("        if (b2)\n");
    buf.append("            n = n + i;\n");
    buf.append("        /*]*/\n");
    buf.append("        return n;\n");
    buf.append("    }\n");
    buf.append("}\n");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> extractMethodAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.REFACTOR_EXTRACT_METHOD);
    Assert.assertNotNull(extractMethodAction);
    Command extractMethodCommand = CodeActionHandlerTest.getCommand(extractMethodAction);
    Assert.assertNotNull(extractMethodCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, extractMethodCommand.getCommand());
    Assert.assertNotNull(extractMethodCommand.getArguments());
    Assert.assertEquals(2, extractMethodCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.EXTRACT_METHOD_COMMAND, extractMethodCommand.getArguments().get(0));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) 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) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest)

Example 54 with Command

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

the class AdvancedExtractTest method testMoveInnerType.

@Test
public void testMoveInnerType() throws Exception {
    when(preferenceManager.getClientPreferences().isMoveRefactoringSupported()).thenReturn(true);
    IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
    StringBuilder buf = new StringBuilder();
    buf.append("package test1;\n");
    buf.append("\n");
    buf.append("public class E {\n");
    buf.append("    class Inner {\n");
    buf.append("        /*[*//*]*/\n");
    buf.append("    }\n");
    buf.append("}");
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
    Range selection = getRange(cu, null);
    List<Either<Command, CodeAction>> codeActions = evaluateCodeActions(cu, selection);
    Assert.assertNotNull(codeActions);
    Either<Command, CodeAction> moveAction = CodeActionHandlerTest.findAction(codeActions, JavaCodeActionKind.REFACTOR_MOVE);
    Assert.assertNotNull(moveAction);
    Command moveCommand = CodeActionHandlerTest.getCommand(moveAction);
    Assert.assertNotNull(moveCommand);
    Assert.assertEquals(RefactorProposalUtility.APPLY_REFACTORING_COMMAND_ID, moveCommand.getCommand());
    Assert.assertNotNull(moveCommand.getArguments());
    Assert.assertEquals(3, moveCommand.getArguments().size());
    Assert.assertEquals(RefactorProposalUtility.MOVE_TYPE_COMMAND, moveCommand.getArguments().get(0));
    Assert.assertTrue(moveCommand.getArguments().get(2) instanceof RefactorProposalUtility.MoveTypeInfo);
    Assert.assertEquals(fJProject1.getProject().getName(), ((RefactorProposalUtility.MoveTypeInfo) moveCommand.getArguments().get(2)).projectName);
    Assert.assertEquals("Inner", ((RefactorProposalUtility.MoveTypeInfo) moveCommand.getArguments().get(2)).displayName);
    Assert.assertEquals("test1.E", ((RefactorProposalUtility.MoveTypeInfo) moveCommand.getArguments().get(2)).enclosingTypeName);
    Assert.assertTrue(((RefactorProposalUtility.MoveTypeInfo) moveCommand.getArguments().get(2)).isMoveAvaiable());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Command(org.eclipse.lsp4j.Command) CodeAction(org.eclipse.lsp4j.CodeAction) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorProposalUtility(org.eclipse.jdt.ls.core.internal.text.correction.RefactorProposalUtility) Range(org.eclipse.lsp4j.Range) Test(org.junit.Test) AbstractSelectionTest(org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest) CodeActionHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.CodeActionHandlerTest)

Example 55 with Command

use of org.eclipse.lsp4j.Command in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method createMXMLNamespaceCommand.

private Command createMXMLNamespaceCommand(IDefinition definition, String prefix, String uri) {
    Command xmlnsCommand = new Command();
    xmlnsCommand.setTitle("Add Namespace " + uri);
    xmlnsCommand.setCommand(COMMAND_XMLNS);
    xmlnsCommand.setArguments(Arrays.asList(prefix, uri, namespaceStartIndex, namespaceEndIndex));
    return xmlnsCommand;
}
Also used : Command(org.eclipse.lsp4j.Command)

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