Search in sources :

Example 11 with FileRename

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();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) FileRename(org.eclipse.lsp4j.FileRename) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) File(java.io.File) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 12 with FileRename

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));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) FileRename(org.eclipse.lsp4j.FileRename) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

FileRename (org.eclipse.lsp4j.FileRename)12 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)10 RenameFilesParams (org.eclipse.lsp4j.RenameFilesParams)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)8 Test (org.junit.Test)8 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)5 ArrayList (java.util.ArrayList)4 SubMonitor (org.eclipse.core.runtime.SubMonitor)4 CoreException (org.eclipse.core.runtime.CoreException)3 TextEdit (org.eclipse.lsp4j.TextEdit)3 IPath (org.eclipse.core.runtime.IPath)2 SourcePath (org.eclipse.jdt.ls.core.internal.commands.BuildPathCommand.SourcePath)2 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)2 File (java.io.File)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1