Skip to content

Commit 5eb456b

Browse files
committed
Merge remote-tracking branch 'upstream/next' into rkapka-master
2 parents 1fa3e01 + f4cf930 commit 5eb456b

39 files changed

+809
-380
lines changed

RetailCoder.VBE/Navigation/RegexSearchReplace/RegexSearchReplace.cs

Lines changed: 80 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,13 @@ private void SetSelection(RegexSearchResult item)
9494

9595
private List<RegexSearchResult> SearchSelection(string searchPattern)
9696
{
97-
var pane = _vbe.ActiveCodePane;
98-
var module = pane.CodeModule;
97+
using (var pane = _vbe.ActiveCodePane)
9998
{
100-
var results = GetResultsFromModule(module, searchPattern);
101-
return results.Where(r => pane.Selection.Contains(r.Selection)).ToList();
99+
using (var module = pane.CodeModule)
100+
{
101+
var results = GetResultsFromModule(module, searchPattern);
102+
return results.Where(r => pane.Selection.Contains(r.Selection)).ToList();
103+
}
102104
}
103105
}
104106

@@ -115,34 +117,56 @@ private List<RegexSearchResult> SearchCurrentBlock(string searchPattern)
115117
};
116118

117119
var state = _parser.State;
118-
var pane = _vbe.ActiveCodePane;
119-
var module = pane.CodeModule;
120+
using (var pane = _vbe.ActiveCodePane)
120121
{
121-
var results = GetResultsFromModule(module, searchPattern);
122+
using (var module = pane.CodeModule)
123+
{
124+
var results = GetResultsFromModule(module, searchPattern);
122125

123-
var qualifiedSelection = new QualifiedSelection(new QualifiedModuleName(module.Parent), pane.Selection);
124-
dynamic block = state.AllDeclarations.FindTarget(qualifiedSelection, declarationTypes).Context.Parent;
125-
var selection = new Selection(block.Start.Line, block.Start.Column, block.Stop.Line, block.Stop.Column);
126-
return results.Where(r => selection.Contains(r.Selection)).ToList();
126+
using (var moduleParent = module.Parent)
127+
{
128+
var qualifiedSelection =
129+
new QualifiedSelection(new QualifiedModuleName(moduleParent), pane.Selection);
130+
dynamic block = state.AllDeclarations.FindTarget(qualifiedSelection, declarationTypes).Context
131+
.Parent;
132+
var selection = new Selection(block.Start.Line, block.Start.Column, block.Stop.Line,
133+
block.Stop.Column);
134+
135+
return results.Where(r => selection.Contains(r.Selection)).ToList();
136+
}
137+
}
127138
}
128139
}
129140

130141
private List<RegexSearchResult> SearchCurrentFile(string searchPattern)
131142
{
132-
var pane = _vbe.ActiveCodePane;
143+
using (var pane = _vbe.ActiveCodePane)
133144
{
134-
return GetResultsFromModule(pane.CodeModule, searchPattern).ToList();
145+
using (var codeModule = pane.CodeModule)
146+
{
147+
return GetResultsFromModule(codeModule, searchPattern).ToList();
148+
}
135149
}
136150
}
137151

138152
private List<RegexSearchResult> SearchOpenFiles(string searchPattern)
139153
{
140154
var results = new List<RegexSearchResult>();
141-
var panes = _vbe.CodePanes;
155+
using (var panes = _vbe.CodePanes)
142156
{
143157
foreach (var codePane in panes)
144158
{
145-
results.AddRange(GetResultsFromModule(codePane.CodeModule, searchPattern));
159+
try
160+
{
161+
using (var codeModule = codePane.CodeModule)
162+
{
163+
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
164+
}
165+
}
166+
finally
167+
{
168+
codePane.Dispose();
169+
}
146170
}
147171

148172
return results;
@@ -152,15 +176,26 @@ private List<RegexSearchResult> SearchOpenFiles(string searchPattern)
152176
private List<RegexSearchResult> SearchCurrentProject(string searchPattern)
153177
{
154178
var results = new List<RegexSearchResult>();
155-
var project = _vbe.ActiveVBProject;
156-
var components = project.VBComponents;
179+
using (var project = _vbe.ActiveVBProject)
157180
{
158-
foreach (var component in components)
181+
using (var components = project.VBComponents)
159182
{
160-
results.AddRange(GetResultsFromModule(component.CodeModule, searchPattern));
183+
foreach (var component in components)
184+
{
185+
try
186+
{
187+
using (var codeModule = component.CodeModule)
188+
{
189+
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
190+
}
191+
}
192+
finally
193+
{
194+
component.Dispose();
195+
}
196+
}
197+
return results;
161198
}
162-
163-
return results;
164199
}
165200
}
166201

@@ -171,27 +206,38 @@ private List<RegexSearchResult> SearchOpenProjects(string searchPattern)
171206
{
172207
foreach (var project in projects)
173208
{
174-
if (project.Protection == ProjectProtection.Locked)
209+
try
175210
{
176-
project.Dispose();
177-
continue;
178-
}
179-
using (var components = project.VBComponents)
180-
{
181-
foreach (var component in components)
211+
if (project.Protection == ProjectProtection.Locked)
182212
{
183-
using (var codeModule = component.CodeModule)
213+
continue;
214+
}
215+
216+
using (var components = project.VBComponents)
217+
{
218+
foreach (var component in components)
184219
{
185-
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
220+
try
221+
{
222+
using (var codeModule = component.CodeModule)
223+
{
224+
results.AddRange(GetResultsFromModule(codeModule, searchPattern));
225+
}
226+
}
227+
finally
228+
{
229+
component.Dispose();
230+
}
186231
}
187-
component.Dispose();
188232
}
189233
}
190-
project.Dispose();
234+
finally
235+
{
236+
project.Dispose();
237+
}
191238
}
192-
193-
return results;
194239
}
240+
return results;
195241
}
196242
}
197243
}

