Search in sources :

Example 6 with RenameFilesParams

use of org.eclipse.lsp4j.RenameFilesParams in project eclipse.jdt.ls by eclipse.

the class FileEventHandlerTest method testRenamePackage.

// Test renaming package from "parent.pack2" to "parent.newpack2"
@Test
public void testRenamePackage() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("parent.pack1", false, null);
    IPackageFragment pack2 = sourceFolder.createPackageFragment("parent.pack2", false, null);
    StringBuilder codeA = new StringBuilder();
    codeA.append("package parent.pack1;\n");
    codeA.append("import parent.pack2.B;\n");
    codeA.append("public class A {\n");
    codeA.append("	public void foo() {\n");
    codeA.append("		B b = new B();\n");
    codeA.append("		b.foo();\n");
    codeA.append("	}\n");
    codeA.append("}\n");
    StringBuilder codeB = new StringBuilder();
    codeB.append("package parent.pack2;\n");
    codeB.append("public class B {\n");
    codeB.append("	public B() {}\n");
    codeB.append("	public void foo() {}\n");
    codeB.append("}\n");
    ICompilationUnit cuA = pack1.createCompilationUnit("A.java", codeA.toString(), false, null);
    ICompilationUnit cuB = pack2.createCompilationUnit("B.java", codeB.toString(), false, null);
    String pack2Uri = JDTUtils.getFileURI(pack2.getResource());
    String newPack2Uri = pack2Uri.replace("pack2", "newpack2");
    WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(pack2Uri, newPack2Uri))), new NullProgressMonitor());
    assertNotNull(edit);
    List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
    assertEquals(2, documentChanges.size());
    assertTrue(documentChanges.get(0).isLeft());
    assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
    assertEquals(TextEditUtil.apply(codeA.toString(), documentChanges.get(0).getLeft().getEdits()), "package parent.pack1;\n" + "import parent.newpack2.B;\n" + "public class A {\n" + "	public void foo() {\n" + "		B b = new B();\n" + "		b.foo();\n" + "	}\n" + "}\n");
    assertTrue(documentChanges.get(1).isLeft());
    assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
    assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(1).getLeft().getEdits()), "package parent.newpack2;\n" + "public class B {\n" + "	public B() {}\n" + "	public void foo() {}\n" + "}\n");
}
Also used : FileRename(org.eclipse.lsp4j.FileRename) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 7 with RenameFilesParams

use of org.eclipse.lsp4j.RenameFilesParams 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 8 with RenameFilesParams

use of org.eclipse.lsp4j.RenameFilesParams 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

NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)8 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)8 FileRename (org.eclipse.lsp4j.FileRename)8 RenameFilesParams (org.eclipse.lsp4j.RenameFilesParams)8 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)8 Test (org.junit.Test)8 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)5 ArrayList (java.util.ArrayList)3 TextEdit (org.eclipse.lsp4j.TextEdit)3 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)2 File (java.io.File)1