use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class CodeActionResolveHandlerTest method testResolveCodeAction_AnnotationQuickFixes.
// See https://github.com/redhat-developer/vscode-java/issues/1992
@Test
public void testResolveCodeAction_AnnotationQuickFixes() throws Exception {
when(preferenceManager.getClientPreferences().isResolveCodeActionSupported()).thenReturn(true);
importProjects("maven/salut4");
IProject proj = WorkspaceHelper.getProject("salut4");
IJavaProject javaProject = JavaCore.create(proj);
assertTrue(javaProject.exists());
IType type = javaProject.findType("org.sample.MyTest");
ICompilationUnit unit = type.getCompilationUnit();
assertFalse(unit.getSource().contains("import org.junit.jupiter.api.Test"));
CodeActionParams params = new CodeActionParams();
params.setTextDocument(new TextDocumentIdentifier(JDTUtils.toURI(unit)));
Position position = new Position(3, 4);
final Range range = new Range(position, position);
params.setRange(range);
CodeActionContext context = new CodeActionContext(Arrays.asList(getDiagnostic(Integer.toString(IProblem.UndefinedType), range)), Collections.singletonList(CodeActionKind.QuickFix));
params.setContext(context);
List<Either<Command, CodeAction>> quickfixActions = server.codeAction(params).join();
assertNotNull(quickfixActions);
assertFalse("No quickfix actions were found", quickfixActions.isEmpty());
Optional<Either<Command, CodeAction>> importTest = quickfixActions.stream().filter(codeAction -> {
return "Import 'Test' (org.junit.jupiter.api)".equals(codeAction.getRight().getTitle());
}).findFirst();
CodeAction codeAction = importTest.get().getRight();
assertEquals(1, codeAction.getDiagnostics().size());
CodeAction resolvedCodeAction = server.resolveCodeAction(codeAction).join();
Assert.assertNotNull("Should resolve the edit property in the resolveCodeAction request", resolvedCodeAction.getEdit());
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(resolvedCodeAction.getEdit());
assertTrue(actual.contains("import org.junit.jupiter.api.Test"));
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class ShowAllQuickFixTest method testShowAt.
@Test
public void testShowAt() throws Exception {
String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
try {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("import java.math.BigDecimal;\n");
buf.append("public class F {\n");
buf.append(" private int i; private BigDecimal b;private void test() {}\n");
buf.append(" public static void main(String[] args) {\n");
buf.append(" System.out.println(greeting());\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
cu.becomeWorkingCopy(null);
CodeActionParams codeActionParams = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
codeActionParams.setTextDocument(textDocument);
codeActionParams.setRange(new Range(new Position(3, 9), new Position(3, 9)));
CodeActionContext context = new CodeActionContext();
context.setDiagnostics(Collections.emptyList());
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
codeActionParams.setContext(context);
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(0, codeActions.size());
codeActionParams.setRange(new Range(new Position(3, 4), new Position(3, 40)));
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(0, codeActions.size());
codeActionParams.setRange(new Range(new Position(5, 1), new Position(5, 1)));
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(0, codeActions.size());
preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
codeActionParams.setRange(new Range(new Position(3, 9), new Position(3, 9)));
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
cu.makeConsistent(null);
List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 4);
context.setDiagnostics(diagnostics);
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(3, codeActions.size());
codeActionParams.setRange(new Range(new Position(3, 4), new Position(3, 40)));
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(3, codeActions.size());
codeActionParams.setRange(new Range(new Position(5, 1), new Position(5, 1)));
diagnostics = getDiagnostics(cu, astRoot, 6);
context.setDiagnostics(diagnostics);
codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(1, codeActions.size());
CodeAction greeting = codeActions.get(0).getRight();
assertNotNull(greeting);
assertEquals("Create method 'greeting()'", greeting.getTitle());
} finally {
preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
}
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class ShowAllQuickFixTest method testDuplicateQuickFix.
// https://github.com/redhat-developer/vscode-java/issues/2236
@Test
public void testDuplicateQuickFix() throws Exception {
String showQuickFixes = preferenceManager.getPreferences().getJavaQuickFixShowAt();
try {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("public class F {\n");
buf.append(" List list = List.of();\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("F.java", buf.toString(), true, null);
cu.becomeWorkingCopy(null);
preferenceManager.getPreferences().setJavaQuickFixShowAt(Preferences.LINE);
CodeActionParams codeActionParams = new CodeActionParams();
TextDocumentIdentifier textDocument = new TextDocumentIdentifier();
textDocument.setUri(JDTUtils.toURI(cu));
codeActionParams.setTextDocument(textDocument);
codeActionParams.setRange(new Range(new Position(2, 6), new Position(2, 6)));
CodeActionContext context = new CodeActionContext();
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(cu, CoreASTProvider.WAIT_YES, null);
List<Diagnostic> diagnostics = getDiagnostics(cu, astRoot, 3);
context.setDiagnostics(diagnostics);
context.setOnly(Arrays.asList(CodeActionKind.QuickFix));
codeActionParams.setContext(context);
List<Either<Command, CodeAction>> codeActions = new CodeActionHandler(this.preferenceManager).getCodeActionCommands(codeActionParams, new NullProgressMonitor());
assertEquals(5, codeActions.size());
CodeAction action = codeActions.get(0).getRight();
assertNotNull(action);
assertEquals("Import 'List' (java.util)", action.getTitle());
action = codeActions.get(1).getRight();
assertNotNull(action);
assertNotEquals("Import 'List' (java.util)", action.getTitle());
} finally {
preferenceManager.getPreferences().setJavaQuickFixShowAt(showQuickFixes);
}
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameField.
@Test
public void testRenameField() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes = { "package test1;\n", "public class E {\n", " private int myValue = 2;\n", " public void bar() {\n", " myValue|* = 3;\n", " }\n", "}\n" };
StringBuilder builder = new StringBuilder();
Position pos = mergeCode(builder, codes);
ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
WorkspaceEdit edit = getRenameEdit(cu, pos, "newname");
assertNotNull(edit);
assertEquals(edit.getChanges().size(), 1);
assertEquals(TextEditUtil.apply(builder.toString(), edit.getChanges().get(JDTUtils.toURI(cu))), "package test1;\n" + "public class E {\n" + " private int newname = 2;\n" + " public void bar() {\n" + " newname = 3;\n" + " }\n" + "}\n");
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameSystemLibrary.
@Test(expected = ResponseErrorException.class)
public void testRenameSystemLibrary() throws JavaModelException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes = { "package test1;\n", "public class E {\n", " public int bar() {\n", " String str = new String();\n", " str.len|*gth();\n", " }\n", "}\n" };
StringBuilder builder = new StringBuilder();
Position pos = mergeCode(builder, codes);
ICompilationUnit cu = pack1.createCompilationUnit("E.java", builder.toString(), false, null);
getRenameEdit(cu, pos, "newname");
}
Aggregations