Search in sources :

Example 1 with FileRename

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

the class FileEventHandlerTest method testRenameSubPackage.

@Test
public void testRenameSubPackage() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment parentPack = sourceFolder.createPackageFragment("parent", false, null);
    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 parentPackUri = JDTUtils.getFileURI(parentPack.getResource());
    String newParentPackUri = parentPackUri.replace("parent", "newparent");
    WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(parentPackUri, newParentPackUri))), new NullProgressMonitor());
    assertNotNull(edit);
    List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
    assertEquals(3, documentChanges.size());
    assertTrue(documentChanges.get(0).isLeft());
    assertEquals(documentChanges.get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
    assertTrue(documentChanges.get(1).isLeft());
    assertEquals(documentChanges.get(1).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuA));
    List<TextEdit> edits = new ArrayList<>();
    edits.addAll(documentChanges.get(0).getLeft().getEdits());
    edits.addAll(documentChanges.get(1).getLeft().getEdits());
    assertEquals(TextEditUtil.apply(codeA.toString(), edits), "package newparent.pack1;\n" + "import newparent.pack2.B;\n" + "public class A {\n" + "	public void foo() {\n" + "		B b = new B();\n" + "		b.foo();\n" + "	}\n" + "}\n");
    assertTrue(documentChanges.get(2).isLeft());
    assertEquals(documentChanges.get(2).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
    assertEquals(TextEditUtil.apply(codeB.toString(), documentChanges.get(2).getLeft().getEdits()), "package newparent.pack2;\n" + "public class B {\n" + "	public B() {}\n" + "	public void foo() {}\n" + "}\n");
}
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) 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)

Example 2 with FileRename

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

the class FileEventHandlerTest method testMoveMultiFiles_differentDestination.

@Test
public void testMoveMultiFiles_differentDestination() 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);
    IPackageFragment pack3 = sourceFolder.createPackageFragment("jdtls.test3", false, null);
    String uriA = JDTUtils.toURI(unitA);
    String uriB = JDTUtils.toURI(unitB);
    String newUriA = uriA.replace("test1", "test2");
    String newUriB = uriB.replace("test1", "test3");
    WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(uriA, newUriA), new FileRename(uriB, newUriB))), new NullProgressMonitor());
    assertNull(edit);
}
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) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 3 with FileRename

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

the class FileEventHandlerTest method testRenameFiles.

@Test
public void testRenameFiles() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    StringBuilder builderA = new StringBuilder();
    builderA.append("package test1;\n");
    builderA.append("public class ObjectA {\n");
    builderA.append("	public void foo() {\n");
    builderA.append("	}\n");
    builderA.append("}\n");
    ICompilationUnit cuA = pack1.createCompilationUnit("ObjectA.java", builderA.toString(), false, null);
    StringBuilder builderB = new StringBuilder();
    builderB.append("package test1;\n");
    builderB.append("public class B {\n");
    builderB.append("	public void foo() {\n");
    builderB.append("		ObjectA a = new ObjectA();\n");
    builderB.append("		a.foo();\n");
    builderB.append("	}\n");
    builderB.append("}\n");
    ICompilationUnit cuB = pack1.createCompilationUnit("B.java", builderB.toString(), false, null);
    String uriA = JDTUtils.toURI(cuA);
    String newUriA = uriA.replace("ObjectA", "ObjectA1");
    WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(uriA, newUriA))), new NullProgressMonitor());
    assertNotNull(edit);
    assertEquals(2, edit.getDocumentChanges().size());
    assertTrue(edit.getDocumentChanges().get(0).isLeft());
    assertEquals(edit.getDocumentChanges().get(0).getLeft().getTextDocument().getUri(), JDTUtils.toURI(cuB));
    assertEquals(TextEditUtil.apply(builderB.toString(), edit.getDocumentChanges().get(0).getLeft().getEdits()), "package test1;\n" + "public class B {\n" + "	public void foo() {\n" + "		ObjectA1 a = new ObjectA1();\n" + "		a.foo();\n" + "	}\n" + "}\n");
    assertTrue(edit.getDocumentChanges().get(1).isLeft());
    assertEquals(edit.getDocumentChanges().get(1).getLeft().getTextDocument().getUri(), uriA);
    assertEquals(TextEditUtil.apply(builderA.toString(), edit.getDocumentChanges().get(1).getLeft().getEdits()), "package test1;\n" + "public class ObjectA1 {\n" + "	public void foo() {\n" + "	}\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) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 4 with FileRename

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

the class FileEventHandlerTest method testRenameFiles_refactoringExists.

@Test
public void testRenameFiles_refactoringExists() throws JavaModelException, BadLocationException {
    when(clientPreferences.isResourceOperationSupported()).thenReturn(true);
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    StringBuilder builderA = new StringBuilder();
    builderA.append("package test1;\n");
    builderA.append("public class ObjectA1 {\n");
    builderA.append("	public void foo() {\n");
    builderA.append("	}\n");
    builderA.append("}\n");
    ICompilationUnit cuA = pack1.createCompilationUnit("ObjectA.java", builderA.toString(), false, null);
    String uriA = JDTUtils.toURI(cuA);
    String newUriA = uriA.replace("ObjectA", "ObjectA1");
    WorkspaceEdit edit = FileEventHandler.handleWillRenameFiles(new RenameFilesParams(Arrays.asList(new FileRename(uriA, newUriA))), new NullProgressMonitor());
    assertNull(edit);
}
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) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFilesParams(org.eclipse.lsp4j.RenameFilesParams) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 5 with FileRename

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

the class FileEventHandlerTest method testRenamePackage2.

// Test renaming package from "parent.pack2" to "newparent.newpack2"
@Test
public void testRenamePackage2() 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").replace("parent", "newparent");
    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 newparent.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 newparent.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)

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