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());
}
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);
}
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);
}
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);
}
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());
}
Aggregations