Skip to content

Commit eefa971

Browse files
bergmeisterChristoph Bergmeisterrjmholt
authored
Improve performance of UseConsistentIndentation even more > another 10% 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]>
1 parent 5f30746 commit eefa971

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

Rules/UseConsistentIndentation.cs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
132132
var indentationLevel = 0;
133133
var currentIndenationLevelIncreaseDueToPipelines = 0;
134134
var onNewLine = true;
135-
var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true);
135+
var pipelineAsts = ast.FindAll(testAst => testAst is PipelineAst && (testAst as PipelineAst).PipelineElements.Count > 1, true).ToList();
136+
int minimumPipelineAstIndex = 0;
136137
for (int tokenIndex = 0; tokenIndex < tokens.Length; tokenIndex++)
137138
{
138139
var token = tokens[tokenIndex];
@@ -228,8 +229,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
228229

229230
if (pipelineIndentationStyle == PipelineIndentationStyle.None) { break; }
230231
// Check if the current token matches the end of a PipelineAst
231-
var matchingPipeLineAstEnd = pipelineAsts.FirstOrDefault(pipelineAst =>
232-
PositionIsEqual(pipelineAst.Extent.EndScriptPosition, token.Extent.EndScriptPosition)) as PipelineAst;
232+
PipelineAst matchingPipeLineAstEnd = MatchingPipelineAstEnd(pipelineAsts, ref minimumPipelineAstIndex, token);
233233
if (matchingPipeLineAstEnd == null)
234234
{
235235
continue;
@@ -286,6 +286,27 @@ private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipel
286286
return lastPipeOnFirstLineWithPipeUsage;
287287
}
288288

289+
private static PipelineAst MatchingPipelineAstEnd(List<Ast> pipelineAsts, ref int minimumPipelineAstIndex, Token token)
290+
{
291+
PipelineAst matchingPipeLineAstEnd = null;
292+
for (int i = minimumPipelineAstIndex; i < pipelineAsts.Count; i++)
293+
{
294+
if (pipelineAsts[i].Extent.EndScriptPosition.LineNumber > token.Extent.EndScriptPosition.LineNumber)
295+
{
296+
break;
297+
}
298+
299+
if (PositionIsEqual(pipelineAsts[i].Extent.EndScriptPosition, token.Extent.EndScriptPosition))
300+
{
301+
matchingPipeLineAstEnd = pipelineAsts[i] as PipelineAst;
302+
minimumPipelineAstIndex = i;
303+
break;
304+
}
305+
}
306+
307+
return matchingPipeLineAstEnd;
308+
}
309+
289310
private static bool PositionIsEqual(IScriptPosition position1, IScriptPosition position2)
290311
{
291312
return position1.ColumnNumber == position2.ColumnNumber &&

0 commit comments

Comments
 (0)