Skip to content

Commit 3ce6d17

Browse files
committed
Consolidated files - remove PRCHelper
1 parent 0a89264 commit 3ce6d17

10 files changed

+231
-414
lines changed

Rubberduck.Inspections/Concrete/FunctionReturnValueNotUsedInspection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ private bool IsReturnValueUsed(Declaration function)
8888

8989
private bool IsAddressOfCall(IdentifierReference usage)
9090
{
91-
return usage.Context.HasParent<VBAParser.AddressOfExpressionContext>();
91+
return usage.Context.IsDescendentOf<VBAParser.AddressOfExpressionContext>();
9292
}
9393

9494
private bool IsTypeOfExpression(IdentifierReference usage)
9595
{
96-
return usage.Context.HasParent<VBAParser.TypeofexprContext>();
96+
return usage.Context.IsDescendentOf<VBAParser.TypeofexprContext>();
9797
}
9898

9999
private bool IsReturnStatement(Declaration function, IdentifierReference assignment)
@@ -118,7 +118,7 @@ private bool IsCallStmt(IdentifierReference usage)
118118
{
119119
return true;
120120
}
121-
return !usage.Context.HasParent(argumentList);
121+
return !usage.Context.IsDescendentOf(argumentList);
122122
}
123123

124124
private bool IsIndexExprContext(IdentifierReference usage)
@@ -133,7 +133,7 @@ private bool IsIndexExprContext(IdentifierReference usage)
133133
{
134134
return true;
135135
}
136-
return !usage.Context.HasParent(argumentList);
136+
return !usage.Context.IsDescendentOf(argumentList);
137137
}
138138

139139
private bool IsLet(IdentifierReference usage)

Rubberduck.Inspections/QuickFixes/AssignedByValParameterMakeLocalCopyQuickFix.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Rubberduck.Parsing.Inspections.Resources;
1515
using Rubberduck.Parsing.Rewriter;
1616
using Rubberduck.Parsing.VBA;
17-
using static Rubberduck.Parsing.Grammar.VBAParser;
1817
using System.Diagnostics;
1918