RetailCoder.VBE/Refactorings/ExtractInterface/ExtractInterfaceRefactoring.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,23 @@ private void AddInterface()
100100

101101
AddInterfaceMembersToClass(rewriter);
102102

103-
var components = _model.TargetDeclaration.Project.VBComponents;
104-
var interfaceComponent = components.Add(ComponentType.ClassModule);
105-
var interfaceModule = interfaceComponent.CodeModule;
106-
interfaceComponent.Name = _model.InterfaceName;
107-
108-
var optionPresent = interfaceModule.CountOfLines > 1;
109-
if (!optionPresent)
103+
using (var components = _model.TargetDeclaration.Project.VBComponents)
110104
{
111-
interfaceModule.InsertLines(1, $"{Tokens.Option} {Tokens.Explicit}{Environment.NewLine}");
105+
using (var interfaceComponent = components.Add(ComponentType.ClassModule))
106+
{
107+
using (var interfaceModule = interfaceComponent.CodeModule)
108+
{
109+
interfaceComponent.Name = _model.InterfaceName;
110+
111+
var optionPresent = interfaceModule.CountOfLines > 1;
112+
if (!optionPresent)
113+
{
114+
interfaceModule.InsertLines(1, $"{Tokens.Option} {Tokens.Explicit}{Environment.NewLine}");
115+
}
116+
interfaceModule.InsertLines(3, GetInterfaceModuleBody());
117+
}
118+
}
112119
}
113-
interfaceModule.InsertLines(3, GetInterfaceModuleBody());
114120
}
115121

116122
private void AddInterfaceMembersToClass(IModuleRewriter rewriter)

RetailCoder.VBE/Refactorings/ImplementInterface/ImplementInterfaceRefactoring.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,23 @@ public void Refactor(QualifiedSelection selection)
7777
QualifiedSelection? oldSelection = null;
7878
if (_vbe.ActiveCodePane != null)
7979
{
80-
oldSelection = _vbe.ActiveCodePane.CodeModule.GetQualifiedSelection();
80+
using (var codePane = _vbe.ActiveCodePane)
81+
{
82+
oldSelection = codePane.GetQualifiedSelection();
83+
}
8184
}
8285

8386
ImplementMissingMembers(_state.GetRewriter(_targetClass));
8487

8588
if (oldSelection.HasValue)
8689
{
87-
var module = oldSelection.Value.QualifiedName.Component.CodeModule;
88-
var pane = module.CodePane;
89-
pane.Selection = oldSelection.Value.Selection;
90+
using (var module = oldSelection.Value.QualifiedName.Component.CodeModule)
91+
{
92+
using (var pane = module.CodePane)
93+
{
94+
pane.Selection = oldSelection.Value.Selection;
95+
}
96+
}
9097
}
9198

9299
_state.OnParseRequested(this);

RetailCoder.VBE/Refactorings/RemoveParameters/RemoveParametersRefactoring.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,22 @@ public void Refactor()
4141
}
4242

4343
QualifiedSelection? oldSelection = null;
44-
var pane = _vbe.ActiveCodePane;
45-
var module = pane.CodeModule;
46-
if (!module.IsWrappingNullReference)
44+
using (var pane = _vbe.ActiveCodePane)
4745
{
48-
oldSelection = module.GetQualifiedSelection();
49-
}
46+
using (var module = pane.CodeModule)
47+
{
48+
if (!module.IsWrappingNullReference)
49+
{
50+
oldSelection = module.GetQualifiedSelection();
51+
}
52+
}
5053

51-
RemoveParameters();
54+
RemoveParameters();
5255

53-
if (oldSelection.HasValue)
54-
{
55-
pane.Selection = oldSelection.Value.Selection;
56+
if (oldSelection.HasValue)
57+
{
58+
pane.Selection = oldSelection.Value.Selection;
59+
}
5660
}
5761

