Skip to content

Commit

Permalink
Improve performance of UseConsistentIndentation even more > another 1…
Browse files Browse the repository at this point in the history
…0% speedup for formatter (#1461)

* Improve performance of UseConsistentIndentation -more > another 10% speedup for formatter

* Update Rules/UseConsistentIndentation.cs

Co-Authored-By: Robert Holt <[email protected]>

Co-authored-by: Christoph Bergmeister <[email protected]>
Co-authored-by: Robert Holt <[email protected]>
  • Loading branch information
3 people authored Apr 24, 2020
1 parent 5f30746 commit eefa971
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -286,6 +286,27 @@ 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 &&
Expand Down

0 comments on commit eefa971

Please sign in to comment.