Skip to content

Commit dc9039c

Browse files
committed
Increase performance of GetAnnotatedLine
This switches getting the first endOfLine to a new dedicated extension that takes as much shortcuts as possible.
1 parent 84cf953 commit dc9039c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Rubberduck.Parsing/Annotations/AnnotationBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected AnnotationBase(AnnotationType annotationType, QualifiedSelection quali
3636
//Annotations on the same line as non-whitespace statements do not scope to anything.
3737
if (enclosingEndOfStatement.Start.TokenIndex != 0)
3838
{
39-
var firstEndOfLine = enclosingEndOfStatement.GetDescendent<VBAParser.EndOfLineContext>();
39+
var firstEndOfLine = enclosingEndOfStatement.GetFirstEndOfLine();
4040
var parentEndOfLine = Context.GetAncestor<VBAParser.EndOfLineContext>();
4141
if (firstEndOfLine.Equals(parentEndOfLine))
4242
{

Rubberduck.Parsing/ParserRuleContextExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,25 @@ public static bool TryGetChildContext<TContext>(this ParserRuleContext ctxt, out
221221
return opCtxt != null;
222222
}
223223

224+
/// <summary>
225+
/// Returns the endOfStatementContext's first endOfLine context.
226+
/// </summary>
227+
public static VBAParser.EndOfLineContext GetFirstEndOfLine(this VBAParser.EndOfStatementContext endOfStatement)
228+
{
229+
//This dedicated method exists for performance reasons on hot-paths.
230+
var individualEndOfStatements = endOfStatement.individualNonEOFEndOfStatement();
231+
foreach (var individualEndOfStatement in individualEndOfStatements)
232+
{
233+
var endOfLine = individualEndOfStatement.endOfLine();
234+
if (endOfLine != null)
235+
{
236+
return endOfLine;
237+
}
238+
}
239+
//The only remaining alternative is whitespace followed by an EOF.
240+
return null;
241+
}
242+
224243
/// <summary>
225244
/// Determines if the context's module declares or defaults to
226245
/// Option Compare Binary

0 commit comments

Comments
 (0)