5862
_model.State.OnParseRequested(this);
@@ -116,7 +120,6 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references, Decla
116120
{
117121
foreach (var reference in references.Where(item => item.Context != method.Context))
118122
{
119-
var module = reference.QualifiedModuleName.Component.CodeModule;
120123
VBAParser.ArgumentListContext argumentList = null;
121124
var callStmt = ParserRuleContextHelper.GetParent<VBAParser.CallStmtContext>(reference.Context);
122125
if (callStmt != null)
@@ -139,7 +142,10 @@ private void AdjustReferences(IEnumerable<IdentifierReference> references, Decla
139142
continue;
140143
}
141144

142-
RemoveCallArguments(argumentList, module);
145+
using (var module = reference.QualifiedModuleName.Component.CodeModule)
146+
{
147+
RemoveCallArguments(argumentList, module);
148+
}
143149
}
144150
}
145151

RetailCoder.VBE/Refactorings/Rename/RenamePresenterFactory.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ public RenamePresenterFactory(IVBE vbe, IRefactoringDialog<RenameViewModel> view
2121

2222
public RenamePresenter Create()
2323
{
24-
var codePane = _vbe.ActiveCodePane;
25-
var qualifiedSelection = codePane.IsWrappingNullReference
26-
? new QualifiedSelection()
27-
: new QualifiedSelection(new QualifiedModuleName(codePane.CodeModule.Parent), codePane.Selection);
28-
24+
QualifiedSelection qualifiedSelection;
25+
using (var codePane = _vbe.ActiveCodePane)
26+
{
27+
using (var codeModule = codePane.CodeModule)
28+
{
29+
qualifiedSelection = codePane.IsWrappingNullReference
30+
? new QualifiedSelection()
31+
: new QualifiedSelection(new QualifiedModuleName(codeModule.Parent), codePane.Selection);
32+
}
33+
}
2934
return new RenamePresenter(_view, new RenameModel(_vbe, _state, qualifiedSelection));
3035
}
3136
}

RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,27 @@ private bool IsValidTarget(Declaration target)
210210

211211
if (target.DeclarationType.HasFlag(DeclarationType.Control))
212212
{
213-
var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule;
214-
var control = module.Parent.Controls.FirstOrDefault(item => item.Name == target.IdentifierName);
215-
if (control == null)
213+
using (var controls = target.QualifiedName.QualifiedModuleName.Component.Controls)
216214
{
217-
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null control reference");
218-
return false;
215+
using (var control = controls.FirstOrDefault(item => item.Name == target.IdentifierName))
216+
{
217+
if (control == null)
218+
{
219+
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null control reference");
220+
return false;
221+
}
222+
}
219223
}
220224
}
221225
else if (target.DeclarationType.HasFlag(DeclarationType.Module))
222226
{
223-
var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule;
224-
if (module.IsWrappingNullReference)
227+
using (var module = target.QualifiedName.QualifiedModuleName.Component.CodeModule)
225228
{
226-
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null Module reference");
227-
return false;
229+
if (module.IsWrappingNullReference)
230+
{
231+
PresentRenameErrorMessage($"{BuildDefaultErrorMessage(target)} - Null Module reference");
232+
return false;
233+
}
228234
}
229235
}
230236
return true;
@@ -381,13 +387,17 @@ private void RenameVariable()
381387
{
382388
if (_model.Target.DeclarationType.HasFlag(DeclarationType.Control))
383389
{
384-
var module = _model.Target.QualifiedName.QualifiedModuleName.Component.CodeModule;
385-
var control = module.Parent.Controls.SingleOrDefault(item => item.Name == _model.Target.IdentifierName);
386-
Debug.Assert(control != null, $"input validation fail: unable to locate '{_model.Target.IdentifierName}' in Controls collection");
390+
using (var controls = _model.Target.QualifiedName.QualifiedModuleName.Component.Controls)
391+
{
392+
using (var control = controls.SingleOrDefault(item => item.Name == _model.Target.IdentifierName))
393+
{
394+
Debug.Assert(control != null,
395+
$"input validation fail: unable to locate '{_model.Target.IdentifierName}' in Controls collection");
387396

388-
control.Name = _model.NewName;
397+
control.Name = _model.NewName;
398+
}
399+
}
389400
RenameReferences(_model.Target, _model.NewName);
390-
391401
var controlEventHandlers = FindEventHandlersForControl(_model.Target);
392402
RenameDefinedFormatMembers(controlEventHandlers, _appendUnderscoreFormat);
393403
}
@@ -443,8 +453,11 @@ private void RenameModule()
443453
}
444454
else
445455
{
446-
Debug.Assert(!component.CodeModule.IsWrappingNullReference, "input validation fail: Attempting to rename an ICodeModule wrapping a null reference");
447-
component.CodeModule.Name = _model.NewName;
456+
using (var codeModule = component.CodeModule)
457+
{
458+
Debug.Assert(!codeModule.IsWrappingNullReference, "input validation fail: Attempting to rename an ICodeModule wrapping a null reference");
459+
codeModule.Name = _model.NewName;
460+
}
448461
}
449462
}
450463

0 commit comments

Comments
 (0)