Skip to content

Commit 5736321

Browse files
committed
implemented CanExecute for all refactoring commands; made corresponding CommandMenuItems call Command.CanExecute - result is a smart context menu that knows what refactorings to enable depending on current selection
1 parent ec9f680 commit 5736321

18 files changed

+259
-46
lines changed

RetailCoder.VBE/UI/Command/MenuItems/RefactorEncapsulateFieldCommandMenuItem.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System.Drawing;
21
using System.Windows.Input;
32
using Rubberduck.Parsing.VBA;
4-
using Rubberduck.Properties;
53
using Rubberduck.UI.Command.MenuItems.ParentMenus;
64

75
namespace Rubberduck.UI.Command.MenuItems
@@ -20,7 +18,7 @@ public RefactorEncapsulateFieldCommandMenuItem(ICommand command)
2018

2119
public override bool EvaluateCanExecute(RubberduckParserState state)
2220
{
23-
return state.Status == ParserState.Ready;
21+
return Command.CanExecute(null);
2422
}
2523
}
2624
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorExtractInterfaceCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public RefactorExtractInterfaceCommandMenuItem(ICommand command)
2020

2121
public override bool EvaluateCanExecute(RubberduckParserState state)
2222
{
23-
return state.Status == ParserState.Ready;
23+
return Command.CanExecute(null);
2424
}
2525
}
2626
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorExtractMethodCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override bool BeginGroup
2626

2727
public override bool EvaluateCanExecute(RubberduckParserState state)
2828
{
29-
return state.Status == ParserState.Ready;
29+
return Command.CanExecute(null);
3030
}
3131
}
3232
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorImplementInterfaceCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public RefactorImplementInterfaceCommandMenuItem(ICommand command)
2020

2121
public override bool EvaluateCanExecute(RubberduckParserState state)
2222
{
23-
return state.Status == ParserState.Ready;
23+
return Command.CanExecute(null);
2424
}
2525
}
2626
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorIntroduceFieldCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RefactorIntroduceFieldCommandMenuItem (ICommand command)
2222

2323
public override bool EvaluateCanExecute(RubberduckParserState state)
2424
{
25-
return state.Status == ParserState.Ready;
25+
return Command.CanExecute(null);
2626
}
2727
}
2828
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorIntroduceParameterCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RefactorIntroduceParameterCommandMenuItem (ICommand command)
2222

2323
public override bool EvaluateCanExecute(RubberduckParserState state)
2424
{
25-
return state.Status == ParserState.Ready;
25+
return Command.CanExecute(null);
2626
}
2727
}
2828
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorMoveCloserToUsageCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public RefactorMoveCloserToUsageCommandMenuItem(ICommand command)
1717

1818
public override bool EvaluateCanExecute(RubberduckParserState state)
1919
{
20-
return state.Status == ParserState.Ready;
20+
return Command.CanExecute(null);
2121
}
2222
}
2323
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorRemoveParametersCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public RefactorRemoveParametersCommandMenuItem(ICommand command) : base(command)
2121

2222
public override bool EvaluateCanExecute(RubberduckParserState state)
2323
{
24-
return state.Status == ParserState.Ready;
24+
return Command.CanExecute(null);
2525
}
2626
}
2727
}

RetailCoder.VBE/UI/Command/MenuItems/RefactorReorderParametersCommandMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public RefactorReorderParametersCommandMenuItem(ICommand command) : base(command
1919

2020
public override bool EvaluateCanExecute(RubberduckParserState state)
2121
{
22-
return state.Status == ParserState.Ready;
22+
return Command.CanExecute(null);
2323
}
2424
}
2525
}

RetailCoder.VBE/UI/Command/Refactorings/RefactorEncapsulateFieldCommand.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
using Microsoft.Vbe.Interop;
1+
using System.Diagnostics;
2+
using Microsoft.Vbe.Interop;
23
using System.Runtime.InteropServices;
4+
using Rubberduck.Common;
5+
using Rubberduck.Parsing.Symbols;
36
using Rubberduck.Parsing.VBA;
47
using Rubberduck.Refactorings.EncapsulateField;
58
using Rubberduck.UI.Refactorings;
69
using Rubberduck.VBEditor;
10+
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
711

812
namespace Rubberduck.UI.Command.Refactorings
913
{
@@ -18,6 +22,23 @@ public RefactorEncapsulateFieldCommand(VBE vbe, RubberduckParserState state, IAc
1822
_state = state;
1923
}
2024

25+
public override bool CanExecute(object parameter)
26+
{
27+
var pane = Vbe.ActiveCodePane;
28+
if (pane == null || _state.Status != ParserState.Ready)
29+
{
30+
return false;
31+
}
32+
33+
var selection = pane.GetSelection();
34+
var target = _state.AllUserDeclarations.FindTarget(selection, new[] {DeclarationType.Variable,});
35+
36+
var canExecute = target != null && !target.ParentScopeDeclaration.DeclarationType.HasFlag(DeclarationType.Member);
37+
38+
Debug.WriteLine("{0}.CanExecute evaluates to {1}", GetType().Name, canExecute);
39+
return canExecute;
40+
}
41+
2142
public override void Execute(object parameter)
2243
{
2344
if (Vbe.ActiveCodePane == null)

0 commit comments

Comments
 (0)