Skip to content

Commit 799b76a

Browse files
committed
Add code pane command for AnnotateDeclaration
Also fixes a bug in the setup of CodePane.GetQualifiedSelection in the MockProjectBuilder.
1 parent 5f24969 commit 799b76a

File tree

18 files changed

+249
-6
lines changed

18 files changed

+249
-6
lines changed

Rubberduck.CodeAnalysis/Inspections/Extensions/DeclarationTypeExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
namespace Rubberduck.CodeAnalysis.Inspections.Extensions
66
{
7-
internal static class DeclarationTypeExtensions
7+
public static class DeclarationTypeExtensions
88
{
9+
//ToDo: Move this to resources. (This will require moving resource lookups to Core.)
910
public static string ToLocalizedString(this DeclarationType type)
1011
{
1112
return RubberduckUI.ResourceManager.GetString("DeclarationType_" + type, CultureInfo.CurrentUICulture);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Rubberduck.Parsing.VBA;
2+
using Rubberduck.UI.Command.MenuItems.ParentMenus;
3+
using Rubberduck.UI.Command.Refactorings;
4+
5+
namespace Rubberduck.UI.Command.MenuItems
6+
{
7+
public class CodePaneAnnotateDeclarationCommandMenuItem : CommandMenuItemBase
8+
{
9+
public CodePaneAnnotateDeclarationCommandMenuItem(CodePaneAnnotateDeclarationCommand command)
10+
: base(command)
11+
{ }
12+
13+
public override string Key => "RefactorMenu_AnnotateDeclaration";
14+
public override int DisplayOrder => (int)RefactoringsMenuItemDisplayOrder.AnnotateDeclaration;
15+
16+
public override bool EvaluateCanExecute(RubberduckParserState state)
17+
{
18+
return state != null && Command.CanExecute(null);
19+
}
20+
}
21+
}

Rubberduck.Core/UI/Command/MenuItems/ParentMenus/RefactoringsParentMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public RefactoringsParentMenu(IEnumerable<IMenuItem> items)
1414

1515
public enum RefactoringsMenuItemDisplayOrder
1616
{
17+
AnnotateDeclaration,
1718
RenameIdentifier,
1819
ExtractMethod,
1920
ExtractInterface,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Rubberduck.Parsing.Symbols;
2+
using Rubberduck.Parsing.VBA;
3+
using Rubberduck.Refactorings.AnnotateDeclaration;
4+
using Rubberduck.UI.Command.Refactorings.Notifiers;
5+
using Rubberduck.VBEditor.Utility;
6+
7+
namespace Rubberduck.UI.Command.Refactorings
8+
{
9+
public class CodePaneAnnotateDeclarationCommand : RefactorCodePaneCommandBase
10+
{
11+
private readonly RubberduckParserState _state;
12+
private readonly ISelectedDeclarationProvider _selectedDeclarationProvider;
13+
14+
public CodePaneAnnotateDeclarationCommand(
15+
AnnotateDeclarationRefactoring refactoring,
16+
AnnotateDeclarationFailedNotifier failureNotifier,
17+
ISelectionProvider selectionProvider,
18+
IParserStatusProvider parserStatusProvider,
19+
RubberduckParserState state,
20+
ISelectedDeclarationProvider selectedDeclarationProvider)
21+
: base(refactoring, failureNotifier, selectionProvider, parserStatusProvider)
22+
{
23+
_selectedDeclarationProvider = selectedDeclarationProvider;
24+
_state = state;
25+
26+
AddToCanExecuteEvaluation(SpecializedEvaluateCanExecute);
27+
}
28+
29+
private bool SpecializedEvaluateCanExecute(object parameter)
30+
{
31+
var target = GetTarget();
32+
33+
if (target == null)
34+
{
35+
return false;
36+
}
37+
38+
var targetType = target.DeclarationType;
39+
40+
if (!targetType.HasFlag(DeclarationType.Member)
41+
&& !targetType.HasFlag(DeclarationType.Module)
42+
&& !targetType.HasFlag(DeclarationType.Variable)
43+
&& !targetType.HasFlag(DeclarationType.Constant))
44+
{
45+
return false;
46+
}
47+
48+
return !_state.IsNewOrModified(target.QualifiedModuleName);
49+
}
50+
51+
private Declaration GetTarget()
52+
{
53+
return _selectedDeclarationProvider.SelectedDeclaration();
54+
}
55+
}
56+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Rubberduck.CodeAnalysis.Inspections.Extensions;
2+
using Rubberduck.Interaction;
3+
using Rubberduck.Refactorings.Exceptions;
4+
using Rubberduck.Resources;
5+
6+
namespace Rubberduck.UI.Command.Refactorings.Notifiers
7+
{
8+
public class AnnotateDeclarationFailedNotifier : RefactoringFailureNotifierBase
9+
{
10+
public AnnotateDeclarationFailedNotifier(IMessageBox messageBox)
11+
: base(messageBox)
12+
{ }
13+
14+
protected override string Caption => RubberduckUI.AnnotateDeclarationDialog_Caption;
15+
16+
protected override string Message(RefactoringException exception)
17+
{
18+
if (exception is InvalidDeclarationTypeException invalidTypeException)
19+
{
20+
Logger.Warn(invalidTypeException);
21+
return string.Format(
22+
RubberduckUI.RefactoringFailure_AnnotateDeclaration_InvalidType,
23+
invalidTypeException.TargetDeclaration.DeclarationType.ToLocalizedString());
24+
}
25+
26+
return base.Message(exception);
27+
}
28+
}
29+
}

Rubberduck.Core/UI/Command/Refactorings/Notifiers/IntroduceParameterFailedNotifier.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Rubberduck.Parsing.Symbols;
33
using Rubberduck.Refactorings.Exceptions;
44
using Rubberduck.Refactorings.Exceptions.IntroduceParameter;
5+
using Rubberduck.CodeAnalysis.Inspections.Extensions;
56

67
namespace Rubberduck.UI.Command.Refactorings.Notifiers
78
{
@@ -25,8 +26,8 @@ protected override string Message(RefactoringException exception)
2526
Logger.Warn(invalidDeclarationType);
2627
return string.Format(Resources.RubberduckUI.RefactoringFailure_InvalidDeclarationType,
2728
invalidDeclarationType.TargetDeclaration.QualifiedModuleName,
28-
invalidDeclarationType.TargetDeclaration.DeclarationType,
29-
DeclarationType.Variable);
29+
invalidDeclarationType.TargetDeclaration.DeclarationType.ToLocalizedString(),
30+
DeclarationType.Variable.ToLocalizedString());
3031
default:
3132
return base.Message(exception);
3233
}

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ private Type[] RefactoringsMenuItems()
664664
{
665665
return new[]
666666
{
667+
typeof(CodePaneAnnotateDeclarationCommandMenuItem),
667668
typeof(CodePaneRefactorRenameCommandMenuItem),
668669
typeof(RefactorExtractMethodCommandMenuItem),
669670
typeof(RefactorReorderParametersCommandMenuItem),

Rubberduck.Refactorings/AnnotateDeclaration/AnnotateDeclarationRefactoring.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ protected override AnnotateDeclarationModel InitializeModel(Declaration target)
3434
throw new TargetDeclarationIsNullException();
3535
}
3636

37+
var targetType = target.DeclarationType;
38+
39+
if (!targetType.HasFlag(DeclarationType.Member)
40+
&& !targetType.HasFlag(DeclarationType.Module)
41+
&& !targetType.HasFlag(DeclarationType.Variable)
42+
&& !targetType.HasFlag(DeclarationType.Constant))
43+
{
44+
throw new InvalidDeclarationTypeException(target);
45+
}
46+
3747
return new AnnotateDeclarationModel(target);
3848
}
3949

Rubberduck.Resources/Menus/RubberduckMenus.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/Menus/RubberduckMenus.de.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,7 @@
249249
<data name="RefactorMenu_MoveContainingFolder" xml:space="preserve">
250250
<value>Enthaltenden Ordner verschieben</value>
251251
</data>
252+
<data name="RefactorMenu_AnnotateDeclaration" xml:space="preserve">
253+
<value>Annotation hinzufügen</value>
254+
</data>
252255
</root>

0 commit comments

Comments
 (0)