Skip to content

Commit 6cba3a3

Browse files
committed
Merge branch 'next' of https://github.com/rubberduck-vba/rubberduck into next
2 parents b0cead9 + bed752c commit 6cba3a3

File tree

11 files changed

+269
-117
lines changed

11 files changed

+269
-117
lines changed

RetailCoder.VBE/Navigation/CodeExplorer/CodeExplorerViewModel.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public sealed class CodeExplorerViewModel : ViewModelBase, IDisposable
3535
private GeneralSettings _generalSettings;
3636
private WindowSettings _windowSettings;
3737

38+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
39+
3840
public CodeExplorerViewModel(FolderHelper folderHelper, RubberduckParserState state, List<CommandBase> commands,
3941
IConfigProvider<GeneralSettings> generalSettingsProvider, IConfigProvider<WindowSettings> windowSettingsProvider)
4042
{
@@ -395,22 +397,30 @@ private void ParserState_ModuleStateChanged(object sender, Parsing.ParseProgress
395397

396398
UiDispatcher.Invoke(() =>
397399
{
398-
if (folderNode == null)
400+
try
399401
{
400-
folderNode = new CodeExplorerCustomFolderViewModel(projectNode, projectName, projectName);
401-
projectNode.AddChild(folderNode);
402+
if (folderNode == null)
403+
{
404+
folderNode = new CodeExplorerCustomFolderViewModel(projectNode, projectName, projectName);
405+
projectNode.AddChild(folderNode);
406+
}
407+
408+
var declaration = CreateDeclaration(e.Module);
409+
var newNode =
410+
new CodeExplorerComponentViewModel(folderNode, declaration, new List<Declaration>())
411+
{
412+
IsErrorState = true
413+
};
414+
415+
folderNode.AddChild(newNode);
416+
417+
// Force a refresh. OnPropertyChanged("Projects") didn't work.
418+
Projects = Projects;
402419
}
403-
404-
var declaration = CreateDeclaration(e.Module);
405-
var newNode = new CodeExplorerComponentViewModel(folderNode, declaration, new List<Declaration>())
420+
catch (Exception exception)
406421
{
407-
IsErrorState = true
408-
};
409-
410-
folderNode.AddChild(newNode);
411-
412-
// Force a refresh. OnPropertyChanged("Projects") didn't work.
413-
Projects = Projects;
422+
Logger.Error(exception, "Exception thrown trying to refresh the code explorer view on the UI thread.");
423+
}
414424
});
415425
}
416426
}

RetailCoder.VBE/UI/Command/FindAllImplementationsCommand.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class FindAllImplementationsCommand : CommandBase, IDisposable
2727
private readonly SearchResultPresenterInstanceManager _presenterService;
2828
private readonly IVBE _vbe;
2929

30+
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
31+
3032
public FindAllImplementationsCommand(INavigateCommand navigateCommand, IMessageBox messageBox,
3133
RubberduckParserState state, IVBE vbe, ISearchResultsWindowViewModel viewModel,
3234
SearchResultPresenterInstanceManager presenterService)
@@ -63,29 +65,36 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)
6365

6466
private void UpdateTab()
6567
{
66-
var findImplementationsTabs = _viewModel.Tabs.Where(
67-
t => t.Header.StartsWith(RubberduckUI.AllImplementations_Caption.Replace("'{0}'", ""))).ToList();
68-
69-
foreach (var tab in findImplementationsTabs)
68+
try
7069
{
71-
var newTarget = FindNewDeclaration(tab.Target);
72-
if (newTarget == null)
73-
{
74-
tab.CloseCommand.Execute(null);
75-
return;
76-
}
70+
var findImplementationsTabs = _viewModel.Tabs.Where(
71+
t => t.Header.StartsWith(RubberduckUI.AllImplementations_Caption.Replace("'{0}'", ""))).ToList();
7772

78-
var vm = CreateViewModel(newTarget);
79-
if (vm.SearchResults.Any())
73+
foreach (var tab in findImplementationsTabs)
8074
{
81-
tab.SearchResults = vm.SearchResults;
82-
tab.Target = vm.Target;
83-
}
84-
else
85-
{
86-
tab.CloseCommand.Execute(null);
75+
var newTarget = FindNewDeclaration(tab.Target);
76+
if (newTarget == null)
77+
{
78+
tab.CloseCommand.Execute(null);
79+
return;
80+
}
81+
82+
var vm = CreateViewModel(newTarget);
83+
if (vm.SearchResults.Any())
84+
{
85+
tab.SearchResults = vm.SearchResults;
86+
tab.Target = vm.Target;
87+
}
88+
else
89+
{
90+
tab.CloseCommand.Execute(null);
91+
}
8792
}
8893
}
94+
catch (Exception exception)
95+
{
96+
Logger.Error(exception, "Exception thrown while trying to update the find implementations tab.");
97+
}
8998
}
9099

91100
protected override bool EvaluateCanExecute(object parameter)

RetailCoder.VBE/UI/Command/FindAllReferencesCommand.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,36 @@ private void _state_StateChanged(object sender, ParserStateEventArgs e)
6161

6262
private void UpdateTab()
6363
{
64-
var findReferenceTabs = _viewModel.Tabs.Where(
65-
t => t.Header.StartsWith(RubberduckUI.AllReferences_Caption.Replace("'{0}'", ""))).ToList();
66-
67-
foreach (var tab in findReferenceTabs)
64+
try
6865
{
69-
var newTarget = FindNewDeclaration(tab.Target);
70-
if (newTarget == null)
71-
{
72-
tab.CloseCommand.Execute(null);
73-
return;
74-
}
66+
var findReferenceTabs = _viewModel.Tabs.Where(
67+
t => t.Header.StartsWith(RubberduckUI.AllReferences_Caption.Replace("'{0}'", ""))).ToList();
7568

76-
var vm = CreateViewModel(newTarget);
77-
if (vm.SearchResults.Any())
69+
foreach (var tab in findReferenceTabs)
7870
{
79-
tab.SearchResults = vm.SearchResults;
80-
tab.Target = vm.Target;
81-
}
82-
else
83-
{
84-
tab.CloseCommand.Execute(null);
71+
var newTarget = FindNewDeclaration(tab.Target);
72+
if (newTarget == null)
73+
{
74+
tab.CloseCommand.Execute(null);
75+
return;
76+
}
77+
78+
var vm = CreateViewModel(newTarget);
79+
if (vm.SearchResults.Any())
80+
{
81+
tab.SearchResults = vm.SearchResults;
82+
tab.Target = vm.Target;
83+
}
84+
else
85+
{
86+
tab.CloseCommand.Execute(null);
87+
}
8588
}
8689
}
90+
catch (Exception exception)
91+
{
92+
Logger.Error(exception, "Exception thrown while trying to update the find all references tab.");
93+
}
8794
}
8895

8996
protected override bool EvaluateCanExecute(object parameter)

RetailCoder.VBE/UI/Command/MenuItems/CommandBars/AppCommandBarBase.cs

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class AppCommandBarBase : IAppCommandBar
1515
private readonly string _name;
1616
private readonly CommandBarPosition _position;
1717
private readonly IDictionary<ICommandMenuItem, ICommandBarControl> _items;
18-
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
18+
protected static readonly Logger Logger = LogManager.GetCurrentClassLogger();
1919

2020
protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerable<ICommandMenuItem> items)
2121
{
@@ -26,10 +26,18 @@ protected AppCommandBarBase(string name, CommandBarPosition position, IEnumerabl
2626

2727
protected ICommandMenuItem FindChildByTag(string tag)
2828
{
29-
var child = _items.FirstOrDefault(kvp => kvp.Value != null && kvp.Value.Tag == tag);
30-
return Equals(child, default(KeyValuePair<ICommandMenuItem, ICommandBarControl>))
31-
? null
32-
: child.Key;
29+
try
30+
{
31+
var child = _items.FirstOrDefault(kvp => kvp.Value != null && kvp.Value.Tag == tag);
32+
return Equals(child, default(KeyValuePair<ICommandMenuItem, ICommandBarControl>))
33+
? null
34+
: child.Key;
35+
}
36+
catch (COMException exception)
37+
{
38+
Logger.Error(exception,$"COMException while finding child with tag '{tag}'.");
39+
}
40+
return null;
3341
}
3442

3543
public void Localize()
@@ -39,34 +47,52 @@ public void Localize()
3947
return;
4048
}
4149

42-
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
50+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null && !kv.Value.IsWrappingNullReference))
4351
{
4452
try
4553
{
4654
var item = kvp;
47-
UiDispatcher.Invoke(() =>
48-
{
49-
item.Value.Caption = item.Key.Caption.Invoke();
50-
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
51-
});
55+
UiDispatcher.Invoke(() => LocalizeInternal(item, kvp));
5256

5357
}
5458
catch (Exception e)
5559
{
56-
Logger.Error(e, $"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
60+
Logger.Error(e, $"Failed to dispatch assignment of {kvp.Value.GetType().Name}.Caption and .TooltipText for {kvp.Key.GetType().Name} to the UI thread.");
5761
}
5862
}
5963
}
6064

