Skip to content

Commit b2aa796

Browse files
committed
Made FindMemberAnnotations able to deal with line continued annotations.
Before, the DeclarationSymbolsListener assumed to have at least one annotations starting in each physical line above the member still belonging to the annotations section for the member. That failed terribly when introducing line continuations. Only logical lines should count.
1 parent 03bfd16 commit b2aa796

File tree

3 files changed

+11
-23
lines changed

3 files changed

+11
-23
lines changed

Rubberduck.CodeAnalysis/QuickFixes/RemoveDuplicatedAnnotationQuickFix.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public override void Fix(IInspectionResult result)
4040
var annotationList = (VBAParser.AnnotationListContext)annotation.Context.Parent;
4141

4242
RemoveAnnotationMarker(annotationList, annotation, rewriter);
43-
RemoveWhiteSpaceAfterAnnotation(annotationList, annotation, rewriter);
4443

4544
rewriter.Remove(annotation.Context);
4645

@@ -71,18 +70,6 @@ private static void RemoveAnnotationMarker(VBAParser.AnnotationListContext annot
7170
rewriter.Remove(annotationList.AT(index));
7271
}
7372

74-
private static void RemoveWhiteSpaceAfterAnnotation(VBAParser.AnnotationListContext annotationList,
75-
IAnnotation annotation, IModuleRewriter rewriter)
76-
{
77-
var whitespace = annotationList.whiteSpace().FirstOrDefault(ws =>
78-
ws.Start.StartIndex == annotation.Context.Stop.StopIndex + 1);
79-
80-
if (whitespace != null)
81-
{
82-
rewriter.Remove(whitespace);
83-
}
84-
}
85-
8673
private static bool OnlyQuoteRemainedFromAnnotationList(KeyValuePair<VBAParser.AnnotationListContext, int> pair)
8774
{
8875
return pair.Key.annotation().Length == pair.Value && pair.Key.commentBody() == null;

Rubberduck.Parsing/Grammar/VBAParser.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,8 @@ commentOrAnnotation :
926926
remComment : REM whiteSpace? commentBody;
927927
comment : SINGLEQUOTE commentBody;
928928
commentBody : (~NEWLINE)*;
929-
annotationList : SINGLEQUOTE (AT annotation whiteSpace?)+ (COLON commentBody)?;
930-
annotation : annotationName annotationArgList?;
929+
annotationList : SINGLEQUOTE (AT annotation)+ (COLON commentBody)?;
930+
annotation : annotationName annotationArgList? whiteSpace?;
931931
annotationName : unrestrictedIdentifier;
932932
annotationArgList :
933933
whiteSpace annotationArg

Rubberduck.Parsing/Symbols/DeclarationSymbolsListener.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private IEnumerable<IAnnotation> FindAnnotations()
107107
return annotations.ToList();
108108
}
109109

110-
private IEnumerable<IAnnotation> FindAnnotations(int line)
110+
private IEnumerable<IAnnotation> FindMemberAnnotations(int firstMemberLine)
111111
{
112112
if (_annotations == null)
113113
{
@@ -117,16 +117,17 @@ private IEnumerable<IAnnotation> FindAnnotations(int line)
117117
var annotations = new List<IAnnotation>();
118118

119119
// VBE 1-based indexing
120-
for (var i = line - 1; i >= 1; i--)
120+
for (var currentLine = firstMemberLine - 1; currentLine >= 1; currentLine--)
121121
{
122-
var lineAnnotations = _annotations.Where(a => a.QualifiedSelection.Selection.StartLine == i);
123-
124-
if (!lineAnnotations.Any())
122+
if (!_annotations.Any(annotation => annotation.QualifiedSelection.Selection.StartLine <= currentLine
123+
&& annotation.QualifiedSelection.Selection.EndLine >= currentLine))
125124
{
126125
break;
127126
}
128127

129-
annotations.AddRange(lineAnnotations);
128+
var annotationsStartingOnCurrentLine = _annotations.Where(a => a.QualifiedSelection.Selection.StartLine == currentLine);
129+
130+
annotations.AddRange(annotationsStartingOnCurrentLine);
130131
}
131132

132133
return annotations;
@@ -225,7 +226,7 @@ private Declaration CreateDeclaration(
225226
_attributes.TryGetValue(key, out var attributes);
226227
_membersAllowingAttributes.TryGetValue(key, out var attributesPassContext);
227228

228-
var annotations = FindAnnotations(selection.StartLine);
229+
var annotations = FindMemberAnnotations(selection.StartLine);
229230
switch (declarationType)
230231
{
231232
case DeclarationType.Procedure:
@@ -821,7 +822,7 @@ public override void EnterConstSubStmt(VBAParser.ConstSubStmtContext context)
821822
asTypeName,
822823
asTypeClause,
823824
typeHint,
824-
FindAnnotations(constStmt.Start.Line),
825+
FindMemberAnnotations(constStmt.Start.Line),
825826
accessibility,
826827
DeclarationType.Constant,
827828
value,

0 commit comments

Comments
 (0)