Search in sources :

Example 1 with CreateFile

use of org.eclipse.lsp4j.CreateFile 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 2 with CreateFile

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

the class ChangeUtil method convertCreateCompilationUnitChange.

private static void convertCreateCompilationUnitChange(WorkspaceEdit edit, CreateCompilationUnitChange change) {
    ICompilationUnit unit = change.getCu();
    CreateFile createFile = new CreateFile();
    createFile.setUri(JDTUtils.toURI(unit));
    createFile.setOptions(new CreateFileOptions(false, true));
    edit.getDocumentChanges().add(Either.forRight(createFile));
    InsertEdit textEdit = new InsertEdit(0, change.getPreview());
    convertTextEdit(edit, unit, textEdit);
}
Also used : CreateFile(org.eclipse.lsp4j.CreateFile) CreateFileOptions(org.eclipse.lsp4j.CreateFileOptions) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit)

Example 3 with CreateFile

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

the class ChangeUtil method convertCreateFileChange.

private static void convertCreateFileChange(WorkspaceEdit edit, CreateFileChange createFileChange) {
    CreateFile createFile = new CreateFile();
    createFile.setUri(ResourceUtils.fixURI(createFileChange.getPath().toFile().toURI()));
    createFile.setOptions(new CreateFileOptions(false, true));
    edit.getDocumentChanges().add(Either.forRight(createFile));
}
Also used : CreateFile(org.eclipse.lsp4j.CreateFile) CreateFileOptions(org.eclipse.lsp4j.CreateFileOptions)

Example 4 with CreateFile

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

the class ChangeUtil method convertRenamePackcageChange.

private static void convertRenamePackcageChange(WorkspaceEdit edit, RenamePackageChange packageChange) throws CoreException {
    IPackageFragment pack = (IPackageFragment) packageChange.getModifiedElement();
    IPath newPackageFragment = new Path(packageChange.getNewName().replace('.', IPath.SEPARATOR));
    IPath oldPackageFragment = new Path(packageChange.getOldName().replace('.', IPath.SEPARATOR));
    IPath newPackagePath = pack.getResource().getLocation().removeLastSegments(oldPackageFragment.segmentCount()).append(newPackageFragment);
    if (packageChange.getRenameSubpackages()) {
        IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
        String oldPrefix = packageChange.getOldName();
        for (IPackageFragment currentPackage : allPackages) {
            String newPkgName = packageChange.getNewName() + currentPackage.getElementName().substring(oldPrefix.length());
            // update package's declaration
            convertPackageUpdateEdit(currentPackage.getCompilationUnits(), newPkgName, edit);
        }
        RenameFile renameFile = new RenameFile();
        renameFile.setNewUri(ResourceUtils.fixURI(newPackagePath.toFile().toURI()));
        renameFile.setOldUri(ResourceUtils.fixURI(pack.getResource().getRawLocationURI()));
        edit.getDocumentChanges().add(Either.forRight(renameFile));
    } else {
        // update package's declaration
        convertPackageUpdateEdit(pack.getCompilationUnits(), packageChange.getNewName(), edit);
        CreateFile createFile = new CreateFile();
        createFile.setUri(ResourceUtils.fixURI(newPackagePath.append(TEMP_FILE_NAME).toFile().toURI()));
        createFile.setOptions(new CreateFileOptions(false, true));
        edit.getDocumentChanges().add(Either.forRight(createFile));
        for (ICompilationUnit unit : pack.getCompilationUnits()) {
            RenameFile cuResourceChange = new RenameFile();
            cuResourceChange.setOldUri(ResourceUtils.fixURI(unit.getResource().getLocationURI()));
            IPath newCUPath = newPackagePath.append(unit.getPath().lastSegment());
            cuResourceChange.setNewUri(ResourceUtils.fixURI(newCUPath.toFile().toURI()));
            edit.getDocumentChanges().add(Either.forRight(cuResourceChange));
        }
        // Workaround: https://github.com/Microsoft/language-server-protocol/issues/272
        DeleteFile deleteFile = new DeleteFile();
        deleteFile.setUri(ResourceUtils.fixURI(newPackagePath.append(TEMP_FILE_NAME).toFile().toURI()));
        deleteFile.setOptions(new DeleteFileOptions(false, true));
        edit.getDocumentChanges().add(Either.forRight(deleteFile));
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) CreateFile(org.eclipse.lsp4j.CreateFile) CreateFileOptions(org.eclipse.lsp4j.CreateFileOptions) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) DeleteFileOptions(org.eclipse.lsp4j.DeleteFileOptions) RenameFile(org.eclipse.lsp4j.RenameFile) DeleteFile(org.eclipse.lsp4j.DeleteFile)

Example 5 with CreateFile

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

Aggregations

CreateFile (org.eclipse.lsp4j.CreateFile)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 CreateFileOptions (org.eclipse.lsp4j.CreateFileOptions)3 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)2 RenameFile (org.eclipse.lsp4j.RenameFile)2 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)2 Test (org.junit.Test)2 LinkedList (java.util.LinkedList)1 IPath (org.eclipse.core.runtime.IPath)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)1 MoveParams (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams)1 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)1 DeleteFile (org.eclipse.lsp4j.DeleteFile)1 DeleteFileOptions (org.eclipse.lsp4j.DeleteFileOptions)1 Position (org.eclipse.lsp4j.Position)1 ResourceOperation (org.eclipse.lsp4j.ResourceOperation)1 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)1