use of org.eclipse.lsp4j.TextDocumentIdentifier 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));
}
}
use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_allKindsOfActions.
@Test
public void testCodeAction_allKindsOfActions() 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)));
params.setContext(context);
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertFalse("No code actions were found", codeActions.isEmpty());
boolean hasQuickFix = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.QuickFix));
assertTrue("No quickfix actions were found", hasQuickFix);
boolean hasRefactor = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Refactor));
assertTrue("No refactor actions were found", hasRefactor);
boolean hasSource = codeActions.stream().anyMatch(codeAction -> codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
assertTrue("No source actions were found", hasSource);
List<String> baseKinds = codeActions.stream().map(codeAction -> getBaseKind(codeAction.getRight().getKind())).collect(Collectors.toList());
assertTrue("quickfix actions should be ahead of refactor actions", baseKinds.lastIndexOf(CodeActionKind.QuickFix) < baseKinds.indexOf(CodeActionKind.Refactor));
assertTrue("refactor actions should be ahead of source actions", baseKinds.lastIndexOf(CodeActionKind.Refactor) < baseKinds.indexOf(CodeActionKind.Source));
}
use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_sourceActionsOnly.
@Test
public void testCodeAction_sourceActionsOnly() throws Exception {
// @formatter:off
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n" + " void foo() {\n" + " }\n" + "}\n");
// @formatter:on
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "foo()");
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList(), Collections.singletonList(CodeActionKind.Source)));
List<Either<Command, CodeAction>> sourceActions = getCodeActions(params);
Assert.assertNotNull(sourceActions);
Assert.assertFalse("No source actions were found", sourceActions.isEmpty());
for (Either<Command, CodeAction> codeAction : sourceActions) {
Assert.assertTrue("Unexpected kind:" + codeAction.getRight().getKind(), codeAction.getRight().getKind().startsWith(CodeActionKind.Source));
}
}
use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_customFileFormattingOptions.
@Test
public void testCodeAction_customFileFormattingOptions() throws Exception {
when(clientPreferences.isWorkspaceConfigurationSupported()).thenReturn(true);
when(connection.configuration(Mockito.any())).thenReturn(Arrays.asList(4, true));
server.setClientConnection(connection);
JavaLanguageServerPlugin.getInstance().setProtocol(server);
IJavaProject javaProject = ProjectUtils.getJavaProject(project);
Map<String, String> projectOptions = javaProject.getOptions(false);
// Indent using Tabs
projectOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, JavaCore.TAB);
projectOptions.put(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "4");
javaProject.setOptions(projectOptions);
IPackageFragmentRoot sourceFolder = javaProject.getPackageFragmentRoot(project.getFolder("src"));
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
StringBuilder builder = new StringBuilder();
builder.append("package test1;\n");
builder.append("interface I {\n");
builder.append(" void method();\n");
builder.append("}\n");
builder.append("public class E {\n");
builder.append(" void bar(I i) {\n");
builder.append(" }\n");
builder.append(" void foo() {\n");
builder.append(" bar(() /*[*//*]*/-> {\n");
builder.append(" });\n");
builder.append(" }\n");
builder.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(cu)));
final Range range = CodeActionUtil.getRange(cu, "/*[*//*]*/");
params.setRange(range);
params.setContext(new CodeActionContext(Collections.emptyList(), Arrays.asList(CodeActionKind.Refactor)));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Optional<Either<Command, CodeAction>> found = codeActions.stream().filter((codeAction) -> {
return codeAction.isRight() && Objects.equals("Convert to anonymous class creation", codeAction.getRight().getTitle());
}).findAny();
Assert.assertTrue(found.isPresent());
Either<Command, CodeAction> codeAction = found.get();
Command c = codeAction.isLeft() ? codeAction.getLeft() : codeAction.getRight().getCommand();
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
Assert.assertNotNull(c.getArguments());
Assert.assertTrue(c.getArguments().get(0) instanceof WorkspaceEdit);
WorkspaceEdit edit = (WorkspaceEdit) c.getArguments().get(0);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(edit);
builder = new StringBuilder();
builder.append("package test1;\n");
builder.append("interface I {\n");
builder.append(" void method();\n");
builder.append("}\n");
builder.append("public class E {\n");
builder.append(" void bar(I i) {\n");
builder.append(" }\n");
builder.append(" void foo() {\n");
builder.append(" bar(new I() {\n");
builder.append(" @Override\n");
builder.append(" public void method() {\n");
builder.append(" }\n");
builder.append(" });\n");
builder.append(" }\n");
builder.append("}\n");
AbstractSourceTestCase.compareSource(builder.toString(), actual);
}
use of org.eclipse.lsp4j.TextDocumentIdentifier in project eclipse.jdt.ls by eclipse.
the class CodeActionHandlerTest method testCodeAction_ignoringOtherDiagnosticWithoutCode.
@Test
public void testCodeAction_ignoringOtherDiagnosticWithoutCode() throws Exception {
ICompilationUnit unit = getWorkingCopy("src/java/Foo.java", "import java.sql.*; \n" + "public class Foo {\n" + " void foo() {\n" + " }\n" + "}\n");
// unit.save(new NullProgressMonitor(), true);
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
final Range range = CodeActionUtil.getRange(unit, "java.sql");
params.setRange(range);
Diagnostic diagnosticWithoutCode = new Diagnostic(new Range(new Position(0, 0), new Position(0, 1)), "fake dignostic without code");
params.setContext(new CodeActionContext(Arrays.asList(diagnosticWithoutCode, getDiagnostic(Integer.toString(IProblem.UnusedImport), range))));
List<Either<Command, CodeAction>> codeActions = getCodeActions(params);
Assert.assertNotNull(codeActions);
Assert.assertTrue(codeActions.size() >= 3);
List<Either<Command, CodeAction>> quickAssistActions = findActions(codeActions, CodeActionKind.QuickFix);
Assert.assertNotNull(quickAssistActions);
Assert.assertTrue(quickAssistActions.size() >= 1);
List<Either<Command, CodeAction>> organizeImportActions = findActions(codeActions, CodeActionKind.SourceOrganizeImports);
Assert.assertNotNull(organizeImportActions);
Assert.assertEquals(1, organizeImportActions.size());
Command c = codeActions.get(0).getRight().getCommand();
Assert.assertEquals(CodeActionHandler.COMMAND_ID_APPLY_EDIT, c.getCommand());
}
Aggregations