Search in sources :

Example 1 with RenameFile

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 2 with RenameFile

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

Example 3 with RenameFile

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());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Position(org.eclipse.lsp4j.Position) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) LinkedList(java.util.LinkedList) RenameFile(org.eclipse.lsp4j.RenameFile) CreateFile(org.eclipse.lsp4j.CreateFile) TextEdit(org.eclipse.lsp4j.TextEdit) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 4 with RenameFile

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

Example 5 with RenameFile

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

Aggregations

RenameFile (org.eclipse.lsp4j.RenameFile)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)9 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)8 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)7 Test (org.junit.Test)7 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)6 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)6 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)5 TextEdit (org.eclipse.lsp4j.TextEdit)5 ResourceOperation (org.eclipse.lsp4j.ResourceOperation)4 ArrayList (java.util.ArrayList)3 IPath (org.eclipse.core.runtime.IPath)3 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)3 Position (org.eclipse.lsp4j.Position)3 LinkedList (java.util.LinkedList)2 RenameCompilationUnitChange (org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange)2 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)2 MoveParams (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams)2 CreateFile (org.eclipse.lsp4j.CreateFile)2 Path (org.eclipse.core.runtime.Path)1