Skip to content

Commit d0c69ae

Browse files
authored
Merge pull request #4335 from mansellan/4305
VB6: Set as start up project
2 parents dc10dab + 34b12f4 commit d0c69ae

File tree

15 files changed

+182
-16
lines changed

15 files changed

+182
-16
lines changed

Rubberduck.Core/CodeAnalysis/CodeMetrics/CodeMetricsViewModel.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Rubberduck.Navigation.CodeExplorer;
99
using System.Windows;
1010
using Rubberduck.Navigation.Folders;
11+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
1112

1213
namespace Rubberduck.CodeAnalysis.CodeMetrics
1314
{
@@ -16,13 +17,15 @@ public class CodeMetricsViewModel : ViewModelBase, IDisposable
1617
private readonly RubberduckParserState _state;
1718
private readonly ICodeMetricsAnalyst _analyst;
1819
private readonly FolderHelper _folderHelper;
20+
private readonly IVBE _vbe;
1921

20-
public CodeMetricsViewModel(RubberduckParserState state, ICodeMetricsAnalyst analyst, FolderHelper folderHelper)
22+
public CodeMetricsViewModel(RubberduckParserState state, ICodeMetricsAnalyst analyst, FolderHelper folderHelper, IVBE vbe)
2123
{
2224
_state = state;
2325
_analyst = analyst;
2426
_folderHelper = folderHelper;
2527
_state.StateChanged += OnStateChanged;
28+
_vbe = vbe;
2629
}
2730

2831
private void OnStateChanged(object sender, ParserStateEventArgs e)
@@ -71,7 +74,8 @@ private void UpdateData()
7174
var newProjects = userDeclarations.Select(grouping =>
7275
new CodeExplorerProjectViewModel(_folderHelper,
7376
grouping.SingleOrDefault(declaration => declaration.DeclarationType == DeclarationType.Project),
74-
grouping)).ToList();
77+
grouping,
78+
_vbe)).ToList();
7579

7680
UpdateNodes(Projects, newProjects);
7781

Rubberduck.Core/Navigation/CodeExplorer/CodeExplorerItemViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.Windows;
34
using System.Windows.Media.Imaging;
45
using Rubberduck.Parsing.Symbols;
56
using Rubberduck.UI;
@@ -203,6 +204,8 @@ public bool IsVisible
203204
public abstract BitmapImage ExpandedIcon { get; }
204205
public abstract CodeExplorerItemViewModel Parent { get; }
205206

207+
public virtual FontWeight FontWeight => FontWeights.Normal;
208+
206209
public abstract QualifiedSelection? QualifiedSelection { get; }
207210

208211
public CodeExplorerItemViewModel GetChild(string name)

Rubberduck.Core/Navigation/CodeExplorer/CodeExplorerProjectViewModel.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Windows;
45
using System.Windows.Media.Imaging;
56
using Rubberduck.Navigation.Folders;
67
using Rubberduck.Parsing.Symbols;
78
using Rubberduck.VBEditor;
89
using Rubberduck.VBEditor.SafeComWrappers;
10+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
911
using resx = Rubberduck.Resources.CodeExplorer.CodeExplorerUI;
1012

1113
namespace Rubberduck.Navigation.CodeExplorer
@@ -15,6 +17,7 @@ public class CodeExplorerProjectViewModel : CodeExplorerItemViewModel, ICodeExpl
1517
public Declaration Declaration { get; }
1618

1719
private readonly CodeExplorerCustomFolderViewModel _folderTree;
20+
private readonly IVBE _vbe;
1821

1922
private static readonly DeclarationType[] ComponentTypes =
2023
{
@@ -24,12 +27,13 @@ public class CodeExplorerProjectViewModel : CodeExplorerItemViewModel, ICodeExpl
2427
DeclarationType.UserForm,
2528
};
2629

