Skip to content

Commit 35d3cd0

Browse files
authored
Merge pull request #1825 from Hosch250/Issue1778
Enhance CE Add commands
2 parents 080a84b + 3056e8e commit 35d3cd0

File tree

5 files changed

+65
-13
lines changed

5 files changed

+65
-13
lines changed

RetailCoder.VBE/UI/CodeExplorer/Commands/CodeExplorer_AddClassModuleCommand.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@ namespace Rubberduck.UI.CodeExplorer.Commands
77
{
88
public class CodeExplorer_AddClassModuleCommand : CommandBase
99
{
10+
private readonly VBE _vbe;
11+
12+
public CodeExplorer_AddClassModuleCommand(VBE vbe)
13+
{
14+
_vbe = vbe;
15+
}
16+
1017
public override bool CanExecute(object parameter)
1118
{
12-
return GetDeclaration(parameter) != null;
19+
return GetDeclaration(parameter) != null || _vbe.VBProjects.Count == 1;
1320
}
1421

1522
public override void Execute(object parameter)
1623
{
17-
GetDeclaration(parameter).Project.VBComponents.Add(vbext_ComponentType.vbext_ct_ClassModule);
24+
if (parameter != null)
25+
{
26+
GetDeclaration(parameter).Project.VBComponents.Add(vbext_ComponentType.vbext_ct_ClassModule);
27+
}
28+
else
29+
{
30+
_vbe.VBProjects.Item(1).VBComponents.Add(vbext_ComponentType.vbext_ct_ClassModule);
31+
}
1832
}
1933

2034
private Declaration GetDeclaration(object parameter)

RetailCoder.VBE/UI/CodeExplorer/Commands/CodeExplorer_AddStdModuleCommand.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@ namespace Rubberduck.UI.CodeExplorer.Commands
77
{
88
public class CodeExplorer_AddStdModuleCommand : CommandBase
99
{
10+
private readonly VBE _vbe;
11+
12+
public CodeExplorer_AddStdModuleCommand(VBE vbe)
13+
{
14+
_vbe = vbe;
15+
}
16+
1017
public override bool CanExecute(object parameter)
1118
{
12-
return GetDeclaration(parameter) != null;
19+
return GetDeclaration(parameter) != null || _vbe.VBProjects.Count == 1;
1320
}
1421

1522
public override void Execute(object parameter)
1623
{
17-
GetDeclaration(parameter).Project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
24+
if (parameter != null)
25+
{
26+
GetDeclaration(parameter).Project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
27+
}
28+
else
29+
{
30+
_vbe.VBProjects.Item(1).VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule);
31+
}
1832
}
1933

2034
private Declaration GetDeclaration(object parameter)

RetailCoder.VBE/UI/CodeExplorer/Commands/CodeExplorer_AddTestModuleCommand.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.Vbe.Interop;
12
using Rubberduck.Navigation.CodeExplorer;
23
using Rubberduck.Parsing.Symbols;
34
using Rubberduck.UI.Command;
@@ -7,21 +8,30 @@ namespace Rubberduck.UI.CodeExplorer.Commands
78
{
89
public class CodeExplorer_AddTestModuleCommand : CommandBase
910
{
11+
private readonly VBE _vbe;
1012
private readonly NewUnitTestModuleCommand _newUnitTestModuleCommand;
1113

12-
public CodeExplorer_AddTestModuleCommand(NewUnitTestModuleCommand newUnitTestModuleCommand)
14+
public CodeExplorer_AddTestModuleCommand(VBE vbe, NewUnitTestModuleCommand newUnitTestModuleCommand)
1315
{
16+
_vbe = vbe;
1417
_newUnitTestModuleCommand = newUnitTestModuleCommand;
1518
}
1619

1720
public override bool CanExecute(object parameter)
1821
{
19-
return GetDeclaration(parameter) != null;
22+
return GetDeclaration(parameter) != null || _vbe.VBProjects.Count == 1;
2023
}
2124

2225
public override void Execute(object parameter)
2326
{
24-
_newUnitTestModuleCommand.NewUnitTestModule(GetDeclaration(parameter).Project);
27+
if (parameter != null)
28+
{
29+
_newUnitTestModuleCommand.NewUnitTestModule(GetDeclaration(parameter).Project);
30+
}
31+
else
32+
{
33+
_newUnitTestModuleCommand.NewUnitTestModule(_vbe.VBProjects.Item(1));
34+
}
2535
}
2636

2737
private Declaration GetDeclaration(object parameter)

RetailCoder.VBE/UI/CodeExplorer/Commands/CodeExplorer_AddUserFormCommand.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@ namespace Rubberduck.UI.CodeExplorer.Commands
77
{
88
public class CodeExplorer_AddUserFormCommand : CommandBase
99
{
10+
private readonly VBE _vbe;
11+
12+
public CodeExplorer_AddUserFormCommand(VBE vbe)
13+
{
14+
_vbe = vbe;
15+
}
16+
1017
public override bool CanExecute(object parameter)
1118
{
12-
return GetDeclaration(parameter) != null;
19+
return GetDeclaration(parameter) != null || _vbe.VBProjects.Count == 1;
1320
}
1421

1522
public override void Execute(object parameter)
1623
{
17-
GetDeclaration(parameter).Project.VBComponents.Add(vbext_ComponentType.vbext_ct_MSForm);
24+
if (parameter != null)
25+
{
26+
GetDeclaration(parameter).Project.VBComponents.Add(vbext_ComponentType.vbext_ct_MSForm);
27+
}
28+
else
29+
{
30+
_vbe.VBProjects.Item(1).VBComponents.Add(vbext_ComponentType.vbext_ct_MSForm);
31+
}
1832
}
1933

2034
private Declaration GetDeclaration(object parameter)

RubberduckTests/CodeExplorer/CodeExplorerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void AddStdModule()
3636
var mockHost = new Mock<IHostApplication>();
3737
mockHost.SetupAllProperties();
3838

39-
var commands = new List<ICommand> { new CodeExplorer_AddStdModuleCommand() };
39+
var commands = new List<ICommand> { new CodeExplorer_AddStdModuleCommand(vbe.Object) };
4040

4141
var state = new RubberduckParserState();
4242
var vm = new CodeExplorerViewModel(new FolderHelper(state, GetDelimiterConfigLoader()), state, commands);
@@ -64,7 +64,7 @@ public void AddClassModule()
6464
var mockHost = new Mock<IHostApplication>();
6565
mockHost.SetupAllProperties();
6666

67-
var commands = new List<ICommand> { new CodeExplorer_AddClassModuleCommand() };
67+
var commands = new List<ICommand> { new CodeExplorer_AddClassModuleCommand(vbe.Object) };
6868

6969
var state = new RubberduckParserState();
7070
var vm = new CodeExplorerViewModel(new FolderHelper(state, GetDelimiterConfigLoader()), state, commands);
@@ -92,7 +92,7 @@ public void AddUserForm()
9292
var mockHost = new Mock<IHostApplication>();
9393
mockHost.SetupAllProperties();
9494

95-
var commands = new List<ICommand> { new CodeExplorer_AddUserFormCommand() };
95+
var commands = new List<ICommand> { new CodeExplorer_AddUserFormCommand(vbe.Object) };
9696

9797
var state = new RubberduckParserState();
9898
var vm = new CodeExplorerViewModel(new FolderHelper(state, GetDelimiterConfigLoader()), state, commands);
@@ -126,7 +126,7 @@ public void AddTestModule()
126126
var state = new RubberduckParserState();
127127
var commands = new List<ICommand>
128128
{
129-
new CodeExplorer_AddTestModuleCommand(new NewUnitTestModuleCommand(state, configLoader.Object))
129+
new CodeExplorer_AddTestModuleCommand(vbe.Object, new NewUnitTestModuleCommand(state, configLoader.Object))
130130
};
131131

132132
var vm = new CodeExplorerViewModel(new FolderHelper(state, GetDelimiterConfigLoader()), state, commands);

0 commit comments

Comments
 (0)