@@ -131,9 +131,9 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
131131 var indentationLevel = 0 ;
132132 var onNewLine = true ;
133133 var pipelineAsts = ast . FindAll ( testAst => testAst is PipelineAst && ( testAst as PipelineAst ) . PipelineElements . Count > 1 , true ) ;
134- for ( int k = 0 ; k < tokens . Length ; k ++ )
134+ for ( int tokenIndex = 0 ; tokenIndex < tokens . Length ; tokenIndex ++ )
135135 {
136- var token = tokens [ k ] ;
136+ var token = tokens [ tokenIndex ] ;
137137
138138 if ( token . Kind == TokenKind . EndOfInput )
139139 {
@@ -151,8 +151,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
151151 break ;
152152
153153 case TokenKind . Pipe :
154- bool pipelineIsFollowedByNewlineOrLineContinuation = k < tokens . Length - 1 && k > 0 &&
155- ( tokens [ k + 1 ] . Kind == TokenKind . NewLine || tokens [ k + 1 ] . Kind == TokenKind . LineContinuation ) ;
154+ bool pipelineIsFollowedByNewlineOrLineContinuation = tokenIndex < tokens . Length - 1 && tokenIndex > 0 &&
155+ ( tokens [ tokenIndex + 1 ] . Kind == TokenKind . NewLine || tokens [ tokenIndex + 1 ] . Kind == TokenKind . LineContinuation ) ;
156156 if ( ! pipelineIsFollowedByNewlineOrLineContinuation )
157157 {
158158 break ;
@@ -164,7 +164,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
164164 }
165165 if ( pipelineIndentationStyle == PipelineIndentationStyle . IncreaseIndentationForFirstPipeline )
166166 {
167- bool isFirstPipeInPipeline = pipelineAsts . Any ( pipelineAst => PositionIsEqual ( ( ( PipelineAst ) pipelineAst ) . PipelineElements [ 0 ] . Extent . EndScriptPosition , tokens [ k - 1 ] . Extent . EndScriptPosition ) ) ;
167+ bool isFirstPipeInPipeline = pipelineAsts . Any ( pipelineAst => PositionIsEqual ( ( ( PipelineAst ) pipelineAst ) . PipelineElements [ 0 ] . Extent . EndScriptPosition , tokens [ tokenIndex - 1 ] . Extent . EndScriptPosition ) ) ;
168168 if ( isFirstPipeInPipeline )
169169 {
170170 AddViolation ( token , indentationLevel ++ , diagnosticRecords , ref onNewLine ) ;
@@ -191,19 +191,24 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
191191 var tempIndentationLevel = indentationLevel ;
192192
193193 // Check if the preceding character is an escape character
194- if ( k > 0 && tokens [ k - 1 ] . Kind == TokenKind . LineContinuation )
194+ if ( tokenIndex > 0 && tokens [ tokenIndex - 1 ] . Kind == TokenKind . LineContinuation )
195195 {
196196 ++ tempIndentationLevel ;
197197 }
198- else
198+
199+ // check for comments in between multi-line commands with line continuation
200+ if ( tokenIndex > 2 && tokens [ tokenIndex - 1 ] . Kind == TokenKind . NewLine
201+ && tokens [ tokenIndex - 2 ] . Kind == TokenKind . Comment )
199202 {
200- // Ignore comments
201- // Since the previous token is a newline token we start
202- // looking for comments at the token before the newline token.
203- int j = k - 2 ;
204- while ( j > 0 && tokens [ j ] . Kind == TokenKind . Comment )
203+ int searchForPrecedingLineContinuationIndex = tokenIndex - 2 ;
204+ while ( searchForPrecedingLineContinuationIndex > 0 && tokens [ searchForPrecedingLineContinuationIndex ] . Kind == TokenKind . Comment )
205+ {
206+ searchForPrecedingLineContinuationIndex -- ;
207+ }
208+
209+ if ( searchForPrecedingLineContinuationIndex >= 0 && tokens [ searchForPrecedingLineContinuationIndex ] . Kind == TokenKind . LineContinuation )
205210 {
206- -- j ;
211+ tempIndentationLevel ++ ;
207212 }
208213 }
209214
0 commit comments