Skip to content

Commit

Permalink
Add MoveContainingFolderRefactoring
Browse files Browse the repository at this point in the history
It moves the containing folder into another folder.

Also fixes ToVbaStringLiteral. (It was missing the surrounding quotes.)
  • Loading branch information
MDoerner committed Mar 21, 2020
1 parent 213822f commit 7b2b173
Show file tree
Hide file tree
Showing 37 changed files with 1,095 additions and 95 deletions.
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Rubberduck.Common;
using Rubberduck.JunkDrawer.Extensions;
using Rubberduck.Navigation.Folders;
using Rubberduck.Parsing.Symbols;
using Rubberduck.VBEditor;
Expand Down Expand Up @@ -30,7 +32,7 @@ public sealed class CodeExplorerCustomFolderViewModel : CodeExplorerItemViewMode
{
_vbe = vbe;
FolderDepth = parent is CodeExplorerCustomFolderViewModel folder ? folder.FolderDepth + 1 : 1;
FullPath = fullPath?.Trim('"') ?? string.Empty;
FullPath = fullPath?.FromVbaStringLiteral() ?? string.Empty;
Name = name.Replace("\"", string.Empty);

AddNewChildren(ref declarations);
Expand All @@ -44,7 +46,7 @@ public sealed class CodeExplorerCustomFolderViewModel : CodeExplorerItemViewMode

public string FullPath { get; }

public string FolderAttribute => $"'@Folder(\"{FullPath.Replace("\"", string.Empty)}\")";
public string FolderAttribute => $"'@Folder({FullPath.ToVbaStringLiteral()})";

/// <summary>
/// One-based depth in the folder hierarchy.
Expand Down
49 changes: 49 additions & 0 deletions Rubberduck.Core/Navigation/Folders/DeclarationFolderExtensions.cs
@@ -0,0 +1,49 @@
using System;
using Rubberduck.Parsing.Symbols;
using Rubberduck.JunkDrawer.Extensions;

namespace Rubberduck.Navigation.Folders
{
public static class DeclarationFolderExtensions
{
public static string RootFolder(this Declaration declaration)
{
return declaration?.CustomFolder?.RootFolder()
?? declaration?.ProjectName
?? string.Empty;
}

public static bool IsInFolder(this Declaration declaration, string folder)
{
if (declaration?.CustomFolder is null || folder is null)
{
return false;
}

return declaration.CustomFolder.Equals(folder, StringComparison.Ordinal);
}

public static bool IsInSubFolder(this Declaration declaration, string folder)
{
var declarationFolder = declaration?.CustomFolder;
if (declarationFolder is null || folder is null)
{
return false;
}

return declarationFolder.IsSubFolderOf(folder);
}

public static bool IsInFolderOrSubFolder(this Declaration declaration, string folder)
{
var declarationFolder = declaration?.CustomFolder;
if (declarationFolder is null || folder is null)
{
return false;
}

return declaration.IsInFolder(folder)
|| declarationFolder.IsSubFolderOf(folder);
}
}
}
78 changes: 0 additions & 78 deletions Rubberduck.Core/Navigation/Folders/FolderExtensions.cs

This file was deleted.

3 changes: 3 additions & 0 deletions Rubberduck.Core/Rubberduck.Core.csproj
Expand Up @@ -117,6 +117,9 @@
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
<Compile Update="UI\Refactorings\MoveFolder\MoveMultipleFoldersView.xaml.cs">
<DependentUpon>MoveMultipleFoldersView.xaml</DependentUpon>
</Compile>
<Compile Update="UI\Refactorings\MoveToFolder\MoveMultipleToFolderView.xaml.cs">
<DependentUpon>MoveMultipleToFolderView.xaml</DependentUpon>
</Compile>
Expand Down
@@ -0,0 +1,22 @@
using Rubberduck.Parsing.VBA;
using Rubberduck.UI.Command.MenuItems.ParentMenus;
using Rubberduck.UI.Command.Refactorings;

namespace Rubberduck.UI.Command.MenuItems
{
public class CodePaneRefactorMoveContainingFolderCommandMenuItem : CommandMenuItemBase
{
public CodePaneRefactorMoveContainingFolderCommandMenuItem(CodePaneRefactorMoveContainingFolderCommand command)
: base(command)
{}

public override string Key => "RefactorMenu_MoveContainingFolder";
public override int DisplayOrder => (int)RefactoringsMenuItemDisplayOrder.MoveContainingFolder;
public override bool BeginGroup => false;

public override bool EvaluateCanExecute(RubberduckParserState state)
{
return state != null && Command.CanExecute(null);
}
}
}
Expand Up @@ -25,6 +25,7 @@ public enum RefactoringsMenuItemDisplayOrder
IntroduceParameter,
IntroduceField,
MoveToFolder,
MoveContainingFolder,
AddRemoveReferences
}
}
@@ -0,0 +1,42 @@
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Refactorings.MoveFolder;
using Rubberduck.UI.Command.Refactorings.Notifiers;
using Rubberduck.VBEditor.Utility;