2019
namespace Rubberduck.Inspections.QuickFixes
@@ -33,8 +32,8 @@ public AssignedByValParameterMakeLocalCopyQuickFix(RubberduckParserState state,
3332

3433
public override void Fix(IInspectionResult result)
3534
{
36-
Debug.Assert(result.Target.Context.Parent is ArgListContext);
37-
Debug.Assert(null != ((ParserRuleContext)result.Target.Context.Parent.Parent).GetChild<EndOfStatementContext>());
35+
Debug.Assert(result.Target.Context.Parent is VBAParser.ArgListContext);
36+
Debug.Assert(null != ((ParserRuleContext)result.Target.Context.Parent.Parent).GetChild<VBAParser.EndOfStatementContext>());
3837

3938
var forbiddenNames = _parserState.DeclarationFinder.GetDeclarationsWithIdentifiersToAvoid(result.Target).Select(n => n.IdentifierName);
4039

@@ -121,13 +120,13 @@ private void InsertLocalVariableDeclarationAndAssignment(IModuleRewriter rewrite
121120
requiresAssignmentUsingSet ? $"{Tokens.Set} " : string.Empty,
122121
$"{localIdentifier} = {target.IdentifierName}");
123122

124-
var endOfStmtCtxt = ((ParserRuleContext)target.Context.Parent.Parent).GetChild<EndOfStatementContext>();
123+
var endOfStmtCtxt = ((ParserRuleContext)target.Context.Parent.Parent).GetChild<VBAParser.EndOfStatementContext>();
125124
var eosContent = endOfStmtCtxt.GetText();
126125
var idxLastNewLine = eosContent.LastIndexOf(Environment.NewLine);
127126
var endOfStmtCtxtComment = eosContent.Substring(0, idxLastNewLine);
128127
var endOfStmtCtxtEndFormat = eosContent.Substring(idxLastNewLine);
129128

130-
var insertCtxt = (ParserRuleContext)((ParserRuleContext)target.Context.Parent.Parent).GetChild<AsTypeClauseContext>();
129+
var insertCtxt = (ParserRuleContext)((ParserRuleContext)target.Context.Parent.Parent).GetChild<VBAParser.AsTypeClauseContext>();
131130
if (insertCtxt == null)
132131
{
133132
insertCtxt = (ParserRuleContext)target.Context.Parent;

Rubberduck.Parsing/ParserRuleContextExtensions.cs

Lines changed: 71 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static Selection GetSelection(this ParserRuleContext context)
2626
context.Stop.EndColumn() + 1);
2727
}
2828

29+
//TODO: Question - remove? None of the GetProcedureSelection overloads below are referenced (except for one...in a test)
30+
//https://github.com/rubberduck-vba/Rubberduck/issues/2164
2931
//This set of overloads returns the selection for the entire procedure statement body, i.e. Public Function Foo(bar As String) As String
3032
public static Selection GetProcedureSelection(this VBAParser.FunctionStmtContext context) { return GetProcedureContextSelection(context); }
3133
public static Selection GetProcedureSelection(this VBAParser.SubStmtContext context) { return GetProcedureContextSelection(context); }
@@ -35,126 +37,83 @@ public static Selection GetSelection(this ParserRuleContext context)
3537

3638
private static Selection GetProcedureContextSelection(ParserRuleContext context)
3739
{
38-
var endContext = context.GetRuleContext<VBAParser.EndOfStatementContext>(0);
40+
var endContext1 = context.GetRuleContext<VBAParser.EndOfStatementContext>(0);
41+
var endContext = context.GetChild<VBAParser.EndOfStatementContext>();
3942
return new Selection(context.Start.Line == 0 ? 1 : context.Start.Line,
4043
context.Start.Column + 1,
4144
endContext.Start.Line == 0 ? 1 : endContext.Start.Line,
4245
endContext.Start.Column + 1);
4346
}
4447

45-
//public static IEnumerable<TContext> FindChildrenX<TContext>(this ParserRuleContext context) where TContext : ParserRuleContext
46-
//{
47-
// var walker = new ParseTreeWalker();
48-
// var listener = new ChildNodeListener<TContext>();
49-
// walker.Walk(listener, context);
50-
// return listener.Matches;
51-
//}
52-
48+
/// <summary>
49+
/// Gets the tokens belonging to the context from the token stream.
50+
/// </summary>
5351
public static IEnumerable<IToken> GetTokens(this ParserRuleContext context, CommonTokenStream tokenStream)
5452
{
5553
var sourceInterval = context.SourceInterval;
5654
if (sourceInterval.Equals(Interval.Invalid) || sourceInterval.b < sourceInterval.a)
5755
{
5856
return new List<IToken>();
5957
}
60-
// Gets the tokens belonging to the context from the token stream.
6158
return tokenStream.GetTokens(sourceInterval.a, sourceInterval.b);
6259
}
6360

61+
/// <summary>
62+
/// Gets the original source, without "synthetic" text such as "<EOF>
63+
/// </summary>
6464
public static string GetText(this ParserRuleContext context, ICharStream stream)
6565
{
6666
// Can be null if the input is empty it seems.
6767
if (context.Stop == null)
6868
{
6969
return string.Empty;
7070
}
71-
// Gets the original source, without "synthetic" text such as "<EOF>".
7271
return stream.GetText(new Interval(context.Start.StartIndex, context.Stop.StopIndex));
7372
}
7473

75-
public static T GetChild<T>(this ParserRuleContext context)
74+
/// <summary>
75+
/// Returns the first direct child of 'context' that is of the generic Type.
76+
/// </summary>
77+
public static TContext GetChild<TContext>(this ParserRuleContext context) where TContext : ParserRuleContext
7678
{
7779
if (context == null)
7880
{
7981
return default;
8082
}
8183

82-
for (var index = 0; index < context.ChildCount; index++)
84+
var results = context.children.Where(child => child is TContext);
85+
return results.Any() ? (TContext)results.First() : default;
86+
}
87+
88+
/// <summary>
89+
/// Determines if any of the context's ancestors are the generic Type.
90+
/// </summary>
91+
public static bool IsDescendentOf<TContext>(this ParserRuleContext context)
92+
{
93+
if(context is TContext)
8394
{
84-
var child = context.GetChild(index);
85-
if (context.GetChild(index) is T)
86-
{
87-
return (T)child;
88-
}
95+
return GetParent_Recursive<TContext>((ParserRuleContext)context.Parent) != null;
8996
}
90-
91-
return default;
97+
return GetParent_Recursive<TContext>(context) != null;
9298
}
9399

94-
//public static IEnumerable<T> GetChildren<T>(this RuleContext context)
95-
//{
96-
// if (context == null)
97-
// {
98-
// yield break;
99-
// }
100-
101-
// for (var index = 0; index < context.ChildCount; index++)
102-
// {
103-
// var child = context.GetChild(index);
104-
// if (child is T)
105-
// {
106-
// yield return (T)child;
107-
// }
108-
// }
109-
//}
110-
111-
public static T GetParent<T>(this ParserRuleContext context)
100+
/// <summary>
101+
/// Determines if any of the context's ancestors are equal to the parameter 'parent'.
102+
/// </summary>
103+
public static bool IsDescendentOf<T>(this ParserRuleContext context, T parent) where T : ParserRuleContext
112104
{
113-
if (context == null)
105+
if (context == null || parent == null)
114106
{
115-
return default;
107+
return false;
116108
}
117-
if (context is T)
109+
if (context != parent)
118110
{
119-
return (T)System.Convert.ChangeType(context, typeof(T));
111+
return IsDescendentOf_Recursive(context, parent);
120112
}
121-
//return GetMyParent<T>(context); //.Parent);
122-
return GetParent<T>((ParserRuleContext)context.Parent);
113+
return false;
123114
}
124115

125-
//private static T GetMyParent<T>(ParserRuleContext context)
126-
//{
127-
// if (context == null)
128-
// {
129-
// return default;
130-
// }
131-
// if (context is T)
132-
// {
133-
// return (T)System.Convert.ChangeType(context, typeof(T));
134-
// }
135-
// return GetMyParent<T>((ParserRuleContext)context.Parent);
136-
//}
137-
138-
public static bool HasParent<TContext>(this ParserRuleContext context)
139-
{
140-
//return TryGetAncestor(context, out TContext _);
141-
return GetParent<TContext>(context) != null;
142-
}
143-
144-
public static bool HasParent(this ParserRuleContext context, IParseTree parent)
145-
{
146-
return HasParent((IParseTree)context, parent);
147-
//if (TryGetAncestor(context, out IParseTree ancestor))
148-
//{
149-
// if (ancestor == parent)
150-
// {
151-
// return true;
152-
// }
153-
//}
154-
//return HasParent(ancestor, parent);
155-
}
156-
157-
private static bool HasParent(this IParseTree context, IParseTree targetParent)
116+
private static bool IsDescendentOf_Recursive(IParseTree context, IParseTree targetParent)
158117
{
159118
if (context == null)
160119
{
@@ -164,90 +123,63 @@ private static bool HasParent(this IParseTree context, IParseTree targetParent)
164123
{
165124
return true;
166125
}
167-
return HasParent(context.Parent, targetParent);
126+
return IsDescendentOf_Recursive(context.Parent, targetParent);
168127
}
169128

170-
private static bool TryGetAncestor<TContext>(this ParserRuleContext context, out TContext ancestor)
171-
{
172-
ancestor = GetParent<TContext>(context);
173-
return ancestor != null;
174-
}
129+
//TODO: Question: I would like to rename GetParent<T>(...) function to GetAncestor<T>(...)
130+
//With a name like GetParent, I would expected the code to simply be: "return context.Parent;"
131+
//This function is called when the caller is interested in looking 'up' the hierarchy including
132+
//and above the immediate parent.
175133

176-
public static TContext FindChild<TContext>(this ParserRuleContext context) where TContext : ParserRuleContext
134+
/// <summary>
135+
/// Returns the context's first ancestor of the generic Type.
136+
/// </summary>
137+
public static TContext GetParent<TContext>(this ParserRuleContext context)
177138
{
178139
if (context == null)
179140
{
180141
return default;
181142
}
182-
183-
for (var index = 0; index < context.ChildCount; index++)
143+
if (context is TContext)
184144
{
185-
var child = context.GetChild(index);
186-
if (context.GetChild(index) is TContext)
187-
{
188-
return (TContext)child;
189-
}
145+
return GetParent_Recursive<TContext>((ParserRuleContext)context.Parent);
190146
}
191-
return default;
192-
}
193-
194-
//public static bool HasChildToken(this IParseTree context, string token)
195-
//{
196-
// for (var index = 0; index < context.ChildCount; index++)
197-
// {
198-
// var child = context.GetChild(index);
199-
// if (context.GetChild(index).GetText().Equals(token))
200-
// {
201-
// return true;
202-
// }
203-
// }
204-
// return false;
205-
//}
206-
207-
public static bool HasChildToken(this ParserRuleContext context, string token)
208-
{
209-
return context.children.Any(ch => ch.GetText().Equals(token));
210-
//for (var index = 0; index < context.ChildCount; index++)
211-
//{
212-
// var child = context.GetChild(index);
213-
// if (context.GetChild(index).GetText().Equals(token))
214-
// {
215-
// return true;
216-
// }
217-
//}
218-
//return false;
219-
}
220-
221-
public static T GetDescendent<T>(this ParserRuleContext context)
222-
{
223-
return GetDescendent<T>((IParseTree)context);
147+
return GetParent_Recursive<TContext>(context);
224148
}
225149

226-
private static T GetDescendent<T>(this IParseTree context)
150+
private static TContext GetParent_Recursive<TContext>(ParserRuleContext context)
227151
{
228152
if (context == null)
229153
{
230154
return default;
231155
}
232-
233-
for (var index = 0; index < context.ChildCount; index++)
156+
if (context is TContext)
234157
{
235-
var child = context.GetChild(index);
236-
if (context.GetChild(index) is T)
237-
{
238-
return (T)child;
239-
}
240-
241-
var descendent = child.GetDescendent<T>();
242-
if (descendent != null)
243-
{
244-
return descendent;
245-
}
158+
return (TContext)System.Convert.ChangeType(context, typeof(TContext));
246159
}
160+
return GetParent_Recursive<TContext>((ParserRuleContext)context.Parent);
161+
}
247162

248-
return default;
163+
/// <summary>
164+
/// Determines if the any of the child contexts child.GetText() equals token .
165+
/// </summary>
166+
public static bool HasChildToken(this ParserRuleContext context, string token)
167+
{
168+
return context.children.Any(ch => ch.GetText().Equals(token));
169+
}
170+
171+
/// <summary>
172+
/// Returns the context's first descendent of the generic Type.
173+
/// </summary>
174+
public static TContext GetDescendent<TContext>(this ParserRuleContext context) where TContext : ParserRuleContext
175+
{
176+
var descendents = GetDescendents<TContext>(context);
177+
return descendents.Any() ? descendents.First() : null;
249178
}
250179

180+
/// <summary>
181+
/// Returns all the context's descendents of the generic Type.
182+
/// </summary>
251183
public static IEnumerable<TContext> GetDescendents<TContext>(this ParserRuleContext context) where TContext : ParserRuleContext
252184
{
253185
var walker = new ParseTreeWalker();

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@
318318
<Compile Include="Symbols\ExternalProcedureDeclaration.cs" />
319319
<Compile Include="Symbols\ICompilationPass.cs" />
320320
<Compile Include="Symbols\IDeclarationFinderFactory.cs" />
321-
<Compile Include="Symbols\ParserRuleContextHelper.cs" />
322321
<Compile Include="Symbols\BoundExpressionVisitor.cs" />
323322
<Compile Include="Symbols\ClassModuleDeclaration.cs" />
324323
<Compile Include="Symbols\CommentExtensions.cs" />

0 commit comments

Comments
 (0)