Skip to content

Instantly share code, notes, and snippets.

@chenbojian
Created March 29, 2023 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenbojian/259fe4746c023371a87eaf4ebcc71647 to your computer and use it in GitHub Desktop.
Save chenbojian/259fe4746c023371a87eaf4ebcc71647 to your computer and use it in GitHub Desktop.
// See https://aka.ms/new-console-template for more information
using System.Text;
using System.Text.RegularExpressions;
var lines = File.ReadAllLines("error.txt");
var regex = new Regex(@"(C:\\workspace.+.cs)\((.+),(.+)\): Error CodeAnalyzer0001", RegexOptions.Compiled);
Console.WriteLine("start");
var fileRowsDict = new Dictionary<string, List<int>>();
foreach (var line in lines)
{
if (!line.Contains("CodeAnalyzer0001"))
{
continue;
}
var match = regex.Match(line);
if (match.Success)
{
// Console.WriteLine(match.Groups[1].Value + match.Groups[2].Value + match.Groups[3].Value);
if (!fileRowsDict.ContainsKey(match.Groups[1].Value))
{
fileRowsDict.Add(match.Groups[1].Value, new List<int>());
}
fileRowsDict[match.Groups[1].Value].Add(int.Parse(match.Groups[2].Value));
}
}
foreach (var f in fileRowsDict)
{
Console.WriteLine(f.Key);
var csLines = File.ReadAllLines(f.Key);
var newCsLines = InsertWarningDisable(csLines.ToList(), f.Value);
File.WriteAllLines(f.Key, newCsLines);
}
Console.WriteLine("Hello, World!");
List<string> InsertWarningDisable(List<string> lines, IEnumerable<int> rows)
{
var sortedRows = rows.OrderBy(r => r).ToList();
var newLines = new List<string>();
var lastRow = 0;
foreach (var row in sortedRows)
{
newLines.AddRange(lines.Skip(lastRow).Take(row - lastRow - 1));
newLines.Add("#pragma warning disable CodeAnalyzer0001");
Console.WriteLine(lines[row-1]);
newLines.Add(lines[row - 1]);
newLines.Add("#pragma warning restore CodeAnalyzer0001");
lastRow = row;
}
newLines.AddRange(lines.Skip(lastRow).Take(lines.Count - lastRow));
return newLines;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment