Search in sources :

Example 6 with ResourceOperation

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

the class MoveHandlerTest method testMoveInnerTypeToFile.

@Test
public void testMoveInnerTypeToFile() throws Exception {
    System.setProperty("line.separator", "\n");
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    ICompilationUnit cu = pack1.createCompilationUnit("Top.java", "package test1;\n" + "\n" + "public class Top {\n" + "    String name;\n\n" + "    public class Inner {\n" + "        public void print() {\n" + "            System.out.println(Top.this.name);\n" + "        }\n" + "    }\n" + "}", false, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "class Inner");
    RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveTypeToNewFile", params, "Foo", 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 test1;\n" + "\n" + "public class Top {\n" + "    String name;\n" + "}";
    // @formatter:on
    TextDocumentEdit textEdit = changes.get(0).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
    ResourceOperation resourceOperation = changes.get(1).getRight();
    assertNotNull(resourceOperation);
    assertTrue(resourceOperation instanceof CreateFile);
    assertEquals(ResourceUtils.fixURI(cu.getResource().getRawLocationURI()).replace("Top", "Inner"), ((CreateFile) resourceOperation).getUri());
    // @formatter:off
    expected = "package test1;\n" + "\n" + "public class Inner {\n" + "    /**\n" + "	 *\n" + "	 */\n" + "	private final Top top;\n\n" + "	/**\n" + "	 * @param top\n" + "	 */\n" + "	Inner(Top top) {\n" + "		this.top = top;\n" + "	}\n\n" + "	public void print() {\n" + "        System.out.println(this.top.name);\n" + "    }\n" + "}";
    // @formatter:on
    textEdit = changes.get(2).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(pack1.getCompilationUnit("Inner.java").getWorkingCopy(null), textEdit.getEdits()));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CreateFile(org.eclipse.lsp4j.CreateFile) 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) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 7 with ResourceOperation

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

the class ReorgQuickFixTest method assertRenameFileOperation.

private void assertRenameFileOperation(Either<Command, CodeAction> codeAction, String newUri) {
    WorkspaceEdit edit = getWorkspaceEdit(codeAction);
    List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = edit.getDocumentChanges();
    assertNotNull(documentChanges);
    assertEquals(1, documentChanges.size());
    ResourceOperation resourceOperation = documentChanges.get(0).getRight();
    assertNotNull(resourceOperation);
    assertTrue(resourceOperation instanceof RenameFile);
    assertEquals(newUri, ((RenameFile) resourceOperation).getNewUri());
}
Also used : Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) RenameFile(org.eclipse.lsp4j.RenameFile) ResourceOperation(org.eclipse.lsp4j.ResourceOperation)

Aggregations

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