Skip to content

Commit b49a62a

Browse files
committed
Moved 'GetDeclarationAccessibleToScope(...) to DeclarationFinder per PR comment.
1 parent b7be5f4 commit b49a62a

File tree

4 files changed

+62
-70
lines changed

4 files changed

+62
-70
lines changed

RetailCoder.VBE/Inspections/AccessibilityEvaluator.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.

RetailCoder.VBE/Refactorings/Rename/RenameRefactoring.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
using Rubberduck.VBEditor;
1515
using Rubberduck.VBEditor.SafeComWrappers;
1616
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
17-
using System.Collections.Generic;
18-
using Rubberduck.Inspections;
17+
using System;
1918

2019
namespace Rubberduck.Refactorings.Rename
2120
{
@@ -102,8 +101,8 @@ public void Refactor(Declaration target)
102101

103102
private void Rename()
104103
{
105-
var declaration = AccessibilityEvaluator.GetDeclarationsAccessibleToScope(_model.Target, _model.Declarations)
106-
.Where(d => d.IdentifierName == _model.NewName).FirstOrDefault();
104+
var declaration = _state.DeclarationFinder.GetDeclarationsAccessibleToScope(_model.Target, _model.Declarations)
105+
.Where(d => d.IdentifierName.Equals(_model.NewName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
107106
if (declaration != null)
108107
{
109108
var message = string.Format(RubberduckUI.RenameDialog_ConflictingNames, _model.NewName,

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@
341341
<Compile Include="Common\WinAPI\WindowLongFlags.cs" />
342342
<Compile Include="Common\WindowsOperatingSystem.cs" />
343343
<Compile Include="Common\UndocumentedAttribute.cs" />
344-
<Compile Include="Inspections\AccessibilityEvaluator.cs" />
345344
<Compile Include="Inspections\ApplicationWorksheetFunctionInspection.cs" />
346345
<Compile Include="Inspections\HostSpecificExpressionInspection.cs" />
347346
<Compile Include="Inspections\HungarianNotationInspection.cs" />

Rubberduck.Parsing/Symbols/DeclarationFinder.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,5 +747,64 @@ private ConcurrentBag<Declaration> FindEventHandlers(IEnumerable<Declaration> de
747747

748748
return new ConcurrentBag<Declaration>(handlers);
749749
}
750+
751+
public IEnumerable<Declaration> GetDeclarationsAccessibleToScope(Declaration target, IEnumerable<Declaration> declarations)
752+
{
753+
754+
object obj = new object();
755+
lock (obj)
756+
{
757+
if (target == null) { return Enumerable.Empty<Declaration>(); }
758+
759+
return declarations
760+
.Where(candidateDeclaration =>
761+
(
762+
IsDeclarationInTheSameProcedure(candidateDeclaration, target)
763+
|| IsDeclarationChildOfTheScope(candidateDeclaration, target)
764+
|| IsModuleLevelDeclarationOfTheScope(candidateDeclaration, target)
765+
|| IsProjectGlobalDeclaration(candidateDeclaration, target)
766+
)).Distinct();
767+
}
768+
}
769+
770+
private bool IsDeclarationInTheSameProcedure(Declaration candidateDeclaration, Declaration scopingDeclaration)
771+
{
772+
return candidateDeclaration.ParentScope == scopingDeclaration.ParentScope;
773+
}
774+
775+
private bool IsDeclarationChildOfTheScope(Declaration candidateDeclaration, Declaration scopingDeclaration)
776+
{
777+
return scopingDeclaration == candidateDeclaration.ParentDeclaration;
778+
}
779+
780+
private bool IsModuleLevelDeclarationOfTheScope(Declaration candidateDeclaration, Declaration scopingDeclaration)
781+
{
782+
if (candidateDeclaration.ParentDeclaration == null)
783+
{
784+
return false;
785+
}
786+
return candidateDeclaration.ComponentName == scopingDeclaration.ComponentName
787+
&& !IsDeclaredWithinMethodOrProperty(candidateDeclaration.ParentDeclaration.Context);
788+
}
789+
790+
private bool IsProjectGlobalDeclaration(Declaration candidateDeclaration, Declaration scopingDeclaration)
791+
{
792+
return candidateDeclaration.ProjectName == scopingDeclaration.ProjectName
793+
&& !(candidateDeclaration.ParentScopeDeclaration is ClassModuleDeclaration)
794+
&& (candidateDeclaration.Accessibility == Accessibility.Public
795+
|| ((candidateDeclaration.Accessibility == Accessibility.Implicit)
796+
&& (candidateDeclaration.ParentScopeDeclaration is ProceduralModuleDeclaration)));
797+
}
798+
799+
private bool IsDeclaredWithinMethodOrProperty(RuleContext procedureContextCandidate)
800+
{
801+
if (procedureContextCandidate == null) { return false; }
802+
803+
return (procedureContextCandidate is VBAParser.SubStmtContext)
804+
|| (procedureContextCandidate is VBAParser.FunctionStmtContext)
805+
|| (procedureContextCandidate is VBAParser.PropertyLetStmtContext)
806+
|| (procedureContextCandidate is VBAParser.PropertyGetStmtContext)
807+
|| (procedureContextCandidate is VBAParser.PropertySetStmtContext);
808+
}
750809
}
751810
}

0 commit comments

Comments
 (0)