Search in sources :

Example 1 with MoveInnerToTopRefactoring

use of org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring 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)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IType (org.eclipse.jdt.core.IType)1 MoveInnerToTopRefactoring (org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring)1 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)1 Change (org.eclipse.ltk.core.refactoring.Change)1 CheckConditionsOperation (org.eclipse.ltk.core.refactoring.CheckConditionsOperation)1