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 -> 35% speedup for formatter #1458

Merged
Changes from all commits
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
22 changes: 18 additions & 4 deletions Rules/UseConsistentIndentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
}
}

var lineHasPipelineBeforeToken = tokens.Any(oneToken =>
oneToken.Kind == TokenKind.Pipe &&
oneToken.Extent.StartLineNumber == token.Extent.StartLineNumber &&
oneToken.Extent.StartColumnNumber < token.Extent.StartColumnNumber);
bool lineHasPipelineBeforeToken = LineHasPipelineBeforeToken(tokens, tokenIndex, token);

AddViolation(token, tempIndentationLevel, diagnosticRecords, ref onNewLine, lineHasPipelineBeforeToken);
}
Expand Down Expand Up @@ -257,6 +254,23 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
return diagnosticRecords;
}

private static bool LineHasPipelineBeforeToken(Token[] tokens, int tokenIndex, Token token)
{
int searchIndex = tokenIndex;
int searchLine = token.Extent.StartLineNumber;
do
{
searchLine = tokens[searchIndex].Extent.StartLineNumber;
int searchcolumn = tokens[searchIndex].Extent.StartColumnNumber;
if (tokens[searchIndex].Kind == TokenKind.Pipe && searchcolumn < token.Extent.StartColumnNumber)
{
return true;
}
searchIndex--;
} while (searchLine == token.Extent.StartLineNumber && searchIndex >= 0);
return false;
}

private static CommandBaseAst LastPipeOnFirstLineWithPipeUsage(PipelineAst pipelineAst)
{
CommandBaseAst lastPipeOnFirstLineWithPipeUsage = pipelineAst.PipelineElements[0];
Expand Down