Skip to content

Commit a43964e

Browse files
committed
Merge branch 'next' of https://github.com/rubberduck-vba/Rubberduck into SearchBox
2 parents 1da6bd2 + 72a53d3 commit a43964e

22 files changed

+392
-146
lines changed

RetailCoder.VBE/API/VBA/Declaration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ internal Declaration(RubberduckDeclaration declaration)
5656
{ Parsing.Symbols.DeclarationType.Control, DeclarationType.Control },
5757
{ Parsing.Symbols.DeclarationType.UserForm, DeclarationType.UserForm },
5858
{ Parsing.Symbols.DeclarationType.Document, DeclarationType.Document },
59-
{ Parsing.Symbols.DeclarationType.ModuleOption, DeclarationType.ModuleOption },
6059
{ Parsing.Symbols.DeclarationType.Procedure, DeclarationType.Procedure },
6160
{ Parsing.Symbols.DeclarationType.Function, DeclarationType.Function },
6261
{ Parsing.Symbols.DeclarationType.PropertyGet, DeclarationType.PropertyGet },

RetailCoder.VBE/API/VBA/DeclarationType.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public enum DeclarationType
1212
Control, //= 1 << 3,
1313
UserForm,// = 1 << 4,
1414
Document,// = 1 << 5,
15-
ModuleOption,// = 1 << 6,
1615
Procedure, //= 1 << 8,
1716
Function,// = 1 << 9,
1817
PropertyGet,// = 1 << 11,

RetailCoder.VBE/Navigation/CodeExplorer/CodeExplorerViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public CodeExplorerViewModel(FolderHelper folderHelper, RubberduckParserState st
6464
OpenDesignerCommand = commands.OfType<OpenDesignerCommand>().SingleOrDefault();
6565

6666
AddTestModuleCommand = commands.OfType<UI.CodeExplorer.Commands.AddTestModuleCommand>().SingleOrDefault();
67+
AddTestModuleWithStubsCommand = commands.OfType<AddTestModuleWithStubsCommand>().SingleOrDefault();
68+
6769
AddStdModuleCommand = commands.OfType<AddStdModuleCommand>().SingleOrDefault();
6870
AddClassModuleCommand = commands.OfType<AddClassModuleCommand>().SingleOrDefault();
6971
AddUserFormCommand = commands.OfType<AddUserFormCommand>().SingleOrDefault();
@@ -491,6 +493,7 @@ private void SwitchNodeState(CodeExplorerItemViewModel node, bool expandedState)
491493
public CommandBase OpenCommand { get; }
492494

493495
public CommandBase AddTestModuleCommand { get; }
496+
public CommandBase AddTestModuleWithStubsCommand { get; }
494497
public CommandBase AddStdModuleCommand { get; }
495498
public CommandBase AddClassModuleCommand { get; }
496499
public CommandBase AddUserFormCommand { get; }

RetailCoder.VBE/Refactorings/Rename/RenameModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public RenameModel(IVBE vbe, RubberduckParserState state, QualifiedSelection sel
4040
private void AcquireTarget(out Declaration target, QualifiedSelection selection)
4141
{
4242
target = _declarations
43-
.Where(item => item.IsUserDefined && item.DeclarationType != DeclarationType.ModuleOption)
43+
.Where(item => item.IsUserDefined)
4444
.FirstOrDefault(item => item.IsSelected(selection) || item.References.Any(r => r.IsSelected(selection)));
4545
}
4646
}

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@
400400
<Compile Include="UI\About\AboutDialog.Designer.cs">
401401
<DependentUpon>AboutDialog.cs</DependentUpon>
402402
</Compile>
403+
<Compile Include="UI\CodeExplorer\Commands\AddTestModuleWithStubsCommand.cs" />
403404
<Compile Include="UI\CodeExplorer\Commands\CodeExplorerCommandAttribute.cs" />
404405
<Compile Include="UI\CodeExplorer\Commands\CommitCommand.cs" />
405406
<Compile Include="UI\CodeExplorer\Commands\AddUserFormCommand.cs" />

RetailCoder.VBE/Settings/CodeInspectionSettings.cs

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ public CodeInspectionSetting GetSetting<TInspection>() where TInspection : IInsp
4343
?? GetSetting(typeof(TInspection));
4444
}
4545

