Skip to content

Commit

Permalink
Trim leading whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Sep 1, 2024
1 parent c0ca13e commit c181baa
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Runner/RegexDiffJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ await RunProcessAsync("git", $"diff --histogram {mainFile} {prFile}", shortDiffL
fullDiffLines.RemoveAll(ShouldSkipLine);
shortDiffLines.RemoveAll(ShouldSkipLine);
TrimExcessLeadingWhiteSpace(shortDiffLines);
entry.FullDiff = string.Join('\n', fullDiffLines);
entry.ShortDiff = string.Join('\n', shortDiffLines);
}
Expand All @@ -222,6 +224,62 @@ static bool ShouldSkipLine(string line)
line.StartsWith("@@", StringComparison.Ordinal) ||
line.StartsWith("\\ No newline at end of file", StringComparison.Ordinal);
}

static void TrimExcessLeadingWhiteSpace(List<string> lines)
{
if (lines.Count == 0)
return;

if (lines[0].AsSpan().TrimEnd().Length == 0)
{
lines.RemoveAt(0);
}

if (lines.Count == 0)
return;

if (lines[^1].AsSpan().TrimEnd().Length == 0)
{
lines.RemoveAt(lines.Count - 1);
}

if (lines.Count == 0)
return;

int minOffset = lines.Where(l => l.Length > 0).Min(CountWhiteSpace);

if (minOffset <= 3)
return;

minOffset -= 2;

for (int i = 0; i < lines.Count; i++)
{
string line = lines[i];
if (line.Length > 0)
{
lines[i] = line[0] is '+' or '-'
? $"{line[0]}{line.AsSpan(minOffset - 1)}"
: line.Substring(minOffset);
}
}

static int CountWhiteSpace(string line)
{
int i = 0;
if (line.StartsWith('+') || line.StartsWith('-'))
{
i++;
}

while (i < line.Length && char.IsWhiteSpace(line[i]))
{
i++;
}

return i;
}
}
}

private async Task ExtractSearchValuesInfoAsync(RegexEntry[] entries)
Expand Down

0 comments on commit c181baa

Please sign in to comment.