@@ -108,21 +108,18 @@ public override void Initialize(AnalysisContext context)
108
108
private static void HandleSyntaxTree ( SyntaxTreeAnalysisContext context )
109
109
{
110
110
var syntaxRoot = context . Tree . GetRoot ( context . CancellationToken ) ;
111
- var previousCommentNotOnOwnLine = false ;
112
111
113
112
foreach ( var trivia in syntaxRoot . DescendantTrivia ( ) . Where ( trivia => trivia . IsKind ( SyntaxKind . SingleLineCommentTrivia ) ) )
114
113
{
115
114
if ( trivia . FullSpan . Start == 0 )
116
115
{
117
116
// skip the trivia if it is at the start of the file
118
- previousCommentNotOnOwnLine = false ;
119
117
continue ;
120
118
}
121
119
122
120
if ( trivia . ToString ( ) . StartsWith ( "////" , StringComparison . Ordinal ) )
123
121
{
124
122
// ignore commented out code
125
- previousCommentNotOnOwnLine = false ;
126
123
continue ;
127
124
}
128
125
@@ -132,26 +129,21 @@ private static void HandleSyntaxTree(SyntaxTreeAnalysisContext context)
132
129
if ( ! IsOnOwnLine ( triviaList , triviaIndex ) )
133
130
{
134
131
// ignore comments after other code elements.
135
- previousCommentNotOnOwnLine = true ;
136
132
continue ;
137
133
}
138
134
139
135
if ( IsPrecededByBlankLine ( triviaList , triviaIndex ) )
140
136
{
141
137
// allow properly formatted blank line comments.
142
- previousCommentNotOnOwnLine = false ;
143
138
continue ;
144
139
}
145
140
146
- if ( ! previousCommentNotOnOwnLine && IsPrecededBySingleLineCommentOrDocumentation ( triviaList , triviaIndex ) )
141
+ if ( IsPrecededBySingleLineCommentOnOwnLineOrDocumentation ( triviaList , triviaIndex ) )
147
142
{
148
143
// allow consecutive single line comments.
149
- previousCommentNotOnOwnLine = false ;
150
144
continue ;
151
145
}
152
146
153
- previousCommentNotOnOwnLine = false ;
154
-
155
147
if ( IsAtStartOfScope ( trivia ) )
156
148
{
157
149
// allow single line comment at scope start.
@@ -174,7 +166,8 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
174
166
{
175
167
while ( triviaIndex >= 0 )
176
168
{
177
- if ( triviaList [ triviaIndex ] . IsKind ( SyntaxKind . EndOfLineTrivia ) )
169
+ var currentTrivia = triviaList [ triviaIndex ] ;
170
+ if ( currentTrivia . IsKind ( SyntaxKind . EndOfLineTrivia ) )
178
171
{
179
172
return true ;
180
173
}
@@ -185,7 +178,7 @@ private static bool IsOnOwnLine<T>(T triviaList, int triviaIndex)
185
178
return false ;
186
179
}
187
180
188
- private static bool IsPrecededBySingleLineCommentOrDocumentation < T > ( T triviaList , int triviaIndex )
181
+ private static bool IsPrecededBySingleLineCommentOnOwnLineOrDocumentation < T > ( T triviaList , int triviaIndex )
189
182
where T : IReadOnlyList < SyntaxTrivia >
190
183
{
191
184
var eolCount = 0 ;
@@ -206,6 +199,8 @@ private static bool IsPrecededBySingleLineCommentOrDocumentation<T>(T triviaList
206
199
break ;
207
200
208
201
case SyntaxKind . SingleLineCommentTrivia :
202
+ return IsOnOwnLine ( triviaList , triviaIndex ) ;
203
+
209
204
case SyntaxKind . SingleLineDocumentationCommentTrivia :
210
205
return true ;
211
206
0 commit comments