81 lines
3.4 KiB
C#
81 lines
3.4 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace FormatFixer
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var srcPath = @"e:\GIT\forkmessager\apps\server-net\src";
|
|
var files = Directory.GetFiles(srcPath, "*.cs", SearchOption.AllDirectories);
|
|
foreach (var file in files)
|
|
{
|
|
var lines = File.ReadAllLines(file);
|
|
bool changed = false;
|
|
for (int i = 0; i < lines.Length; i++)
|
|
{
|
|
var line = lines[i];
|
|
var stripped = line.TrimStart();
|
|
if ((stripped.StartsWith("if (") || stripped.StartsWith("if(") ||
|
|
stripped.StartsWith("foreach (") || stripped.StartsWith("foreach("))
|
|
&& stripped.EndsWith(";"))
|
|
{
|
|
// Check if it's already a block
|
|
if (stripped.Contains("{")) continue;
|
|
|
|
int parenCount = 0;
|
|
bool inString = false;
|
|
int firstParenIdx = line.IndexOf('(');
|
|
int condEndIdx = -1;
|
|
|
|
for (int j = firstParenIdx; j < line.Length; j++)
|
|
{
|
|
char c = line[j];
|
|
if (c == '"' && line[j - 1] != '\\')
|
|
{
|
|
inString = !inString;
|
|
}
|
|
else if (!inString)
|
|
{
|
|
if (c == '(') parenCount++;
|
|
else if (c == ')')
|
|
{
|
|
parenCount--;
|
|
if (parenCount == 0)
|
|
{
|
|
condEndIdx = j;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (condEndIdx != -1)
|
|
{
|
|
var statement = line.Substring(condEndIdx + 1).Trim();
|
|
if (!string.IsNullOrEmpty(statement) && !statement.StartsWith("{") && statement.EndsWith(";"))
|
|
{
|
|
var indent = line.Substring(0, line.Length - line.TrimStart().Length);
|
|
var newLine1 = line.Substring(0, condEndIdx + 1);
|
|
var newLine2 = indent + "{";
|
|
var newLine3 = indent + " " + statement;
|
|
var newLine4 = indent + "}";
|
|
|
|
lines[i] = newLine1 + Environment.NewLine + newLine2 + Environment.NewLine + newLine3 + Environment.NewLine + newLine4;
|
|
changed = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (changed)
|
|
{
|
|
File.WriteAllText(file, string.Join(Environment.NewLine, lines));
|
|
Console.WriteLine("Fixed: " + file);
|
|
}
|
|
}
|
|
Console.WriteLine("Formatting complete.");
|
|
}
|
|
}
|
|
}
|