-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhance CE Rename command to cover folder renames
It incorporates a new RenameFolderCommand.
- Loading branch information
Showing
6 changed files
with
223 additions
and
6 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...k.Core/UI/CodeExplorer/Commands/Abstract/CodeExplorerInteractiveRefactoringCommandBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Refactorings; | ||
using Rubberduck.UI.Command.Refactorings.Notifiers; | ||
using Rubberduck.VBEditor.Events; | ||
|
||
namespace Rubberduck.UI.CodeExplorer.Commands.Abstract | ||
{ | ||
public abstract class CodeExplorerInteractiveRefactoringCommandBase<TModel> : CodeExplorerRefactoringCommandBase<TModel> | ||
where TModel : class, IRefactoringModel | ||
{ | ||
private readonly IRefactoringAction<TModel> _refactoringAction; | ||
private readonly IRefactoringUserInteraction<TModel> _refactoringUserInteraction; | ||
private readonly IRefactoringFailureNotifier _failureNotifier; | ||
|
||
protected CodeExplorerInteractiveRefactoringCommandBase( | ||
IRefactoringAction<TModel> refactoringAction, | ||
IRefactoringUserInteraction<TModel> refactoringUserInteraction, | ||
IRefactoringFailureNotifier failureNotifier, | ||
IParserStatusProvider parserStatusProvider, | ||
IVbeEvents vbeEvents) | ||
: base(refactoringAction, failureNotifier, parserStatusProvider, vbeEvents) | ||
{ | ||
_refactoringUserInteraction = refactoringUserInteraction; | ||
_refactoringAction = refactoringAction; | ||
_failureNotifier = failureNotifier; | ||
} | ||
|
||
protected abstract TModel InitialModelFromParameter(object parameter); | ||
protected abstract void ValidateInitialModel(TModel model); | ||
|
||
protected override TModel ModelFromParameter(object parameter) | ||
{ | ||
var initialModel = InitialModelFromParameter(parameter); | ||
ValidateInitialModel(initialModel); | ||
return _refactoringUserInteraction.UserModifiedModel(initialModel); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
Rubberduck.Core/UI/CodeExplorer/Commands/Abstract/CodeExplorerRefactoringCommandBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Refactorings; | ||
using Rubberduck.Refactorings.Exceptions; | ||
using Rubberduck.UI.Command.Refactorings.Notifiers; | ||
using Rubberduck.VBEditor.Events; | ||
|
||
namespace Rubberduck.UI.CodeExplorer.Commands.Abstract | ||
{ | ||
public abstract class CodeExplorerRefactoringCommandBase<TModel> : CodeExplorerCommandBase | ||
where TModel : class, IRefactoringModel | ||
{ | ||
private readonly IParserStatusProvider _parserStatusProvider; | ||
|
||
private readonly IRefactoringAction<TModel> _refactoringAction; | ||
private readonly IRefactoringFailureNotifier _failureNotifier; | ||
|
||
protected CodeExplorerRefactoringCommandBase( | ||
IRefactoringAction<TModel> refactoringAction, | ||
IRefactoringFailureNotifier failureNotifier, | ||
IParserStatusProvider parserStatusProvider, | ||
IVbeEvents vbeEvents) | ||
: base(vbeEvents) | ||
{ | ||
_refactoringAction = refactoringAction; | ||
_failureNotifier = failureNotifier; | ||
|
||
_parserStatusProvider = parserStatusProvider; | ||
|
||
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute); | ||
} | ||
|
||
private bool SpecialEvaluateCanExecute(object parameter) | ||
{ | ||
return _parserStatusProvider.Status == ParserState.Ready; | ||
} | ||
|
||
protected abstract TModel ModelFromParameter(object parameter); | ||
protected abstract void ValidateModel(TModel model); | ||
|
||
protected override void OnExecute(object parameter) | ||
{ | ||
if (!CanExecute(parameter)) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
var model = ModelFromParameter(parameter); | ||
ValidateModel(model); | ||
_refactoringAction.Refactor(model); | ||
} | ||
catch (RefactoringAbortedException) | ||
{ } | ||
catch (RefactoringException exception) | ||
{ | ||
_failureNotifier.Notify(exception); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Rubberduck.Core/UI/CodeExplorer/Commands/RenameFolderCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Rubberduck.JunkDrawer.Extensions; | ||
using Rubberduck.Navigation.CodeExplorer; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Refactorings; | ||
using Rubberduck.Refactorings.Exceptions; | ||
using Rubberduck.Refactorings.RenameFolder; | ||
using Rubberduck.UI.CodeExplorer.Commands.Abstract; | ||
using Rubberduck.UI.Command.Refactorings.Notifiers; | ||
using Rubberduck.VBEditor.Events; | ||
|
||
namespace Rubberduck.UI.CodeExplorer.Commands | ||
{ | ||
public class RenameFolderCommand : CodeExplorerInteractiveRefactoringCommandBase<RenameFolderModel> | ||
{ | ||
private static readonly Type[] ApplicableNodes = | ||
{ | ||
typeof(CodeExplorerCustomFolderViewModel) | ||
}; | ||
|
||
private RubberduckParserState _state; | ||
|
||
public RenameFolderCommand( | ||
RenameFolderRefactoringAction refactoringAction, | ||
RefactoringUserInteraction<IRenameFolderPresenter, RenameFolderModel> userInteraction, | ||
RenameFolderFailedNotifier failureNotifier, | ||
IParserStatusProvider parserStatusProvider, | ||
IVbeEvents vbeEvents, | ||
RubberduckParserState state) | ||
: base(refactoringAction, userInteraction, failureNotifier, parserStatusProvider, vbeEvents) | ||
{ | ||
_state = state; | ||
} | ||
|
||
public override IEnumerable<Type> ApplicableNodeTypes => ApplicableNodes; | ||
|
||
protected override RenameFolderModel InitialModelFromParameter(object parameter) | ||
{ | ||
if (!(parameter is CodeExplorerCustomFolderViewModel folderModel)) | ||
{ | ||
throw new ArgumentException(nameof(parameter)); | ||
} | ||
|
||
return ModelFromNode(folderModel); | ||
} | ||
|
||
private static RenameFolderModel ModelFromNode(CodeExplorerCustomFolderViewModel folderModel) | ||
{ | ||
var folder = folderModel.FullPath; | ||
var containedModules = ContainedModules(folderModel); | ||
var initialSubFolder = folder.SubFolderName(); | ||
return new RenameFolderModel(folder, containedModules, initialSubFolder); | ||
} | ||
|
||
private static ICollection<ModuleDeclaration> ContainedModules(ICodeExplorerNode itemModel) | ||
{ | ||
if (itemModel is CodeExplorerComponentViewModel componentModel) | ||
{ | ||
var component = componentModel.Declaration; | ||
return component is ModuleDeclaration moduleDeclaration | ||
? new List<ModuleDeclaration> { moduleDeclaration } | ||
: new List<ModuleDeclaration>(); | ||
} | ||
|
||
return itemModel.Children | ||
.SelectMany(ContainedModules) | ||
.ToList(); | ||
} | ||
|
||
protected override void ValidateInitialModel(RenameFolderModel model) | ||
{ | ||
var firstStaleAffectedModules = model.ModulesToMove | ||
.FirstOrDefault(module => _state.IsNewOrModified(module.QualifiedModuleName)); | ||
if (firstStaleAffectedModules != null) | ||
{ | ||
throw new AffectedModuleIsStaleException(firstStaleAffectedModules.QualifiedModuleName); | ||
} | ||
} | ||
|
||
protected override void ValidateModel(RenameFolderModel model) | ||
{ } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Rubberduck.Core/UI/Command/Refactorings/Notifiers/RenameFolderFailedNotifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Rubberduck.Interaction; | ||
|
||
namespace Rubberduck.UI.Command.Refactorings.Notifiers | ||
{ | ||
public class RenameFolderFailedNotifier : RefactoringFailureNotifierBase | ||
{ | ||
public RenameFolderFailedNotifier(IMessageBox messageBox) | ||
: base(messageBox) | ||
{} | ||
|
||
protected override string Caption => Resources.RubberduckUI.RenameDialog_Caption; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
Rubberduck.Core/UI/Refactorings/RenameFolder/RenameFolderView.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters