Skip to content

Commit 5dae49d

Browse files
committed
Merge pull request #644 from rossknudsen/refactor
Tidied up App class instantiation of fields.
2 parents d2caba0 + d93d435 commit 5dae49d

File tree

2 files changed

+60
-53
lines changed

2 files changed

+60
-53
lines changed

RetailCoder.VBE/App.cs

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class App : IDisposable
3030
private RubberduckMenu _menu;
3131
private FormContextMenu _formContextMenu;
3232
private CodeInspectionsToolbar _codeInspectionsToolbar;
33+
private bool displayToolbar = false;
34+
private Point toolbarCoords = new Point(-1, -1);
3335

3436
public App(VBE vbe, AddIn addIn)
3537
{
@@ -42,11 +44,19 @@ public App(VBE vbe, AddIn addIn)
4244
_editor = new ActiveCodePaneEditor(vbe);
4345

4446
LoadConfig();
47+
48+
CleanUp();
49+
50+
Setup();
4551
}
4652

4753
private void _configService_SettingsChanged(object sender, EventArgs e)
4854
{
4955
LoadConfig();
56+
57+
CleanUp();
58+
59+
Setup();
5060
}
5161

5262
private void LoadConfig()
@@ -64,48 +74,15 @@ private void LoadConfig()
6474
_config.UserSettings.LanguageSetting.Code = currentCulture.Name;
6575
_configService.SaveConfiguration(_config);
6676
}
77+
}
6778

68-
if (_menu != null)
69-
{
70-
_menu.Dispose();
71-
}
72-
73-
if (_formContextMenu != null)
74-
{
75-
_formContextMenu.Dispose();
76-
}
77-
78-
var displayToolbar = false;
79-
var toolbarCoords = new Point(-1, -1);
80-
if (_codeInspectionsToolbar != null)
81-
{
82-
displayToolbar = _codeInspectionsToolbar.ToolbarVisible;
83-
toolbarCoords = _codeInspectionsToolbar.ToolbarCoords;
84-
_codeInspectionsToolbar.Dispose();
85-
}
86-
87-
if (_inspector != null)
88-
{
89-
_inspector.Dispose();
90-
}
91-
92-
if (_parserErrorsPresenter != null)
93-
{
94-
_parserErrorsPresenter.Dispose();
95-
}
96-
97-
if (_parser != null)
98-
{
99-
_parser.ParseStarted -= _parser_ParseStarted;
100-
_parser.ParserError -= _parser_ParserError;
101-
}
79+
private void Setup()
80+
{
10281
_parser = new RubberduckParser();
10382
_parser.ParseStarted += _parser_ParseStarted;
10483
_parser.ParserError += _parser_ParserError;
10584

106-
_inspections = _configService.GetImplementedCodeInspections();
107-
_inspector = new Inspector(_parser, _inspections);
108-
EnableCodeInspections(_config);
85+
_inspector = new Inspector(_parser, _configService);
10986

11087
_parserErrorsPresenter = new ParserErrorsPresenter(_vbe, _addIn);
11188

@@ -136,20 +113,6 @@ private void _parser_ParserError(object sender, ParseErrorEventArgs e)
136113
_parserErrorsPresenter.Show();
137114
}
138115

139-
private void EnableCodeInspections(Configuration config)
140-
{
141-
foreach (var inspection in _inspections)
142-
{
143-
foreach (var setting in config.UserSettings.CodeInspectionSettings.CodeInspections)
144-
{
145-
if (inspection.Description == setting.Description)
146-
{
147-
inspection.Severity = setting.Severity;
148-
}
149-
}
150-
}
151-
}
152-
153116
public void Dispose()
154117
{
155118
Dispose(true);
@@ -159,6 +122,11 @@ protected virtual void Dispose(bool disposing)
159122
{
160123
if (!disposing) { return; }
161124

125+
CleanUp();
126+
}
127+
128+
private void CleanUp()
129+
{
162130
if (_menu != null)
163131
{
164132
_menu.Dispose();
@@ -171,6 +139,8 @@ protected virtual void Dispose(bool disposing)
171139

172140
if (_codeInspectionsToolbar != null)
173141
{
142+
displayToolbar = _codeInspectionsToolbar.ToolbarVisible;
143+
toolbarCoords = _codeInspectionsToolbar.ToolbarCoords;
174144
_codeInspectionsToolbar.Dispose();
175145
}
176146

@@ -183,6 +153,12 @@ protected virtual void Dispose(bool disposing)
183153
{
184154
_parserErrorsPresenter.Dispose();
185155
}
156+
157+
if (_parser != null)
158+
{
159+
_parser.ParseStarted -= _parser_ParseStarted;
160+
_parser.ParserError -= _parser_ParserError;
161+
}
186162
}
187163
}
188164
}

RetailCoder.VBE/Inspections/Inspector.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,47 @@
66
using System.Threading.Tasks;
77
using Microsoft.Vbe.Interop;
88
using Rubberduck.Parsing;
9+
using Rubberduck.Settings;
910

1011
namespace Rubberduck.Inspections
1112
{
1213
public class Inspector : IInspector, IDisposable
1314
{
1415
private readonly IRubberduckParser _parser;
16+
private readonly IGeneralConfigService _configService;
1517
private readonly IList<IInspection> _inspections;
1618

17-
public Inspector(IRubberduckParser parser, IEnumerable<IInspection> inspections)
19+
public Inspector(IRubberduckParser parser, IGeneralConfigService configService)
1820
{
1921
_parser = parser;
2022
_parser.ParseStarted += _parser_ParseStarted;
2123
_parser.ParseCompleted += _parser_ParseCompleted;
2224

23-
_inspections = inspections.ToList();
25+
_configService = configService;
26+
_inspections = configService.GetImplementedCodeInspections();
27+
configService.SettingsChanged += ConfigServiceSettingsChanged;
28+
UpdateInspectionSeverity();
29+
}
30+
31+
private void ConfigServiceSettingsChanged(object sender, EventArgs e)
32+
{
33+
UpdateInspectionSeverity();
34+
}
35+
36+
private void UpdateInspectionSeverity()
37+
{
38+
var config = _configService.LoadConfiguration();
39+
40+
foreach (var inspection in _inspections)
41+
{
42+
foreach (var setting in config.UserSettings.CodeInspectionSettings.CodeInspections)
43+
{
44+
if (inspection.Description == setting.Description)
45+
{
46+
inspection.Severity = setting.Severity;
47+
}
48+
}
49+
}
2450
}
2551

2652
private void _parser_ParseCompleted(object sender, ParseCompletedEventArgs e)
@@ -139,6 +165,11 @@ protected virtual void Dispose(bool disposing)
139165
if (!disposing) { return; }
140166
_parser.ParseStarted -= _parser_ParseStarted;
141167
_parser.ParseCompleted -= _parser_ParseCompleted;
168+
169+
if (_configService != null)
170+
{
171+
_configService.SettingsChanged -= ConfigServiceSettingsChanged;
172+
}
142173
}
143174
}
144175
}

0 commit comments

Comments
 (0)