Skip to content

Commit 58518f5

Browse files
committed
Updated per comment to limit fetching AllUserDeclarations just once. Various name changes
1 parent 81e96de commit 58518f5

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

RetailCoder.VBE/Inspections/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFix.cs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void InsertLocalVariableDeclarationAndAssignment()
100100
{
101101
string[] lines = { BuildLocalCopyDeclaration(), BuildLocalCopyAssignment() };
102102
var module = Selection.QualifiedName.Component.CodeModule;
103-
module.InsertLines(((VBAParser.ArgListContext)_target.Context.Parent).Stop.Line+1, lines);
103+
module.InsertLines(((VBAParser.ArgListContext)_target.Context.Parent).Stop.Line + 1, lines);
104104
}
105105

106106
private string BuildLocalCopyDeclaration()
@@ -116,50 +116,54 @@ private string BuildLocalCopyAssignment()
116116

117117
private IEnumerable<string> GetIdentifierNamesAccessibleToProcedureContext()
118118
{
119-
var allSameProcedureDeclarations = _parserState.AllUserDeclarations
120-
.Where(item => item.ParentScope == _target.ParentScope)
121-
.ToList();
122-
123-
var sameModuleDeclarations = _parserState.AllUserDeclarations
124-
.Where(item => item.ComponentName == _target.ComponentName
125-
&& !IsDeclaredInMethodOrProperty(item.ParentDeclaration.Context))
126-
.ToList();
127-
128-
var allGloballyAccessibleDeclarations = _parserState.AllUserDeclarations
129-
.Where(item => item.ProjectName == _target.ProjectName
130-
&& !(item.ParentScopeDeclaration is ClassModuleDeclaration)
131-
&& (item.Accessibility == Accessibility.Public
132-
|| ((item.Accessibility == Accessibility.Implicit)
133-
&& (item.ParentScopeDeclaration is ProceduralModuleDeclaration))))
134-
.ToList();
135-
136-
var accessibleIdentifierNames = new List<string>();
137-
accessibleIdentifierNames.AddRange(allSameProcedureDeclarations.Select(d => d.IdentifierName));
138-
accessibleIdentifierNames.AddRange(sameModuleDeclarations.Select(d => d.IdentifierName));
139-
accessibleIdentifierNames.AddRange(allGloballyAccessibleDeclarations.Select(d => d.IdentifierName));
140-
141-
return accessibleIdentifierNames.Distinct();
119+
return _parserState.AllUserDeclarations
120+
.Where(candidateDeclaration =>
121+
(
122+
IsDeclarationInTheSameProcedure(candidateDeclaration, _target)
123+
|| IsDeclarationInTheSameModule(candidateDeclaration, _target)
124+
|| IsProjectGlobalDeclaration(candidateDeclaration, _target))
125+
).Select(declaration => declaration.IdentifierName).Distinct();
142126
}
143127

144-
private bool IsDeclaredInMethodOrProperty(RuleContext context)
128+
private bool IsDeclarationInTheSameProcedure(Declaration candidateDeclaration, Declaration scopingDeclaration)
145129
{
146-
if (context is VBAParser.SubStmtContext)
130+
return candidateDeclaration.ParentScope == scopingDeclaration.ParentScope;
131+
}
132+
133+
private bool IsDeclarationInTheSameModule(Declaration candidateDeclaration, Declaration scopingDeclaration)
134+
{
135+
return candidateDeclaration.ComponentName == scopingDeclaration.ComponentName
136+
&& !IsDeclaredInMethodOrProperty(candidateDeclaration.ParentDeclaration.Context);
137+
}
138+
139+
private bool IsProjectGlobalDeclaration(Declaration candidateDeclaration, Declaration scopingDeclaration)
140+
{
141+
return candidateDeclaration.ProjectName == scopingDeclaration.ProjectName
142+
&& !(candidateDeclaration.ParentScopeDeclaration is ClassModuleDeclaration)
143+
&& (candidateDeclaration.Accessibility == Accessibility.Public
144+
|| ((candidateDeclaration.Accessibility == Accessibility.Implicit)
145+
&& (candidateDeclaration.ParentScopeDeclaration is ProceduralModuleDeclaration)));
146+
}
147+
148+
private bool IsDeclaredInMethodOrProperty(RuleContext procedureContext)
149+
{
150+
if (procedureContext is VBAParser.SubStmtContext)
147151
{
148152
return true;
149153
}
150-
else if (context is VBAParser.FunctionStmtContext)
154+
else if (procedureContext is VBAParser.FunctionStmtContext)
151155
{
152156
return true;
153157
}
154-
else if (context is VBAParser.PropertyLetStmtContext)
158+
else if (procedureContext is VBAParser.PropertyLetStmtContext)
155159
{
156160
return true;
157161
}
158-
else if (context is VBAParser.PropertyGetStmtContext)
162+
else if (procedureContext is VBAParser.PropertyGetStmtContext)
159163
{
160164
return true;
161165
}
162-
else if (context is VBAParser.PropertySetStmtContext)
166+
else if (procedureContext is VBAParser.PropertySetStmtContext)
163167
{
164168
return true;
165169
}

0 commit comments

Comments
 (0)