27-
public CodeExplorerProjectViewModel(FolderHelper folderHelper, Declaration declaration, IEnumerable<Declaration> declarations)
30+
public CodeExplorerProjectViewModel(FolderHelper folderHelper, Declaration declaration, IEnumerable<Declaration> declarations, IVBE vbe)
2831
{
2932
Declaration = declaration;
3033
_name = Declaration.IdentifierName;
3134
IsExpanded = true;
3235
_folderTree = folderHelper.GetFolderTree(declaration);
36+
_vbe = vbe;
3337

3438
try
3539
{
@@ -95,6 +99,27 @@ private bool CanAddNodesToTree(CodeExplorerCustomFolderViewModel tree, List<Decl
9599
public override BitmapImage CollapsedIcon => _icon;
96100
public override BitmapImage ExpandedIcon => _icon;
97101

102+
public override FontWeight FontWeight
103+
{
104+
get
105+
{
106+
if (_vbe.Kind == VBEKind.Hosted || Declaration.Project == null)
107+
{
108+
return base.FontWeight;
109+
}
110+
111+
using (var vbProjects = _vbe.VBProjects)
112+
{
113+
if (Declaration.Project.Equals(vbProjects.StartProject))
114+
{
115+
return FontWeights.Bold;
116+
}
117+
118+
return base.FontWeight;
119+
}
120+
}
121+
}
122+
98123
// projects are always at the top of the tree
99124
public override CodeExplorerItemViewModel Parent => null;
100125

Rubberduck.Core/Navigation/CodeExplorer/CodeExplorerViewModel.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public sealed class CodeExplorerViewModel : ViewModelBase, IDisposable
3434
private readonly GeneralSettings _generalSettings;
3535
private readonly WindowSettings _windowSettings;
3636
private readonly IUiDispatcher _uiDispatcher;
37-
private readonly VBEKind _vbeKind;
37+
private readonly IVBE _vbe;
3838

3939
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
4040

@@ -53,7 +53,7 @@ public CodeExplorerViewModel(
5353
_state.ModuleStateChanged += ParserState_ModuleStateChanged;
5454
_windowSettingsProvider = windowSettingsProvider;
5555
_uiDispatcher = uiDispatcher;
56-
_vbeKind = vbe.Kind;
56+
_vbe = vbe;
5757

5858
if (generalSettingsProvider != null)
5959
{
@@ -84,8 +84,9 @@ public CodeExplorerViewModel(
8484
AddPropertyPageCommand = commands.OfType<AddPropertyPageCommand>().SingleOrDefault();
8585
AddUserDocumentCommand = commands.OfType<AddUserDocumentCommand>().SingleOrDefault();
8686
AddTestModuleCommand = commands.OfType<UI.CodeExplorer.Commands.AddTestModuleCommand>().SingleOrDefault();
87-
AddTestModuleWithStubsCommand = commands.OfType<AddTestModuleWithStubsCommand>().SingleOrDefault();
87+
AddTestModuleWithStubsCommand = commands.OfType<AddTestModuleWithStubsCommand>().SingleOrDefault();
8888

89+
SetAsStartupProjectCommand = commands.OfType<SetAsStartupProjectCommand>().SingleOrDefault();
8990
OpenProjectPropertiesCommand = commands.OfType<OpenProjectPropertiesCommand>().SingleOrDefault();
9091
RenameCommand = commands.OfType<RenameCommand>().SingleOrDefault();
9192
IndenterCommand = commands.OfType<IndentCommand>().SingleOrDefault();
@@ -334,7 +335,8 @@ private void HandleStateChanged(object sender, ParserStateEventArgs e)
334335
var newProjects = userDeclarations.Select(grouping =>
335336
new CodeExplorerProjectViewModel(_folderHelper,
336337
grouping.SingleOrDefault(declaration => declaration.DeclarationType == DeclarationType.Project),
337-
grouping)).ToList();
338+
grouping,
339+
_vbe)).ToList();
338340

339341
UpdateNodes(Projects, newProjects);
340342

@@ -532,6 +534,7 @@ private void SwitchNodeState(CodeExplorerItemViewModel node, bool expandedState)
532534
public CommandBase AddTestModuleWithStubsCommand { get; }
533535

534536
public CommandBase OpenDesignerCommand { get; }
537+
public CommandBase SetAsStartupProjectCommand { get; }
535538
public CommandBase OpenProjectPropertiesCommand { get; }
536539

537540
public CommandBase RenameCommand { get; }
@@ -566,17 +569,17 @@ private void ExecuteRemoveComand(object param)
566569

567570
private bool CanExecuteExportAllCommand => ExportAllCommand.CanExecute(SelectedItem);
568571

569-
public Visibility ExportVisibility => _vbeKind == VBEKind.Standalone || CanExecuteExportAllCommand ? Visibility.Collapsed : Visibility.Visible;
572+
public Visibility ExportVisibility => _vbe.Kind == VBEKind.Standalone || CanExecuteExportAllCommand ? Visibility.Collapsed : Visibility.Visible;
570573

571574
public Visibility ExportAllVisibility => CanExecuteExportAllCommand ? Visibility.Visible : Visibility.Collapsed;
572575

573576
public Visibility TreeViewVisibility => Projects == null || Projects.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
574577

575578
public Visibility EmptyUIRefreshMessageVisibility => _isBusy ? Visibility.Hidden : Visibility.Visible;
576579

577-
public Visibility VB6Visibility => _vbeKind == VBEKind.Standalone ? Visibility.Visible : Visibility.Collapsed;
580+
public Visibility VB6Visibility => _vbe.Kind == VBEKind.Standalone ? Visibility.Visible : Visibility.Collapsed;
578581

579-
public Visibility VBAVisibility => _vbeKind == VBEKind.Hosted ? Visibility.Visible : Visibility.Collapsed;
582+
public Visibility VBAVisibility => _vbe.Kind == VBEKind.Hosted ? Visibility.Visible : Visibility.Collapsed;
580583

581584
public void FilterByName(IEnumerable<CodeExplorerItemViewModel> nodes, string searchString)
582585
{

Rubberduck.Core/Rubberduck.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@
391391
<Compile Include="UI\CodeExplorer\Commands\CodeExplorerCommandAttribute.cs" />
392392
<Compile Include="UI\CodeExplorer\Commands\AddUserFormCommand.cs" />
393393
<Compile Include="UI\CodeExplorer\Commands\CopyResultsCommand.cs" />
394+
<Compile Include="UI\CodeExplorer\Commands\SetAsStartupProjectCommand.cs" />
394395
<Compile Include="UI\CodeMetrics\CodeMetricsWindow.cs">
395396
<SubType>UserControl</SubType>
396397
</Compile>

Rubberduck.Core/UI/CodeExplorer/CodeExplorerControl.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<Style x:Key="TreeViewItemStyle" TargetType="TextBlock">
8989
<Setter Property="Text" Value="{Binding Name}" />
9090
<Setter Property="FontSize" Value="10" />
91+
<Setter Property="FontWeight" Value="{Binding FontWeight}"/>
9192
<Setter Property="Margin" Value="2,0,2,0" />
9293
<Setter Property="VerticalAlignment" Value="Center" />
9394
<Setter Property="ToolTip" Value="{Binding Name}" />
@@ -104,6 +105,7 @@
104105
<Style x:Key="TreeViewItemStyleWithSignatures" TargetType="TextBlock">
105106
<Setter Property="Text" Value="{Binding NameWithSignature}" />
106107
<Setter Property="FontSize" Value="10" />
108+
<Setter Property="FontWeight" Value="{Binding FontWeight}"/>
107109
<Setter Property="Margin" Value="2,0,2,0" />
108110
<Setter Property="VerticalAlignment" Value="Center" />
109111
<Setter Property="ToolTip" Value="{Binding NameWithSignature}" />
@@ -166,6 +168,10 @@
166168
Command="{Binding RenameCommand}"
167169
CommandParameter="{Binding SelectedItem}" />
168170
<Separator />
171+
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.CodeExplorer.CodeExplorerUI, Key=CodeExplorer_SetAsStartupProject}"
172+
Command="{Binding SetAsStartupProjectCommand}"
173+
CommandParameter="{Binding SelectedItem}"
174+
Visibility="{Binding VB6Visibility}"/>
169175
<MenuItem Header="{Resx ResxName=Rubberduck.Resources.CodeExplorer.CodeExplorerUI, Key=CodeExplorer_OpenProjectProperties}"
170176
Command="{Binding OpenProjectPropertiesCommand}"
171177
CommandParameter="{Binding SelectedItem}" />

Rubberduck.Core/UI/CodeExplorer/Commands/AddComponentCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public bool CanAddComponent(CodeExplorerItemViewModel parameter, IEnumerable<Pro
2525

2626
if (project == null && _vbe.ProjectsCount == 1)
2727
{
28-
project = _vbe.VBProjects[1];
28+
using (var vbProjects = _vbe.VBProjects)
29+
using (project = vbProjects[1])
30+
{
31+
return project != null && allowableProjectTypes.Contains(project.Type);
32+
}
2933
}
3034

3135
return project != null && allowableProjectTypes.Contains(project.Type);

Rubberduck.Core/UI/CodeExplorer/Commands/AddMDIFormCommand.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,18 @@ protected override bool EvaluateCanExecute(object parameter)
2929

3030
if (project == null && _vbe.ProjectsCount == 1)
3131
{
32-
project = _vbe.VBProjects[1];
32+
using (var vbProjects = _vbe.VBProjects)
33+
using (project = vbProjects[1])
34+
{
35+
return EvaluateCanExecuteCore(project, parameter as CodeExplorerItemViewModel);
36+
}
3337
}
3438

39+
return EvaluateCanExecuteCore(project, parameter as CodeExplorerItemViewModel);
40+
}
41+
42+
private bool EvaluateCanExecuteCore(IVBProject project, CodeExplorerItemViewModel itemViewModel)
43+
{
3544
if (project == null)
3645
{
3746
return false;
@@ -46,10 +55,10 @@ protected override bool EvaluateCanExecute(object parameter)
4655
// Only one MDI Form allowed per project
4756
return false;
4857
}
49-
}
50-
}
58+
}
59+
}
5160

52-
return _addComponentCommand.CanAddComponent(parameter as CodeExplorerItemViewModel, new[] { ProjectType.StandardExe, ProjectType.ActiveXExe });
61+
return _addComponentCommand.CanAddComponent(itemViewModel, new[] {ProjectType.StandardExe, ProjectType.ActiveXExe});
5362
}
5463

