Search in sources :

Example 6 with RenameFile

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

the class ChangeUtil method convertCUResourceChange.

private static void convertCUResourceChange(WorkspaceEdit edit, RenameCompilationUnitChange cuChange) {
    ICompilationUnit modifiedCU = (ICompilationUnit) cuChange.getModifiedElement();
    RenameFile rf = new RenameFile();
    String newCUName = cuChange.getNewName();
    IPath currentPath = modifiedCU.getResource().getLocation();
    rf.setOldUri(ResourceUtils.fixURI(modifiedCU.getResource().getRawLocationURI()));
    IPath newPath = currentPath.removeLastSegments(1).append(newCUName);
    rf.setNewUri(ResourceUtils.fixURI(newPath.toFile().toURI()));
    edit.getDocumentChanges().add(Either.forRight(rf));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPath(org.eclipse.core.runtime.IPath) RenameFile(org.eclipse.lsp4j.RenameFile)

Example 7 with RenameFile

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

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

the class ChangeUtil method convertMoveCompilationUnitChange.

private static void convertMoveCompilationUnitChange(WorkspaceEdit edit, MoveCompilationUnitChange change) throws JavaModelException {
    IPackageFragment newPackage = change.getDestinationPackage();
    ICompilationUnit unit = change.getCu();
    CompilationUnit astCU = RefactoringASTParser.parseWithASTProvider(unit, true, new NullProgressMonitor());
    ASTRewrite rewrite = ASTRewrite.create(astCU.getAST());
    IPackageDeclaration[] packDecls = unit.getPackageDeclarations();
    String oldPackageName = packDecls.length > 0 ? packDecls[0].getElementName() : "";
    if (!Objects.equals(oldPackageName, newPackage.getElementName())) {
        // update the package declaration
        if (updatePackageStatement(astCU, newPackage.getElementName(), rewrite, unit)) {
            convertTextEdit(edit, unit, rewrite.rewriteAST());
        }
    }
    RenameFile cuResourceChange = new RenameFile();
    cuResourceChange.setOldUri(JDTUtils.toURI(unit));
    IPath newCUPath = newPackage.getResource().getLocation().append(unit.getPath().lastSegment());
    String newUri = ResourceUtils.fixURI(newCUPath.toFile().toURI());
    cuResourceChange.setNewUri(newUri);
    edit.getDocumentChanges().add(Either.forRight(cuResourceChange));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) RenameFile(org.eclipse.lsp4j.RenameFile)

Example 9 with RenameFile

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

the class ChangeUtilTest method testMergeChanges.

@Test
public void testMergeChanges() {
    WorkspaceEdit editA = new WorkspaceEdit();
    String uriA = "uriA";
    TextEdit textEdit = new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), "package test;");
    editA.getChanges().put(uriA, Arrays.asList(textEdit));
    WorkspaceEdit root = ChangeUtil.mergeChanges(null, editA);
    assertNotNull(root);
    assertNotNull(root.getChanges());
    assertNotNull(root.getChanges().get(uriA));
    assertEquals(1, root.getChanges().size());
    assertEquals(1, root.getChanges().get(uriA).size());
    WorkspaceEdit editB = new WorkspaceEdit();
    editB.getChanges().put(uriA, Arrays.asList(textEdit));
    List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();
    TextDocumentEdit textDocumentEdit = new TextDocumentEdit(new VersionedTextDocumentIdentifier(uriA, 1), Arrays.asList(textEdit));
    documentChanges.add(Either.forLeft(textDocumentEdit));
    ResourceOperation resourceOperation = new RenameFile("uriA", "uriB");
    documentChanges.add(Either.forRight(resourceOperation));
    editB.setDocumentChanges(documentChanges);
    root = ChangeUtil.mergeChanges(editA, editB);
    assertNotNull(root);
    assertNotNull(root.getChanges());
    assertNotNull(root.getChanges().get(uriA));
    assertEquals(1, root.getChanges().size());
    assertEquals(2, root.getChanges().get(uriA).size());
    assertNotNull(root.getDocumentChanges());
    assertEquals(2, root.getDocumentChanges().size());
    assertTrue(root.getDocumentChanges().get(0).isLeft());
    assertEquals(textDocumentEdit, root.getDocumentChanges().get(0).getLeft());
    assertTrue(root.getDocumentChanges().get(1).isRight());
    assertEquals(resourceOperation, root.getDocumentChanges().get(1).getRight());
    root = ChangeUtil.mergeChanges(editA, editB, true);
    assertNotNull(root);
    assertNotNull(root.getChanges());
    assertNotNull(root.getChanges().get(uriA));
    assertEquals(1, root.getChanges().size());
    assertEquals(2, root.getChanges().get(uriA).size());
    assertNotNull(root.getDocumentChanges());
    assertEquals(1, root.getDocumentChanges().size());
    assertTrue(root.getDocumentChanges().get(0).isLeft());
    assertEquals(textDocumentEdit, root.getDocumentChanges().get(0).getLeft());
}
Also used : VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Position(org.eclipse.lsp4j.Position) TextEdit(org.eclipse.lsp4j.TextEdit) ArrayList(java.util.ArrayList) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) Range(org.eclipse.lsp4j.Range) RenameFile(org.eclipse.lsp4j.RenameFile) ResourceOperation(org.eclipse.lsp4j.ResourceOperation) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 10 with RenameFile

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

the class ChangeUtilTest method testConvertSimpleCompositeChange.

// Composite Changes
@Test
public void testConvertSimpleCompositeChange() throws CoreException {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "", false, null);
    CompositeChange change = new CompositeChange("simple composite change");
    RenameCompilationUnitChange resourceChange = new RenameCompilationUnitChange(cu, "ENew.java");
    change.add(resourceChange);
    CompilationUnitChange textChange = new CompilationUnitChange("insertText", cu);
    textChange.setEdit(new InsertEdit(0, "// some content"));
    change.add(textChange);
    WorkspaceEdit edit = ChangeUtil.convertToWorkspaceEdit(change);
    assertEquals(edit.getDocumentChanges().size(), 2);
    assertTrue(edit.getDocumentChanges().get(0).getRight() instanceof RenameFile);
    assertTrue(edit.getDocumentChanges().get(1).getLeft() instanceof TextDocumentEdit);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) RenameFile(org.eclipse.lsp4j.RenameFile) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) RenameCompilationUnitChange(org.eclipse.jdt.ls.core.internal.corext.refactoring.changes.RenameCompilationUnitChange) 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