use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameSuperMethod.
@Test
public void testRenameSuperMethod() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes = { "package test1;\n", "class A {\n", " public void bar() {\n", " }\n", "}\n", "class B extends A {\n", " public void bar() {\n", " super|*.bar();\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, "TypeA");
assertNotNull(edit);
assertEquals(1, edit.getChanges().size());
assertEquals(TextEditUtil.apply(builder.toString(), edit.getChanges().get(JDTUtils.toURI(cu))), "package test1;\n" + "class TypeA {\n" + " public void bar() {\n" + " }\n" + "}\n" + "class B extends TypeA {\n" + " public void bar() {\n" + " super.bar();\n" + " }\n" + "}\n");
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameMultipleFiles.
@Test
public void testRenameMultipleFiles() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes1 = { "package test1;\n", "public class A {\n", " public void foo() {\n", " }\n", "}\n" };
String[] codes2 = { "package test1;\n", "public class B {\n", " public void foo() {\n", " A a = new A();\n", " a.foo|*();\n", " }\n", "}\n" };
StringBuilder builderA = new StringBuilder();
mergeCode(builderA, codes1);
ICompilationUnit cuA = pack1.createCompilationUnit("A.java", builderA.toString(), false, null);
StringBuilder builderB = new StringBuilder();
Position pos = mergeCode(builderB, codes2);
ICompilationUnit cuB = pack1.createCompilationUnit("B.java", builderB.toString(), false, null);
WorkspaceEdit edit = getRenameEdit(cuB, pos, "newname");
assertNotNull(edit);
assertEquals(edit.getChanges().size(), 2);
assertEquals(TextEditUtil.apply(builderA.toString(), edit.getChanges().get(JDTUtils.toURI(cuA))), "package test1;\n" + "public class A {\n" + " public void newname() {\n" + " }\n" + "}\n");
assertEquals(TextEditUtil.apply(builderB.toString(), edit.getChanges().get(JDTUtils.toURI(cuB))), "package test1;\n" + "public class B {\n" + " public void foo() {\n" + " A a = new A();\n" + " a.newname();\n" + " }\n" + "}\n");
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenamePackage.
@Test
public void testRenamePackage() throws JavaModelException, BadLocationException {
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.test2", false, null);
String[] codes1 = { "package test1;\n", "import parent.test2.B;\n", "public class A {\n", " public void foo(){\n", " B b = new B();\n", " b.foo();\n", " }\n", "}\n" };
String[] codes2 = { "package parent.test2|*;\n", "public class B {\n", " public B() {}\n", " public void foo() {}\n", "}\n" };
StringBuilder builderA = new StringBuilder();
mergeCode(builderA, codes1);
ICompilationUnit cuA = pack1.createCompilationUnit("A.java", builderA.toString(), false, null);
StringBuilder builderB = new StringBuilder();
Position pos = mergeCode(builderB, codes2);
ICompilationUnit cuB = pack2.createCompilationUnit("B.java", builderB.toString(), false, null);
WorkspaceEdit edit = getRenameEdit(cuB, pos, "parent.newpackage");
assertNotNull(edit);
List<Either<TextDocumentEdit, ResourceOperation>> resourceChanges = edit.getDocumentChanges();
assertEquals(5, resourceChanges.size());
List<TextEdit> testChangesA = new LinkedList<>();
testChangesA.addAll(resourceChanges.get(0).getLeft().getEdits());
List<TextEdit> testChangesB = new LinkedList<>();
testChangesB.addAll(resourceChanges.get(1).getLeft().getEdits());
String expectedA = "package test1;\n" + "import parent.newpackage.B;\n" + "public class A {\n" + " public void foo(){\n" + " B b = new B();\n" + " b.foo();\n" + " }\n" + "}\n";
String expectedB = "package parent.newpackage;\n" + "public class B {\n" + " public B() {}\n" + " public void foo() {}\n" + "}\n";
assertEquals(expectedA, TextEditUtil.apply(builderA.toString(), testChangesA));
assertEquals(expectedB, TextEditUtil.apply(builderB.toString(), testChangesB));
// moved package
CreateFile resourceChange = (CreateFile) resourceChanges.get(2).getRight();
assertEquals(ResourceUtils.fixURI(pack2.getResource().getRawLocationURI()).replaceFirst("test2[/]?", "newpackage/.temp"), resourceChange.getUri());
// moved class B
RenameFile resourceChange2 = (RenameFile) resourceChanges.get(3).getRight();
assertEquals(ResourceUtils.fixURI(cuB.getResource().getRawLocationURI()), resourceChange2.getOldUri());
assertEquals(ResourceUtils.fixURI(cuB.getResource().getRawLocationURI()).replace("test2", "newpackage"), resourceChange2.getNewUri());
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameJavadoc.
@Test
public void testRenameJavadoc() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes = { "package test1;\n", "public class E {\n", " /**\n", " *@param i int\n", " */\n", " public int foo(int i|*) {\n", " E e = new E();\n", " e.foo();\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, "i2");
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" + " /**\n" + " *@param i2 int\n" + " */\n" + " public int foo(int i2) {\n" + " E e = new E();\n" + " e.foo();\n" + " }\n" + "}\n");
}
use of org.eclipse.lsp4j.Position in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameInterfaceMethod.
@Test
public void testRenameInterfaceMethod() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes1 = { "package test1;\n", "public interface A {\n", " public void foo();\n", "}\n" };
String[] codes2 = { "package test1;\n", "public class B implements A {\n", " @Override\n", " public void foo|*() {\n", " }\n", "}\n" };
StringBuilder builderA = new StringBuilder();
mergeCode(builderA, codes1);
ICompilationUnit cuA = pack1.createCompilationUnit("A.java", builderA.toString(), false, null);
StringBuilder builderB = new StringBuilder();
Position pos = mergeCode(builderB, codes2);
ICompilationUnit cuB = pack1.createCompilationUnit("B.java", builderB.toString(), false, null);
WorkspaceEdit edit = getRenameEdit(cuB, pos, "newname");
assertNotNull(edit);
assertEquals(edit.getChanges().size(), 2);
assertEquals(TextEditUtil.apply(builderA.toString(), edit.getChanges().get(JDTUtils.toURI(cuA))), "package test1;\n" + "public interface A {\n" + " public void newname();\n" + "}\n");
assertEquals(TextEditUtil.apply(builderB.toString(), edit.getChanges().get(JDTUtils.toURI(cuB))), "package test1;\n" + "public class B implements A {\n" + " @Override\n" + " public void newname() {\n" + " }\n" + "}\n");
}
Aggregations