use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandlerTest method testMoveNonClasspathFile.
@Test
public void testMoveNonClasspathFile() throws Exception {
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
IPackageFragment pack1 = sourceFolder.createPackageFragment("jdtls.test1", true, null);
File projectRoot = javaProject.getProject().getLocation().toFile();
File file = new File(projectRoot, "Bar.java");
file.createNewFile();
String contents = "public class Bar {\r\n}";
FileUtils.writeStringToFile(file, contents);
ICompilationUnit bar = JDTUtils.resolveCompilationUnit(file.toURI());
bar.getResource().refreshLocal(IResource.DEPTH_ONE, null);
String uri = JDTUtils.toURI(bar);
String newUri = uri.replace("Bar.java", "src/jdtls/test1/Bar.java");
WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(uri, newUri))), new NullProgressMonitor());
assertNotNull(edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = edit.getDocumentChanges();
assertEquals(1, changes.size());
// @formatter:off
String expected = "package jdtls.test1;\r\n" + "public class Bar {\r\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
bar.becomeWorkingCopy(null);
assertEquals(expected, TextEditUtil.apply(bar.getSource(), edits));
bar.discardWorkingCopy();
}
use of org.eclipse.lsp4j.FileRename in project eclipse.jdt.ls by eclipse.
the class FileEventHandlerTest method testMoveMultiFiles.
@Test
public void testMoveMultiFiles() throws JavaModelException, BadLocationException {
when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
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
// @formatter:off
ICompilationUnit unitC = pack1.createCompilationUnit("C.java", "package jdtls.test1;\r\n" + "\r\n" + "public class C {\r\n" + " private B b = new B();\r\n" + "}", true, null);
// @formatter:on
IPackageFragment pack2 = sourceFolder.createPackageFragment("jdtls.test2", false, null);
String uriA = JDTUtils.toURI(unitA);
String uriB = JDTUtils.toURI(unitB);
String newUriA = uriA.replace("test1", "test2");
String newUriB = uriB.replace("test1", "test2");
WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(uriA, newUriA), new FileRename(uriB, newUriB))), new NullProgressMonitor());
assertNotNull(edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = edit.getDocumentChanges();
assertEquals(3, changes.size());
// @formatter:off
String expected = "package jdtls.test1;\r\n" + "\r\n" + "import jdtls.test2.B;\r\n" + "\r\n" + "public class C {\r\n" + " private B b = new B();\r\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
List<TextEdit> edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitC.getSource(), edits));
// @formatter:off
expected = "package jdtls.test2;\r\n" + "\r\n" + "public class B {\r\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
edits = new ArrayList<>(textEdit.getEdits());
assertEquals(expected, TextEditUtil.apply(unitB.getSource(), edits));
// @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));
}
Aggregations