Skip to content

Commit ef28979

Browse files
committed
refactored QualifiedModuleName and QualifiedMemberName, impacted multiple files including the whole unit testing engine.
1 parent e1608f8 commit ef28979

File tree

58 files changed

+288
-427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+288
-427
lines changed

RetailCoder.VBE/Extensions/VbeExtensions.cs

Lines changed: 31 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,23 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Linq;
4-
using Antlr4.Runtime;
52
using Microsoft.Vbe.Interop;
63
using Rubberduck.Parsing;
7-
using Rubberduck.Parsing.Nodes;
84
using Rubberduck.VBEHost;
95

106
namespace Rubberduck.Extensions
117
{
128
public static class VbeExtensions
139
{
14-
public static IEnumerable<CodeModule> FindCodeModules(this VBE vbe, QualifiedModuleName qualifiedName)
10+
public static CodeModule FindCodeModule(this VBE vbe, QualifiedModuleName qualifiedName)
1511
{
16-
return FindCodeModules(vbe, qualifiedName.ProjectName, qualifiedName.ModuleName);
17-
}
18-
19-
/// <summary>
20-
/// Finds all code modules that match the specified project and component names.
21-
/// </summary>
22-
/// <param name="vbe"></param>
23-
/// <param name="projectName"></param>
24-
/// <param name="componentName"></param>
25-
/// <returns></returns>
26-
public static IEnumerable<CodeModule> FindCodeModules(this VBE vbe, string projectName, string componentName)
27-
{
28-
var matches =
29-
vbe.VBProjects.Cast<VBProject>()
30-
.Where(project => project.Protection != vbext_ProjectProtection.vbext_pp_locked && project.Name == projectName)
31-
.SelectMany(project => project.VBComponents.Cast<VBComponent>()
32-
.Where(component => component.Name == componentName))
33-
.Select(component => component.CodeModule);
34-
return matches;
35-
}
36-
37-
public static CodeModuleSelection FindInstruction(this VBE vbe, CommentNode comment)
38-
{
39-
var modules = FindCodeModules(vbe, comment.QualifiedSelection.QualifiedName);
40-
foreach (var module in modules)
41-
{
42-
var selection = comment.QualifiedSelection.Selection;
43-
44-
if (module.Lines[selection.StartLine, selection.LineCount]
45-
.Replace(" _\n", " ").Contains(comment.Comment))
46-
{
47-
return new CodeModuleSelection(module, selection);
48-
}
49-
}
50-
51-
return null;
12+
var vbComponent = vbe.VBProjects.Cast<VBProject>()
13+
.Where(project => project.Protection != vbext_ProjectProtection.vbext_pp_locked
14+
&& project.Equals(qualifiedName.Project))
15+
.SelectMany(project => project.VBComponents.Cast<VBComponent>())
16+
.SingleOrDefault(component => component.Equals(qualifiedName.Component));
17+
18+
return vbComponent == null
19+
? null
20+
: vbComponent.CodeModule;
5221
}
5322

5423
public static void SetSelection(this VBE vbe, QualifiedSelection selection)
@@ -62,7 +31,7 @@ public static void SetSelection(this VBE vbe, QualifiedSelection selection)
6231
if (project != null)
6332
{
6433
component = project.VBComponents.Cast<VBComponent>()
65-
.FirstOrDefault(c => c.Name == selection.QualifiedName.ModuleName);
34+
.FirstOrDefault(c => c.Equals(selection.QualifiedName.Component));
6635
}
6736

6837
if (component == null)
@@ -73,61 +42,38 @@ public static void SetSelection(this VBE vbe, QualifiedSelection selection)
7342
component.CodeModule.CodePane.SetSelection(selection.Selection);
7443
}
7544

76-
[Obsolete]
77-
public static CodeModuleSelection FindInstruction(this VBE vbe, QualifiedModuleName qualifiedModuleName, ParserRuleContext context)
45+
public static CodeModuleSelection FindInstruction(this VBE vbe, QualifiedModuleName qualifiedModuleName, Selection selection)
7846
{
79-
var projectName = qualifiedModuleName.ProjectName;
80-
var componentName = qualifiedModuleName.ModuleName;
81-
82-
var modules = FindCodeModules(vbe, projectName, componentName).ToList();
83-
foreach (var module in modules)
47+
var module = FindCodeModule(vbe, qualifiedModuleName);
48+
if (module == null)
8449
{
85-
Selection selection;
86-
var text = " ";
87-
if (context == null)
88-
{
89-
selection = Selection.Home;
90-
}
91-
else
92-
{
93-
selection = context.GetSelection();
94-
text = context.GetText();
95-
}
96-
97-
if (module.Lines[selection.StartLine, selection.LineCount]
98-
.Replace(" _\n", " ").Contains(text))
99-
{
100-
return new CodeModuleSelection(module, selection);
101-
}
50+
return null;
10251
}
10352

104-
return new CodeModuleSelection(modules.First(), Selection.Home);
105-
}
106-
107-
public static CodeModuleSelection FindInstruction(this VBE vbe, QualifiedModuleName qualifiedModuleName, Selection selection)
108-
{
109-
var projectName = qualifiedModuleName.ProjectName;
110-
var componentName = qualifiedModuleName.ModuleName;
111-
112-
var modules = FindCodeModules(vbe, projectName, componentName).ToList();
113-
114-
return new CodeModuleSelection(modules.First(), selection);
53+
return new CodeModuleSelection(module, selection);
11554
}
11655

11756
/// <summary> Returns the type of Office Application that is hosting the VBE. </summary>
11857
/// <returns> Returns null if Unit Testing does not support Host Application.</returns>
11958
public static IHostApplication HostApplication(this VBE vbe)
12059
{
121-
foreach (Reference reference in vbe.ActiveVBProject.References)
60+
foreach (var reference in vbe.ActiveVBProject.References.Cast<Reference>()
61+
.Where(reference => reference.BuiltIn && reference.Name != "VBA"))
12262
{
123-
if (reference.BuiltIn && reference.Name != "VBA")
63+
switch (reference.Name)
12464
{
125-
if (reference.Name == "Excel") return new ExcelApp();
126-
if (reference.Name == "Access") return new AccessApp();
127-
if (reference.Name == "Word") return new WordApp();
128-
if (reference.Name == "PowerPoint") return new PowerPointApp();
129-
if (reference.Name == "Outlook") return new OutlookApp();
130-
if (reference.Name == "Publisher") return new PublisherApp();
65+
case "Excel":
66+
return new ExcelApp();
67+
case "Access":
68+
return new AccessApp();
69+
case "Word":
70+
return new WordApp();
71+
case "PowerPoint":
72+
return new PowerPointApp();
73+
case "Outlook":
74+
return new OutlookApp();
75+
case "Publisher":
76+
return new PublisherApp();
13177
}
13278
}
13379

RetailCoder.VBE/Inspections/AssignedByValParameterInspectionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void PassParameterByReference(VBE vbe)
3232
var newContent = string.Concat(Tokens.ByRef, " ", parameter.Replace(Tokens.ByVal, string.Empty).Trim());
3333
var selection = QualifiedSelection.Selection;
3434

35-
var module = vbe.FindCodeModules(QualifiedName.ProjectName, QualifiedName.ModuleName).First();
35+
var module = vbe.FindCodeModule(QualifiedName);
3636
var lines = module.get_Lines(selection.StartLine, selection.LineCount);
3737

3838
var result = lines.Replace(parameter, newContent);

RetailCoder.VBE/Inspections/CodeInspectionResultBase.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,17 @@ public virtual QualifiedSelection QualifiedSelection
7676

7777
public VBComponent FindComponent(VBE vbe)
7878
{
79-
return vbe.VBProjects.Cast<VBProject>()
80-
.Where(project => project.Protection != vbext_ProjectProtection.vbext_pp_locked && project.Name == QualifiedName.ProjectName)
81-
.SelectMany(project =>
82-
project.VBComponents.Cast<VBComponent>()
83-
.Where(component => component.Name == QualifiedName.ModuleName))
84-
.FirstOrDefault();
79+
var vbProject = vbe.VBProjects.Cast<VBProject>()
80+
.SingleOrDefault(project => project.Protection != vbext_ProjectProtection.vbext_pp_locked
81+
&& project.Equals(QualifiedName.Project));
82+
83+
if (vbProject == null)
84+
{
85+
return null;
86+
}
87+
88+
return vbProject.VBComponents.Cast<VBComponent>()
89+
.SingleOrDefault(component => component.Equals(QualifiedName.Component));
8590
}
8691
}
8792
}