5564
protected override void OnExecute(object parameter)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Runtime.InteropServices;
2+
using NLog;
3+
using Rubberduck.Navigation.CodeExplorer;
4+
using Rubberduck.Parsing.Symbols;
5+
using Rubberduck.Parsing.VBA;
6+
using Rubberduck.UI.Command;
7+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
8+
9+
namespace Rubberduck.UI.CodeExplorer.Commands
10+
{
11+
[CodeExplorerCommand]
12+
public class SetAsStartupProjectCommand : CommandBase
13+
{
14+
private readonly IVBE _vbe;
15+
private readonly RubberduckParserState _parserState;
16+
17+
public SetAsStartupProjectCommand(IVBE vbe, RubberduckParserState parserState)
18+
: base(LogManager.GetCurrentClassLogger())
19+
{
20+
_vbe = vbe;
21+
_parserState = parserState;
22+
}
23+
24+
protected override bool EvaluateCanExecute(object parameter)
25+
{
26+
try
27+
{
28+
if (_vbe.ProjectsCount <= 1)
29+
{
30+
return false;
31+
}
32+
33+
var project = GetDeclaration(parameter as CodeExplorerItemViewModel)?.Project;
34+
35+
using (var vbProjects = _vbe.VBProjects)
36+
{
37+
return project != null && !project.Equals(vbProjects.StartProject);
38+
}
39+
}
40+
catch (COMException exception)
41+
{
42+
Logger.Error(exception);
43+
return false;
44+
}
45+
}
46+
47+
protected override void OnExecute(object parameter)
48+
{
49+
try
50+
{
51+
if (_vbe.ProjectsCount <= 1)
52+
{
53+
return;
54+
}
55+
56+
var project = GetDeclaration(parameter as CodeExplorerItemViewModel)?.Project;
57+
58+
using (var vbProjects = _vbe.VBProjects)
59+
{
60+
if (!vbProjects.StartProject.Equals(project))
61+
{
62+
vbProjects.StartProject = project;
63+
_parserState.OnParseRequested(this);
64+
}
65+
}
66+
}
67+
catch (COMException exception)
68+
{
69+
Logger.Error(exception);
70+
}
71+
}
72+
73+
private Declaration GetDeclaration(CodeExplorerItemViewModel node)
74+
{
75+
while (node != null && !(node is ICodeExplorerDeclarationViewModel))
76+
{
77+
node = node.Parent;
78+
}
79+
80+
return (node as ICodeExplorerDeclarationViewModel)?.Declaration;
81+
}
82+
}
83+
}

Rubberduck.Resources/CodeExplorer/CodeExplorerUI.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.

0 commit comments

Comments
 (0)