65+
private static void LocalizeInternal(KeyValuePair<ICommandMenuItem, ICommandBarControl> item, KeyValuePair<ICommandMenuItem, ICommandBarControl> kvp)
66+
{
67+
try
68+
{
69+
item.Value.Caption = item.Key.Caption.Invoke();
70+
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
71+
}
72+
catch (Exception e)
73+
{
74+
Logger.Error(e,
75+
$"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
76+
}
77+
}
78+
6179
public virtual void Initialize()
6280
{
63-
if (Parent == null)
81+
if (Parent == null || Parent.IsWrappingNullReference)
6482
{
6583
return;
6684
}
6785

68-
Item = Parent.Add(_name, _position);
69-
Item.IsVisible = true;
86+
try
87+
{
88+
Item = Parent.Add(_name, _position);
89+
Item.IsVisible = true;
90+
}
91+
catch (COMException exception)
92+
{
93+
Logger.Error(exception, $"Failed to add the command bar {_name}.");
94+
return;
95+
}
7096
foreach (var item in _items.Keys.OrderBy(item => item.DisplayOrder))
7197
{
7298
try
@@ -109,7 +135,7 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
109135

110136
public void EvaluateCanExecute(RubberduckParserState state)
111137
{
112-
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
138+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null && !kv.Value.IsWrappingNullReference))
113139
{
114140
var commandItem = kvp.Key;
115141
var canExecute = false;
@@ -122,10 +148,17 @@ public void EvaluateCanExecute(RubberduckParserState state)
122148
{
123149
Logger.Error(e, $"{commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}.EvaluateCanExecute(RubberduckParserState) threw an exception.");
124150
}
125-
kvp.Value.IsEnabled = canExecute;
126-
if (commandItem?.HiddenWhenDisabled ?? false)
151+
try
152+
{
153+
kvp.Value.IsEnabled = canExecute;
154+
if (commandItem?.HiddenWhenDisabled ?? false)
155+
{
156+
kvp.Value.IsVisible = canExecute;
157+
}
158+
}
159+
catch (COMException exception)
127160
{
128-
kvp.Value.IsVisible = canExecute;
161+
Logger.Error(exception,$"COMException while trying to set IsEnabled and IsVisible on {commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}");
129162
}
130163
}
131164
}
@@ -135,11 +168,18 @@ public void EvaluateCanExecute(RubberduckParserState state)
135168

136169
public void RemoveCommandBar()
137170
{
138-
Logger.Debug("Removing commandbar.");
139-
RemoveChildren();
140-
Item?.Delete();
141-
Item = null;
142-
Parent = null;
171+
try
172+
{
173+
Logger.Debug("Removing commandbar.");
174+
RemoveChildren();
175+
Item?.Delete();
176+
Item = null;
177+
Parent = null;
178+
}
179+
catch (COMException exception)
180+
{
181+
Logger.Error(exception, "COM exception while trying to delee the commandbar");
182+
}
143183
}
144184

145185
private void RemoveChildren()
@@ -169,7 +209,16 @@ private void RemoveChildren()
169209

170210
private void child_Click(object sender, CommandBarButtonClickEventArgs e)
171211
{
172-
var item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag);
212+
ICommandMenuItem item;
213+
try
214+
{
215+
item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag);
216+
}
217+
catch (COMException exception)
218+
{
219+
Logger.Error(exception, "COM exception finding command for a control.");
220+
item = null;
221+
}
173222
if (item == null)
174223
{
175224
return;

RetailCoder.VBE/UI/Command/MenuItems/CommandBars/RubberduckCommandBar.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,19 @@ private void SetStatusLabelCaption(string caption, int? errorCount = null)
107107

108108
UiDispatcher.Invoke(() =>
109109
{
110-
reparseCommandButton.SetCaption(caption);
111-
reparseCommandButton.SetToolTip(string.Format(RubberduckUI.ReparseToolTipText, caption));
112-
if (errorCount.HasValue && errorCount.Value > 0)
110+
try
113111
{
114-
showErrorsCommandButton.SetToolTip(string.Format(RubberduckUI.ParserErrorToolTipText, errorCount.Value));
112+
reparseCommandButton.SetCaption(caption);
113+
reparseCommandButton.SetToolTip(string.Format(RubberduckUI.ReparseToolTipText, caption));
114+
if (errorCount.HasValue && errorCount.Value > 0)
115+
{
116+
showErrorsCommandButton.SetToolTip(
117+
string.Format(RubberduckUI.ParserErrorToolTipText, errorCount.Value));
118+
}
119+
}
120+
catch (Exception exception)
121+
{
122+
Logger.Error(exception, "Exception thrown trying to set the status label caption on the UI thread.");
115123
}
116124
});
117125
Localize();
@@ -125,9 +133,16 @@ private void SetContextSelectionCaption(string caption, int contextReferenceCoun
125133

126134
UiDispatcher.Invoke(() =>
127135
{
128-
contextLabel?.SetCaption(caption);
129-
contextReferences?.SetCaption(contextReferenceCount);
130-
contextDescription?.SetCaption(description);
136+
try
137+
{
138+
contextLabel?.SetCaption(caption);
139+
contextReferences?.SetCaption(contextReferenceCount);
140+
contextDescription?.SetCaption(description);
141+
}
142+
catch (Exception exception)
143+
{
144+
Logger.Error(exception, "Exception thrown trying to set the context selection caption on the UI thread.");
145+
}
131146
});
132147
Localize();
133148
}

0 commit comments

Comments
 (0)