RetailCoder.VBE/Inspections/IdentifierNotAssignedInspectionResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Antlr4.Runtime;
54
using Microsoft.Vbe.Interop;
65
using Rubberduck.Extensions;
@@ -27,7 +26,7 @@ public override IDictionary<string, Action<VBE>> GetQuickFixes()
2726

2827
protected override void RemoveUnusedDeclaration(VBE vbe)
2928
{
30-
var module = vbe.FindCodeModules(QualifiedName).First();
29+
var module = vbe.FindCodeModule(QualifiedName);
3130
var selection = QualifiedSelection.Selection;
3231

3332
var originalCodeLines = module.get_Lines(selection.StartLine, selection.LineCount)

RetailCoder.VBE/Inspections/IdentifierNotUsedInspectionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override IDictionary<string, Action<VBE>> GetQuickFixes()
2727

2828
protected virtual void RemoveUnusedDeclaration(VBE vbe)
2929
{
30-
var module = vbe.FindCodeModules(QualifiedName).First();
30+
var module = vbe.FindCodeModule(QualifiedName);
3131
var selection = QualifiedSelection.Selection;
3232

3333
var originalCodeLines = module.get_Lines(selection.StartLine, selection.LineCount)

RetailCoder.VBE/Inspections/ImplicitByRefParameterInspectionResult.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Microsoft.Vbe.Interop;
54
using Rubberduck.Extensions;
65
using Rubberduck.Parsing;
@@ -51,7 +50,7 @@ private void ChangeParameterPassing(VBE vbe, string newValue)
5150
var newContent = string.Concat(newValue, " ", parameter);
5251
var selection = QualifiedSelection.Selection;
5352

54-
var module = vbe.FindCodeModules(QualifiedName.ProjectName, QualifiedName.ModuleName).First();
53+
var module = vbe.FindCodeModule(QualifiedName);
5554
var lines = module.get_Lines(selection.StartLine, selection.LineCount);
5655

5756
var result = lines.Replace(parameter, newContent);

RetailCoder.VBE/Inspections/ImplicitPublicMemberInspectionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private void SpecifyPublicModifier(VBE vbe)
3131

3232
var selection = QualifiedSelection.Selection;
3333

34-
var module = vbe.FindCodeModules(QualifiedName.ProjectName, QualifiedName.ModuleName).First();
34+
var module = vbe.FindCodeModule(QualifiedName);
3535
var lines = module.get_Lines(selection.StartLine, selection.LineCount);
3636

3737
var result = lines.Replace(oldContent, newContent);

RetailCoder.VBE/Inspections/ImplicitVariantReturnTypeInspectionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void ReturnExplicitVariant(VBE vbe)
3939
var procedure = Context.GetText();
4040
var result = procedure.Replace(signature, signature + ' ' + Tokens.As + ' ' + Tokens.Variant);
4141

42-
var module = vbe.FindCodeModules(QualifiedName).First();
42+
var module = vbe.FindCodeModule(QualifiedName);
4343
var selection = Context.GetSelection();
4444

4545
module.DeleteLines(selection.StartLine, selection.LineCount);

RetailCoder.VBE/Inspections/MultipleDeclarationsInspectionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private void SplitDeclarations(VBE vbe)
9090
}
9191
}
9292

93-
var module = vbe.FindCodeModules(QualifiedName).First();
93+
var module = vbe.FindCodeModule(QualifiedName);
9494
module.ReplaceLine(selection.StartLine, newContent.ToString());
9595
}
9696
}

RetailCoder.VBE/Inspections/NonReturningFunctionInspectionResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void ConvertFunctionToProcedure(VBE vbe)
4747
.Replace(Tokens.End + ' ' + Tokens.Function, Tokens.End + ' ' + Tokens.Sub)
4848
.Replace(Tokens.Exit + ' ' + Tokens.Function, Tokens.Exit + ' ' + Tokens.Sub);
4949

50-
var module = vbe.FindCodeModules(QualifiedName).First();
50+
var module = vbe.FindCodeModule(QualifiedName);
5151
var selection = Context.GetSelection();
5252

5353
module.DeleteLines(selection.StartLine, selection.LineCount);

0 commit comments

Comments
 (0)