@@ -131,9 +131,9 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
131
131
var indentationLevel = 0 ;
132
132
var onNewLine = true ;
133
133
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 ++ )
135
135
{
136
- var token = tokens [ k ] ;
136
+ var token = tokens [ tokenIndex ] ;
137
137
138
138
if ( token . Kind == TokenKind . EndOfInput )
139
139
{
@@ -151,8 +151,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
151
151
break ;
152
152
153
153
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 ) ;
156
156
if ( ! pipelineIsFollowedByNewlineOrLineContinuation )
157
157
{
158
158
break ;
@@ -164,7 +164,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
164
164
}
165
165
if ( pipelineIndentationStyle == PipelineIndentationStyle . IncreaseIndentationForFirstPipeline )
166
166
{
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 ) ) ;
168
168
if ( isFirstPipeInPipeline )
169
169
{
170
170
AddViolation ( token , indentationLevel ++ , diagnosticRecords , ref onNewLine ) ;
@@ -191,19 +191,24 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
191
191
var tempIndentationLevel = indentationLevel ;
192
192
193
193
// 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 )
195
195
{
196
196
++ tempIndentationLevel ;
197
197
}
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 )
199
202
{
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 )
205
210
{
206
- -- j ;
211
+ tempIndentationLevel ++ ;
207
212
}
208
213
}
209
214
0 commit comments