diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs index c92d7c291..43a4f071f 100644 --- a/Rules/UseConsistentIndentation.cs +++ b/Rules/UseConsistentIndentation.cs @@ -132,7 +132,8 @@ public override IEnumerable AnalyzeScript(Ast ast, string file var indentationLevel = 0; var currentIndenationLevelIncreaseDueToPipelines = 0; var onNewLine = true; - var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true); + var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true).ToList(); + int minimumPipelineAstIndex = 0; for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++) { var token = tokens[tokenIndex]; @@ -228,8 +229,7 @@ public override IEnumerable AnalyzeScript(Ast ast, string file if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; } // Check if the current token matches the end of a PipelineAst - var matchingPipeLineAstEnd = pipelineAsts.FirstOrDefault(pipelineAst => - PositionIsEqual(pipelineAst.Extent.EndScriptPosition, token.Extent.EndScriptPosition)) as PipelineAst; + PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token); if (matchingPipeLineAstEnd == null) { continue; @@ -286,6 +286,27 @@ private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipel return lastPipeOnFirstLineWithPipeUsage; } + private static PipelineAst MatchingPipelineAstEnd(List pipelineAsts, ref int minimumPipelineAstIndex, Token token) + { + PipelineAst matchingPipeLineAstEnd = null; + for (int i = minimumPipelineAstIndex; i < pipelineAsts.Count; i++) + { + if (pipelineAsts[i].Extent.EndScriptPosition.LineNumber > token.Extent.EndScriptPosition.LineNumber) + { + break; + } + + if (PositionIsEqual(pipelineAsts[i].Extent.EndScriptPosition, token.Extent.EndScriptPosition)) + { + matchingPipeLineAstEnd = pipelineAsts[i] as PipelineAst; + minimumPipelineAstIndex = i; + break; + } + } + + return matchingPipeLineAstEnd; + } + private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2) { return position1.ColumnNumber == position2.ColumnNumber &&