Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of UseConsistentIndentation even more > another 10% speedup for formatter #1461

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 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,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;
}
bergmeister marked this conversation as resolved.
Show resolved Hide resolved
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