From b82a46aa3b7fac7355ffebeee060e8316ba61922 Mon Sep 17 00:00:00 2001
From: Christoph Bergmeister <christoph.bergmeister@bjss.com>
Date: Fri, 24 Apr 2020 20:28:31 +0100
Subject: [PATCH 1/2] Improve performance of UseConsistentIndentation -more >
 another 10% speedup for formatter

---
 Rules/UseConsistentIndentation.cs | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)

diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs
index c92d7c291..854ee6a20 100644
--- a/Rules/UseConsistentIndentation.cs
+++ b/Rules/UseConsistentIndentation.cs
@@ -132,7 +132,8 @@ public override IEnumerable<DiagnosticRecord> 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<DiagnosticRecord> 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,26 @@ private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipel
             return lastPipeOnFirstLineWithPipeUsage;
         }
 
+        private static PipelineAst MatchingPipelineAstEnd(List<Ast> 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 &&

From d7e1ce63fca8820a8c8e344bddf4b463120671d0 Mon Sep 17 00:00:00 2001
From: "Christoph Bergmeister [MVP]" <c.bergmeister@gmail.com>
Date: Fri, 24 Apr 2020 21:05:22 +0100
Subject: [PATCH 2/2] Update Rules/UseConsistentIndentation.cs

Co-Authored-By: Robert Holt <rjmholt@gmail.com>
---
 Rules/UseConsistentIndentation.cs | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Rules/UseConsistentIndentation.cs b/Rules/UseConsistentIndentation.cs
index 854ee6a20..43a4f071f 100644
--- a/Rules/UseConsistentIndentation.cs
+++ b/Rules/UseConsistentIndentation.cs
@@ -295,6 +295,7 @@ private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, ref in
                 {
                     break;
                 }
+
                 if (PositionIsEqual(pipelineAsts[i].Extent.EndScriptPosition, token.Extent.EndScriptPosition))
                 {
                     matchingPipeLineAstEnd = pipelineAsts[i] as PipelineAst;