46-
public CodeInspectionSetting GetSetting(Type inspection)
46+
public CodeInspectionSetting GetSetting(Type inspectionType)
4747
{
4848
try
4949
{
50-
var proto = Convert.ChangeType(Activator.CreateInstance(inspection), inspection);
51-
var existing = CodeInspections.FirstOrDefault(s => proto.GetType().ToString().Equals(s.Name));
52-
if (existing != null) return existing;
50+
var existing = CodeInspections.FirstOrDefault(s => inspectionType.ToString().Equals(s.Name));
51+
if (existing != null)
52+
{
53+
return existing;
54+
}
55+
var proto = Convert.ChangeType(Activator.CreateInstance(inspectionType), inspectionType);
5356
var setting = new CodeInspectionSetting(proto as IInspectionModel);
5457
CodeInspections.Add(setting);
5558
return setting;
@@ -80,37 +83,15 @@ public class CodeInspectionSetting : IInspectionModel
8083
[XmlIgnore]
8184
public string Description
8285
{
83-
get
84-
{
85-
if (_description == null)
86-
{
87-
_description = InspectionsUI.ResourceManager.GetString(Name + "Name");
88-
}
89-
return _description;
90-
}
91-
set
92-
{
93-
_description = value;
94-
}
86+
get => _description ?? (_description = InspectionsUI.ResourceManager.GetString(Name + "Name"));
87+
set => _description = value;
9588
}// not serialized because culture-dependent
9689

9790
[XmlIgnore]
98-
public string LocalizedName
99-
{
100-
get
101-
{
102-
return InspectionsUI.ResourceManager.GetString(Name + "Name", CultureInfo.CurrentUICulture);
103-
}
104-
} // not serialized because culture-dependent
91+
public string LocalizedName => InspectionsUI.ResourceManager.GetString(Name + "Name", CultureInfo.CurrentUICulture); // not serialized because culture-dependent
10592

10693
[XmlIgnore]
107-
public string AnnotationName
108-
{
109-
get
110-
{
111-
return Name.Replace("Inspection", string.Empty);
112-
}
113-
}
94+
public string AnnotationName => Name.Replace("Inspection", string.Empty);
11495

11596
[XmlIgnore]
11697
public CodeInspectionSeverity DefaultSeverity { get; private set; }
@@ -119,13 +100,7 @@ public string AnnotationName
119100
public CodeInspectionSeverity Severity { get; set; }
120101

121102
[XmlIgnore]
122-
public string Meta
123-
{
124-
get
125-
{
126-
return InspectionsUI.ResourceManager.GetString(Name + "Meta", CultureInfo.CurrentUICulture);
127-
}
128-
}
103+
public string Meta => InspectionsUI.ResourceManager.GetString(Name + "Meta", CultureInfo.CurrentUICulture);
129104

130105
[XmlIgnore]
131106
// ReSharper disable once UnusedMember.Global; used in string literal to define collection groupings
@@ -134,7 +109,7 @@ public string Meta
134109
[XmlIgnore]
135110
public string SeverityLabel
136111
{
137-
get { return InspectionsUI.ResourceManager.GetString("CodeInspectionSeverity_" + Severity, CultureInfo.CurrentUICulture); }
112+
get => InspectionsUI.ResourceManager.GetString("CodeInspectionSeverity_" + Severity, CultureInfo.CurrentUICulture);
138113
set
139114
{
140115
foreach (var severity in Enum.GetValues(typeof(CodeInspectionSeverity)))

RetailCoder.VBE/UI/CodeExplorer/CodeExplorerControl.xaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@
163163
<Image Height="16" Source="{StaticResource AddTestModuleImage}" />
164164
</MenuItem.Icon>
165165
</MenuItem>
166+
<MenuItem Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=CodeExplorer_AddTestModuleWithStubsText}"
167+
Command="{Binding AddTestModuleWithStubsCommand}"
168+
CommandParameter="{Binding SelectedItem}">
169+
<MenuItem.Icon>
170+
<Image Height="16" Source="{StaticResource AddTestModuleImage}" />
171+
</MenuItem.Icon>
172+
</MenuItem>
166173
<Separator />
167174
<MenuItem Header="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=CodeExplorer_AddStdModuleText}"
168175
Command="{Binding AddStdModuleCommand}"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using NLog;
2+
using Rubberduck.Navigation.CodeExplorer;
3+
using Rubberduck.Parsing.Symbols;
4+
using Rubberduck.UI.Command;
5+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
6+
7+
namespace Rubberduck.UI.CodeExplorer.Commands
8+
{
9+
[CodeExplorerCommand]
10+
public class AddTestModuleWithStubsCommand : CommandBase
11+
{
12+
private readonly IVBE _vbe;
13+
private readonly Command.AddTestModuleCommand _newUnitTestModuleCommand;
14+
15+
public AddTestModuleWithStubsCommand(IVBE vbe, Command.AddTestModuleCommand newUnitTestModuleCommand) : base(LogManager.GetCurrentClassLogger())
16+
{
17+
_vbe = vbe;
18+
_newUnitTestModuleCommand = newUnitTestModuleCommand;
19+
}
20+
21+
protected override bool EvaluateCanExecute(object parameter) => parameter is CodeExplorerComponentViewModel;
22+
23+
protected override void OnExecute(object parameter)
24+
{
25+
if (parameter != null)
26+
{
27+
_newUnitTestModuleCommand.Execute(GetDeclaration(parameter));
28+
}
29+
else
30+
{
31+
_newUnitTestModuleCommand.Execute(_vbe.ActiveVBProject);
32+
}
33+
}
34+
35+
private Declaration GetDeclaration(object parameter)
36+
{
37+
var node = parameter as CodeExplorerItemViewModel;
38+
while (node != null && !(node is ICodeExplorerDeclarationViewModel))
39+
{
40+
node = node.Parent;
41+
}
42+
43+
return ((ICodeExplorerDeclarationViewModel)node)?.Declaration;
44+
}
45+
}
46+
}

RetailCoder.VBE/UI/Command/AddTestModuleCommand.cs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using System.Linq;
23
using System.Runtime.InteropServices;
34
using NLog;
@@ -9,6 +10,7 @@
910
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
1011
using Rubberduck.VBEditor.SafeComWrappers.VBA;
1112
using System.Text;
13+
using Rubberduck.Parsing.Symbols;
1214

1315
namespace Rubberduck.UI.Command
1416
{
@@ -138,7 +140,11 @@ protected override bool EvaluateCanExecute(object parameter)
138140

139141
protected override void OnExecute(object parameter)
140142
{
141-
var project = parameter as IVBProject ?? GetProject();
143+
var parameterIsModuleDeclaration = parameter is ProceduralModuleDeclaration || parameter is ClassModuleDeclaration;
144+
145+
var project = parameter as IVBProject ??
146+
(parameterIsModuleDeclaration ? ((Declaration) parameter).Project : GetProject());
147+
142148
if (project.IsWrappingNullReference)
143149
{
144150
return;
@@ -164,14 +170,47 @@ protected override void OnExecute(object parameter)
164170
var options = string.Concat(hasOptionExplicit ? string.Empty : "Option Explicit\r\n",
165171
"Option Private Module\r\n\r\n");
166172

167-
var defaultTestMethod = string.Empty;
168-
if (settings.DefaultTestStubInNewModule)
173+
if (parameterIsModuleDeclaration)
174+
{
175+
var moduleCodeBuilder = new StringBuilder();
176+
var declarationsToStub = GetDeclarationsToStub((Declaration)parameter);
177+
178+
foreach (var declaration in declarationsToStub)
179+
{
180+
var name = string.Empty;
181+
182+
switch (declaration.DeclarationType)
183+
{
184+
case DeclarationType.Procedure:
185+
case DeclarationType.Function:
186+
name = declaration.IdentifierName;
187+
break;
188+
case DeclarationType.PropertyGet:
189+
name = $"Get{declaration.IdentifierName}";
190+
break;
191+
case DeclarationType.PropertyLet:
192+
name = $"Let{declaration.IdentifierName}";
193+
break;
194+
case DeclarationType.PropertySet:
195+
name = $"Set{declaration.IdentifierName}";
196+
break;
197+
}
198+
199+
var stub = AddTestMethodCommand.TestMethodTemplate.Replace(AddTestMethodCommand.NamePlaceholder, $"{name}_Test");
200+
moduleCodeBuilder.AppendLine(stub);
201+
}
202+
203+
module.AddFromString(options + GetTestModule(settings) + moduleCodeBuilder);
204+
}
205+
else
169206
{
170-
defaultTestMethod = AddTestMethodCommand.TestMethodTemplate.Replace(
171-
AddTestMethodCommand.NamePlaceholder, "TestMethod1");
207+
var defaultTestMethod = settings.DefaultTestStubInNewModule
208+
? AddTestMethodCommand.TestMethodTemplate.Replace(AddTestMethodCommand.NamePlaceholder, "TestMethod1")
209+
: string.Empty;
210+
211+
module.AddFromString(options + GetTestModule(settings) + defaultTestMethod);
172212
}
173213

174-
module.AddFromString(options + GetTestModule(settings) + defaultTestMethod);
175214
component.Activate();
176215
_state.OnParseRequested(this, component);
177216
}
@@ -183,5 +222,13 @@ private string GetNextTestModuleName(IVBProject project)
183222

184223
return string.Concat(TestModuleBaseName, index);
185224
}
225+
226+
private IEnumerable<Declaration> GetDeclarationsToStub(Declaration parentDeclaration)
227+
{
228+
return _state.DeclarationFinder.Members(parentDeclaration)
229+
.Where(d => Equals(d.ParentDeclaration, parentDeclaration) && d.Accessibility == Accessibility.Public &&
230+
(d.DeclarationType == DeclarationType.Procedure || d.DeclarationType == DeclarationType.Function || d.DeclarationType.HasFlag(DeclarationType.Property)))
231+
.OrderBy(d => d.Context.Start.TokenIndex);
232+
}
186233
}
187234
}

RetailCoder.VBE/UI/FindSymbol/FindSymbolViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ public class FindSymbolViewModel : INotifyPropertyChanged
1414
{
1515
private static readonly DeclarationType[] ExcludedTypes =
1616
{
17-
DeclarationType.Control,
18-
DeclarationType.ModuleOption,
17+
DeclarationType.Control,
1918
DeclarationType.Project
2019
};
2120

0 commit comments

Comments
 (0)