Skip to content

Commit f638b76

Browse files
committed
replaced all instances of VBE.VBProjects with _state.Projects - this breaks 308 tests, but fixes equality checks and stabilizes lots of things.
1 parent 8bab6ea commit f638b76

File tree

17 files changed

+91
-74
lines changed

17 files changed

+91
-74
lines changed

RetailCoder.VBE/App.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public void Startup()
9494
#region sink handlers. todo: move to another class
9595
async void sink_ProjectRemoved(object sender, DispatcherEventArgs<VBProject> e)
9696
{
97+
_parser.State.RemoveProject(e.Item);
98+
9799
Debug.WriteLine(string.Format("Project '{0}' was removed.", e.Item.Name));
98100
Tuple<IConnectionPoint, int> value;
99101
if (_componentsEventsConnectionPoints.TryGetValue(e.Item.VBComponents, out value))
@@ -107,6 +109,8 @@ async void sink_ProjectRemoved(object sender, DispatcherEventArgs<VBProject> e)
107109

108110
async void sink_ProjectAdded(object sender, DispatcherEventArgs<VBProject> e)
109111
{
112+
_parser.State.AddProject(e.Item);
113+
110114
if (!_parser.State.AllDeclarations.Any())
111115
{
112116
// forces menus to evaluate their CanExecute state:

RetailCoder.VBE/AutoSave/AutoSave.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ private void _timer_Elapsed(object sender, ElapsedEventArgs e)
4848
{
4949
try
5050
{
51-
// note: VBProject.FileName getter throws IOException if unsaved
52-
_vbe.VBProjects.OfType<VBProject>().Select(p => p.FileName).ToList();
51+
var projects = _vbe.VBProjects.OfType<VBProject>().Select(p => p.FileName).ToList();
5352
}
54-
catch (DirectoryNotFoundException)
53+
catch (IOException)
5554
{
55+
// note: VBProject.FileName getter throws IOException if unsaved
5656
return;
5757
}
5858

RetailCoder.VBE/Common/DeclarationExtensions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,14 @@ public static Declaration FindInterfaceMember(this IEnumerable<Declaration> decl
398398
: matches.First();
399399
}
400400

401+
public static Declaration FindTarget(this IEnumerable<Declaration> declarations, QualifiedSelection selection)
402+
{
403+
var items = declarations.ToList();
404+
Debug.Assert(!items.Any(item => item.IsBuiltIn));
405+
406+
return items.SingleOrDefault(item => item.IsSelected(selection) || item.References.Any(reference => reference.IsSelected(selection)));
407+
}
408+
401409
/// <summary>
402410
/// Returns the declaration contained in a qualified selection.
403411
/// To get the selection of a variable or field, use FindVariable(QualifiedSelection)
@@ -412,7 +420,7 @@ public static Declaration FindTarget(this IEnumerable<Declaration> declarations,
412420

413421
var target = items
414422
.Where(item => !item.IsBuiltIn)
415-
.FirstOrDefault(item => item.IsSelected(selection)
423+
.SingleOrDefault(item => item.IsSelected(selection)
416424
|| item.References.Any(r => r.IsSelected(selection)));
417425

418426
if (target != null && validDeclarationTypes.Contains(target.DeclarationType))
@@ -531,8 +539,7 @@ public static Declaration FindInterface(this IEnumerable<Declaration> declaratio
531539
implementsStmt.GetSelection().StartColumn, reference.Selection.EndLine,
532540
reference.Selection.EndColumn);
533541

534-
if (reference.QualifiedModuleName.ComponentName == selection.QualifiedName.ComponentName &&
535-
reference.QualifiedModuleName.Project == selection.QualifiedName.Project &&
542+
if (reference.QualifiedModuleName.Equals(selection.QualifiedName) &&
536543
completeSelection.Contains(selection.Selection))
537544
{
538545
return declaration;

RetailCoder.VBE/Inspections/MoveFieldCloserToUsageInspection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private Declaration ParentDeclaration(IdentifierReference reference)
6161

6262
return UserDeclarations.SingleOrDefault(d =>
6363
reference.ParentScoping.Equals(d) && declarationTypes.Contains(d.DeclarationType) &&
64-
d.Project == reference.QualifiedModuleName.Project);
64+
d.QualifiedName.QualifiedModuleName.Equals(reference.QualifiedModuleName));
6565
}
6666
}
6767
}

RetailCoder.VBE/Inspections/ProcedureShouldBeFunctionInspectionResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ private void UpdateCalls()
122122
!d.IsBuiltIn &&
123123
d.IdentifierName == procedureName &&
124124
d.Context is VBAParser.SubStmtContext &&
125-
d.ComponentName == _subStmtQualifiedContext.ModuleName.ComponentName &&
126-
d.Project == _subStmtQualifiedContext.ModuleName.Project);
125+
d.QualifiedName.QualifiedModuleName.Equals(_subStmtQualifiedContext.ModuleName));
127126

128127
if (procedure == null) { return; }
129128

RetailCoder.VBE/Navigation/RegexSearchReplace/RegexSearchReplace.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ private IEnumerable<RegexSearchResult> GetResultsFromModule(CodeModule module, s
100100
private void SetSelection(RegexSearchResult item)
101101
{
102102
var project = _vbe.ActiveVBProject;
103-
foreach (var proj in _vbe.VBProjects.Cast<VBProject>())
103+
foreach (var proj in _parser.State.Projects)
104104
{
105+
// wtf?
105106
project = proj;
106107
break;
107108
}
@@ -170,14 +171,14 @@ private List<RegexSearchResult> SearchCurrentProject(string searchPattern)
170171
private List<RegexSearchResult> SearchOpenProjects(string searchPattern)
171172
{
172173
var results = new List<RegexSearchResult>();
174+
var modules = _vbe.VBProjects.Cast<VBProject>()
175+
.Where(project => project.Protection == vbext_ProjectProtection.vbext_pp_none)
176+
.SelectMany(project => project.VBComponents.Cast<VBComponent>())
177+
.Select(component => component.CodeModule);
173178

174-
foreach (VBProject project in _vbe.VBProjects)
179+
foreach (var module in modules)
175180
{
176-
foreach (var component in project.VBComponents.Cast<VBComponent>())
177-
{
178-
var module = component.CodeModule;
179-
results.AddRange(GetResultsFromModule(module, searchPattern));
180-
}
181+
results.AddRange(GetResultsFromModule(module, searchPattern));
181182
}
182183

183184
return results;

RetailCoder.VBE/Refactorings/ExtractInterface/ExtractInterfaceModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public ExtractInterfaceModel(RubberduckParserState state, QualifiedSelection sel
3838
var candidates = declarations.Where(item => !item.IsBuiltIn && ModuleTypes.Contains(item.DeclarationType)).ToList();
3939

4040
_targetDeclaration = candidates.SingleOrDefault(item =>
41-
item.Project == selection.QualifiedName.Project
42-
&& item.QualifiedSelection.QualifiedName.ComponentName == selection.QualifiedName.ComponentName);
41+
item.QualifiedSelection.QualifiedName.Equals(selection.QualifiedName));
4342

4443
if (_targetDeclaration == null)
4544
{

RetailCoder.VBE/Refactorings/ImplementInterface/ImplementInterfaceRefactoring.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public void Refactor(QualifiedSelection selection)
4747

4848
_targetClass = _declarations.SingleOrDefault(d =>
4949
!d.IsBuiltIn && d.DeclarationType == DeclarationType.Class &&
50-
d.QualifiedSelection.QualifiedName.ComponentName == selection.QualifiedName.ComponentName &&
51-
d.Project == selection.QualifiedName.Project);
50+
d.QualifiedSelection.QualifiedName.Equals(selection.QualifiedName));
5251

5352
if (_targetClass == null || _targetInterface == null)
5453
{

RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ private void RenameProject()
211211
{
212212
try
213213
{
214-
var project = _model.VBE.VBProjects.Cast<VBProject>().FirstOrDefault(p => p.Name == _model.Target.IdentifierName);
214+
var project = _state.Projects.SingleOrDefault(p => p == _model.Target.Project);
215215
if (project != null)
216216
{
217217
project.Name = _model.NewName;

RetailCoder.VBE/UI/Command/Refactorings/CodePaneRefactorRenameCommand.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Rubberduck.VBEditor;
33
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;
44
using System.Runtime.InteropServices;
5+
using Rubberduck.Common;
56
using Rubberduck.Parsing.Symbols;
67
using Rubberduck.Parsing.VBA;
78
using Rubberduck.Refactorings.Rename;
@@ -27,21 +28,28 @@ public override void Execute(object parameter)
2728
{
2829
if (Vbe.ActiveCodePane == null) { return; }
2930

31+
Declaration target;
32+
if (parameter != null)
33+
{
34+
target = parameter as Declaration;
35+
}
36+
else
37+
{
38+
var selection = Vbe.ActiveCodePane.GetSelection();
39+
target = _state.AllUserDeclarations.FindTarget(selection);
40+
}
41+
42+
if (target == null)
43+
{
44+
return;
45+
}
46+
3047
using (var view = new RenameDialog())
3148
{
3249
var factory = new RenamePresenterFactory(Vbe, view, _state, new MessageBox(), _wrapperWrapperFactory);
3350
var refactoring = new RenameRefactoring(factory, Editor, new MessageBox(), _state);
3451

35-
var target = parameter as Declaration;
36-
37-
if (target == null)
38-
{
39-
refactoring.Refactor();
40-
}
41-
else
42-
{
43-
refactoring.Refactor(target);
44-
}
52+
refactoring.Refactor(target);
4553
}
4654
}
4755

0 commit comments

Comments
 (0)