Search in sources :

Example 11 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class MoveHandler method moveStaticMember.

private static RefactorWorkspaceEdit moveStaticMember(IMember[] members, String destinationTypeName, IProgressMonitor monitor) {
    if (members.length == 0 || destinationTypeName == null) {
        return new RefactorWorkspaceEdit("Failed to move static member because no members are selected or no destination is specified.");
    }
    CodeGenerationSettings settings = members[0].getTypeRoot() instanceof ICompilationUnit ? PreferenceManager.getCodeGenerationSettings((ICompilationUnit) members[0].getTypeRoot()) : PreferenceManager.getCodeGenerationSettings(members[0].getJavaProject().getProject());
    MoveStaticMembersProcessor processor = new MoveStaticMembersProcessor(members, settings);
    Refactoring refactoring = new MoveRefactoring(processor);
    CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.INITIAL_CONDITONS);
    SubMonitor subMonitor = SubMonitor.convert(monitor, "Moving static members...", 100);
    try {
        check.run(subMonitor.split(20));
        if (check.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
            JavaLanguageServerPlugin.logError("Failed to execute the 'move' refactoring.");
            JavaLanguageServerPlugin.logError(check.getStatus().toString());
        }
        processor.setDestinationTypeFullyQualifiedName(destinationTypeName);
        processor.setDeprecateDelegates(false);
        check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
        check.run(subMonitor.split(60));
        if (check.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
            JavaLanguageServerPlugin.logError("Failed to execute the 'move' refactoring.");
            JavaLanguageServerPlugin.logError(check.getStatus().toString());
            return new RefactorWorkspaceEdit("Failed to move static member. Reason: " + check.getStatus().toString());
        }
        Change change = processor.createChange(subMonitor.split(20));
        return new RefactorWorkspaceEdit(ChangeUtil.convertToWorkspaceEdit(change));
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e);
        return new RefactorWorkspaceEdit("Failed to move static member because of " + e.toString());
    } finally {
        subMonitor.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) SubMonitor(org.eclipse.core.runtime.SubMonitor) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) Change(org.eclipse.ltk.core.refactoring.Change) MoveStaticMembersProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring)

Example 12 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class MoveHandler method moveTypeToNewFile.

private static RefactorWorkspaceEdit moveTypeToNewFile(CodeActionParams params, IProgressMonitor monitor) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null) {
        return new RefactorWorkspaceEdit("Failed to move type to new file because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
    }
    IType type = getSelectedType(unit, params);
    if (type == null) {
        return new RefactorWorkspaceEdit("Failed to move type to new file because no type is selected.");
    }
    SubMonitor subMonitor = SubMonitor.convert(monitor, "Moving type to new file...", 100);
    try {
        MoveInnerToTopRefactoring refactoring = new MoveInnerToTopRefactoring(type, PreferenceManager.getCodeGenerationSettings(unit));
        CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
        check.run(subMonitor.split(50));
        if (check.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
            JavaLanguageServerPlugin.logError("Failed to execute the 'move' refactoring.");
            JavaLanguageServerPlugin.logError(check.getStatus().toString());
            return new RefactorWorkspaceEdit("Failed to move type to new file. Reason: " + check.getStatus().toString());
        }
        Change change = refactoring.createChange(subMonitor.split(50));
        return new RefactorWorkspaceEdit(ChangeUtil.convertToWorkspaceEdit(change));
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e);
        return new RefactorWorkspaceEdit("Failed to move type to new file because of " + e.toString());
    } catch (OperationCanceledException e) {
        return null;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) Change(org.eclipse.ltk.core.refactoring.Change) IType(org.eclipse.jdt.core.IType)

Example 13 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class MoveHandler method moveTypeToClass.