namespace Rubberduck.UI.Command.Refactorings
{
public class CodePaneRefactorMoveContainingFolderCommand : RefactorCodePaneCommandBase
{
private readonly RubberduckParserState _state;
private readonly ISelectedDeclarationProvider _selectedDeclarationProvider;

public CodePaneRefactorMoveContainingFolderCommand(
MoveContainingFolderRefactoring refactoring,
MoveContainingFolderRefactoringFailedNotifier failureNotifier,
ISelectionProvider selectionProvider,
RubberduckParserState state,
ISelectedDeclarationProvider selectedDeclarationProvider)
: base(refactoring, failureNotifier, selectionProvider, state)
{
_selectedDeclarationProvider = selectedDeclarationProvider;
_state = state;

AddToCanExecuteEvaluation(SpecializedEvaluateCanExecute);
}

private bool SpecializedEvaluateCanExecute(object parameter)
{
var target = GetTarget();

return target != null
&& target is ModuleDeclaration
&& !_state.IsNewOrModified(target.QualifiedModuleName);
}

private Declaration GetTarget()
{
return _selectedDeclarationProvider.SelectedModule();
}
}
}
@@ -0,0 +1,38 @@
using Rubberduck.Interaction;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Refactorings.Exceptions;
using Rubberduck.Refactorings.Exceptions.MoveToFolder;

namespace Rubberduck.UI.Command.Refactorings.Notifiers
{
public class MoveContainingFolderRefactoringFailedNotifier : RefactoringFailureNotifierBase
{
public MoveContainingFolderRefactoringFailedNotifier(IMessageBox messageBox)
: base(messageBox)
{}

protected override string Caption => Resources.RubberduckUI.MoveToFolderDialog_Caption;

protected override string Message(RefactoringException exception)
{
switch (exception)
{
case InvalidDeclarationTypeException invalidDeclarationType:
Logger.Warn(invalidDeclarationType);
return string.Format(
Resources.RubberduckUI.RefactoringFailure_InvalidDeclarationType,
invalidDeclarationType.TargetDeclaration.QualifiedName,
invalidDeclarationType.TargetDeclaration.DeclarationType,
DeclarationType.Module);
case NoTargetFolderException noTargetFolder:
return Resources.RubberduckUI.RefactoringFailure_NoTargetFolder;
case AffectedModuleIsStaleException affectedModuleIsStale:
return string.Format(
Resources.RubberduckUI.RefactoringFailure_AffectedModuleIsStale,
affectedModuleIsStale.StaleModule.ToString());
default:
return base.Message(exception);
}
}
}
}
@@ -1,6 +1,7 @@
using Rubberduck.Interaction;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Refactorings.Exceptions;
using Rubberduck.Refactorings.Exceptions.MoveToFolder;

namespace Rubberduck.UI.Command.Refactorings.Notifiers
{
Expand All @@ -18,10 +19,17 @@ protected override string Message(RefactoringException exception)
{
case InvalidDeclarationTypeException invalidDeclarationType:
Logger.Warn(invalidDeclarationType);
return string.Format(Resources.RubberduckUI.RefactoringFailure_InvalidDeclarationType,
return string.Format(
Resources.RubberduckUI.RefactoringFailure_InvalidDeclarationType,
invalidDeclarationType.TargetDeclaration.QualifiedName,
invalidDeclarationType.TargetDeclaration.DeclarationType,
DeclarationType.Module);
case NoTargetFolderException noTargetFolder:
return Resources.RubberduckUI.RefactoringFailure_NoTargetFolder;
case AffectedModuleIsStaleException affectedModuleIsStale:
return string.Format(
Resources.RubberduckUI.RefactoringFailure_AffectedModuleIsStale,
affectedModuleIsStale.StaleModule.ToString());
default:
return base.Message(exception);
}
Expand Down
@@ -0,0 +1,15 @@
using Rubberduck.Refactorings.MoveFolder;
using Rubberduck.Resources;

namespace Rubberduck.UI.Refactorings.MoveFolder
{
internal class MoveMultipleFoldersPresenter : RefactoringPresenterBase<MoveMultipleFoldersModel>, IMoveMultipleFoldersPresenter
{
private static readonly DialogData DialogData = DialogData.Create(RubberduckUI.MoveToFolderDialog_Caption, 164, 684);

public MoveMultipleFoldersPresenter(MoveMultipleFoldersModel model, IRefactoringDialogFactory dialogFactory) :
base(DialogData, model, dialogFactory)
{}
}
}

0 comments on commit 7b2b173

Please sign in to comment.