use of org.eclipse.lsp4j.RenameFile in project eclipse.jdt.ls by eclipse.
the class ChangeUtilTest method testConvertRenameCompilationUnitChange.
// Resource Changes
@Test
public void testConvertRenameCompilationUnitChange() throws CoreException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
String newName = "ENew.java";
RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newName);
String oldUri = JDTUtils.toURI(cu);
String newUri = ResourceUtils.fixURI(URI.create(oldUri).resolve(newName));
WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
assertEquals(edit.getDocumentChanges().size(), 1);
ResourceOperation resourceOperation = edit.getDocumentChanges().get(0).getRight();
assertTrue(resourceOperation instanceof RenameFile);
assertEquals(((RenameFile) resourceOperation).getOldUri(), oldUri);
assertEquals(((RenameFile) resourceOperation).getNewUri(), newUri);
}
use of org.eclipse.lsp4j.RenameFile in project eclipse.jdt.ls by eclipse.
the class RenameHandlerTest method testRenameTypeWithResourceChanges.
@Test
public void testRenameTypeWithResourceChanges() throws JavaModelException, BadLocationException {
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
String[] codes = { "package test1;\n", "public class E|* {\n", " public E() {\n", " }\n", " public int bar() {\n", " }\n", " public int foo() {\n", " this.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, "Newname");
assertNotNull(edit);
List<Either<TextDocumentEdit, ResourceOperation>> resourceChanges = edit.getDocumentChanges();
assertEquals(resourceChanges.size(), 2);
Either<TextDocumentEdit, ResourceOperation> change = resourceChanges.get(1);
RenameFile resourceChange = (RenameFile) change.getRight();
assertEquals(JDTUtils.toURI(cu), resourceChange.getOldUri());
assertEquals(JDTUtils.toURI(cu).replaceFirst("(?s)E(?!.*?E)", "Newname"), resourceChange.getNewUri());
List<TextEdit> testChanges = new LinkedList<>();
testChanges.addAll(resourceChanges.get(0).getLeft().getEdits());
String expected = "package test1;\n" + "public class Newname {\n" + " public Newname() {\n" + " }\n" + " public int bar() {\n" + " }\n" + " public int foo() {\n" + " this.bar();\n" + " }\n" + "}\n";
assertEquals(expected, TextEditUtil.apply(builder.toString(), testChanges));
}
use of org.eclipse.lsp4j.RenameFile 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.RenameFile in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveMultiFiles.
@Test
public void testMoveMultiFiles() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
// @formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}", true, null);
// @formatter:on
// @formatter:off
ICompilationUnit unitB = pack1.createCompilationUnit("B.java", "package jdtls.test1;\r\n" + "\r\n" + "public class B {\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
String packageUri = JDTUtils.getFileURI(pack2.getResource());
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveResource", new String[] { JDTUtils.toURI(unitA), JDTUtils.toURI(unitB) }, packageUri, true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(4, changes.size());
// @formatter:off
String expected = "package jdtls.test2;\r\n" + "\r\n" + "public class B {\r\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));
RenameFile renameFileB = (RenameFile) changes.get(1).getRight();
assertNotNull(renameFileB);
assertEquals(JDTUtils.toURI(unitB), renameFileB.getOldUri());
assertEquals(ResourceUtils.fixURI(unitB.getResource().getRawLocationURI()).replace("test1", "test2"), renameFileB.getNewUri());
// @formatter:off
expected = "package jdtls.test2;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}";
// @formatter:on
textEdit = changes.get(2).getLeft();
assertNotNull(textEdit);
edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitA.getSource(), edits));
RenameFile renameFileA = (RenameFile) changes.get(3).getRight();
assertNotNull(renameFileA);
assertEquals(JDTUtils.toURI(unitA), renameFileA.getOldUri());
assertEquals(ResourceUtils.fixURI(unitA.getResource().getRawLocationURI()).replace("test1", "test2"), renameFileA.getNewUri());
}
use of org.eclipse.lsp4j.RenameFile in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveFile.
@Test
public void testMoveFile() throws JavaModelException, BadLocationException {
IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", false, null);
// @formatter:off
ICompilationUnit unitA = pack1.createCompilationUnit("A.java", "package jdtls.test1;\r\n" + "import jdtls.test2.B;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
// @formatter:off
ICompilationUnit unitB = pack2.createCompilationUnit("B.java", "package jdtls.test2;\r\n" + "\r\n" + "public class B {\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack3 = sourceFolder.createPackageFragment("jdtls.test3", false, null);
String packageUri = JDTUtils.getFileURI(pack3.getResource());
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveResource", new String[] { JDTUtils.toURI(unitB) }, packageUri, true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(3, changes.size());
// @formatter:off
String expected = "package jdtls.test1;\r\n" + "import jdtls.test3.B;\r\n" + "\r\n" + "public class A {\r\n" + " private B b = new B();\r\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(unitA.getSource(), textEdit.getEdits()));
// @formatter:off
expected = "package jdtls.test3;\r\n" + "\r\n" + "public class B {\r\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));
RenameFile renameFile = (RenameFile) changes.get(2).getRight();
assertNotNull(renameFile);
assertEquals(JDTUtils.toURI(unitB), renameFile.getOldUri());
assertEquals(ResourceUtils.fixURI(unitB.getResource().getRawLocationURI()).replace("test2", "test3"), renameFile.getNewUri());
}
Aggregations