private static RefactorWorkspaceEdit moveTypeToClass(CodeActionParams params, String destinationTypeName, IProgressMonitor monitor) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null) {
        return new RefactorWorkspaceEdit("Failed to move type to another class because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
    }
    IType type = getSelectedType(unit, params);
    if (type == null) {
        return new RefactorWorkspaceEdit("Failed to move type to another class because no type is selected.");
    }
    try {
        if (RefactoringAvailabilityTesterCore.isMoveStaticAvailable(type)) {
            return moveStaticMember(new IMember[] { type }, destinationTypeName, monitor);
        }
        return new RefactorWorkspaceEdit("Moving non-static type to another class is not supported.");
    } catch (JavaModelException e) {
        return new RefactorWorkspaceEdit("Failed to move type to another class. Reason: " + e.toString());
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) IType(org.eclipse.jdt.core.IType)

Example 14 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class MoveHandler method move.

public static RefactorWorkspaceEdit move(MoveParams moveParams, IProgressMonitor monitor) {
    if (moveParams == null) {
        return new RefactorWorkspaceEdit("moveParams should not be empty.");
    }
    try {
        if ("moveResource".equalsIgnoreCase(moveParams.moveKind)) {
            String targetUri = null;
            if (moveParams.destination instanceof String) {
                targetUri = (String) moveParams.destination;
            } else {
                String json = (moveParams.destination == null ? null : new Gson().toJson(moveParams.destination));
                PackageNode packageNode = JSONUtility.toLsp4jModel(json, PackageNode.class);
                if (packageNode == null) {
                    return new RefactorWorkspaceEdit("Invalid destination object: " + moveParams.destination);
                }
                targetUri = packageNode.uri;
            }
            return moveCU(moveParams.sourceUris, targetUri, moveParams.updateReferences, monitor);
        } else if ("moveInstanceMethod".equalsIgnoreCase(moveParams.moveKind)) {
            String json = (moveParams.destination == null ? null : new Gson().toJson(moveParams.destination));
            LspVariableBinding variableBinding = JSONUtility.toLsp4jModel(json, LspVariableBinding.class);
            if (variableBinding == null) {
                return new RefactorWorkspaceEdit("Invalid destination object: " + moveParams.destination);
            }
            return moveInstanceMethod(moveParams.params, variableBinding, monitor);
        } else if ("moveStaticMember".equalsIgnoreCase(moveParams.moveKind)) {
            String typeName = resolveTargetTypeName(moveParams.destination);
            return moveStaticMember(moveParams.params, typeName, monitor);
        } else if ("moveTypeToNewFile".equalsIgnoreCase(moveParams.moveKind)) {
            return moveTypeToNewFile(moveParams.params, monitor);
        } else if ("moveTypeToClass".equalsIgnoreCase(moveParams.moveKind)) {
            String typeName = resolveTargetTypeName(moveParams.destination);
            return moveTypeToClass(moveParams.params, typeName, monitor);
        }
    } catch (IllegalArgumentException e) {
        return new RefactorWorkspaceEdit(e.getMessage());
    }
    return new RefactorWorkspaceEdit("Unsupported move operation.");
}
Also used : LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) Gson(com.google.gson.Gson) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)

Example 15 with RefactorWorkspaceEdit

use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.

the class MoveHandlerTest method testMoveInstanceMethod.

@Test
public void testMoveInstanceMethod() throws Exception {
    IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
    // @formatter:off
    ICompilationUnit cuSecond = pack1.createCompilationUnit("Second.java", "package test1;\n" + "\n" + "public class Second {\n" + "    public void foo() {\n" + "    }\n" + "}", false, null);
    // @formatter:on
    // @formatter:off
    pack1.createCompilationUnit("Third.java", "package test1;\n" + "\n" + "public class Third {\n" + "    public void bar() {\n" + "    }\n" + "}", false, null);
    // @formatter:on
    // @formatter:off
    ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + "    Second s;\n" + "    String name;\n" + "    int id;\n" + "    public void print(Third t) {\n" + "        System.out.println(name);\n" + "        s.foo();\n" + "        t.bar();\n" + "    }\n" + "\n" + "    public void log(Third t) {\n" + "        print(t);\n" + "    }\n" + "}", false, null);
    // @formatter:on
    CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "s.foo");
    MoveParams moveParams = new MoveParams("moveInstanceMethod", new String[] { JDTUtils.toURI(cu) }, params);
    MoveDestinationsResponse response = MoveHandler.getMoveDestinations(moveParams);
    assertNotNull(response);
    assertNull(response.errorMessage);
    assertNotNull(response.destinations);
    assertEquals(2, response.destinations.length);
    RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveInstanceMethod", params, response.destinations[1], true), new NullProgressMonitor());
    assertNotNull(refactorEdit);
    assertNotNull(refactorEdit.edit);
    List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
    assertEquals(2, changes.size());
    // @formatter:off
    String expected = "package test1;\n" + "\n" + "public class E {\n" + "    Second s;\n" + "    String name;\n" + "    int id;\n" + "    public void log(Third t) {\n" + "        s.print(this, t);\n" + "    }\n" + "}";
    // @formatter:on
    TextDocumentEdit textEdit = changes.get(0).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
    // @formatter:off
    expected = "package test1;\n" + "\n" + "public class Second {\n" + "    public void foo() {\n" + "    }\n" + "\n" + "	public void print(E e, Third t) {\n" + "	    System.out.println(e.name);\n" + "	    foo();\n" + "	    t.bar();\n" + "	}\n" + "}";
    // @formatter:on
    textEdit = changes.get(1).getLeft();
    assertNotNull(textEdit);
    assertEquals(expected, TextEditUtil.apply(cuSecond.getSource(), textEdit.getEdits()));
}
Also used : CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) MoveDestinationsResponse(org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveDestinationsResponse) 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) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Aggregations

RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)19 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)14 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)12 Test (org.junit.Test)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 MoveParams (org.eclipse.jdt.ls.core.internal.handlers.MoveHandler.MoveParams)7 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)7 TextDocumentEdit (org.eclipse.lsp4j.TextDocumentEdit)7 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)7 ArrayList (java.util.ArrayList)5 CoreException (org.eclipse.core.runtime.CoreException)5 SubMonitor (org.eclipse.core.runtime.SubMonitor)5 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)5 AbstractSelectionTest (org.eclipse.jdt.ls.core.internal.correction.AbstractSelectionTest)5 GetRefactorEditParams (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.GetRefactorEditParams)5 Range (org.eclipse.lsp4j.Range)5 Gson (com.google.gson.Gson)3 URI (java.net.URI)3 IType (org.eclipse.jdt.core.IType)3