71ef79a9e2
新增功能: ✅ 代码格式化集成 (CodeFormatter.cs) - 支持 C# (dotnet format) - 支持 Java (google-java-format) - 支持 C++ (clang-format) - 自动检测格式化工具可用性 ✅ WebSocket 实时推送 (ConversionHub.cs) - SignalR Hub 实现 - 进度组管理 - 实时转换进度推送 ✅ Python 语言支持 - PythonParser.cs: Python 代码解析 - PythonToCSharpConverter.cs: Python→C# 转换 - 支持 class/def/import 解析 测试结果: 42 个测试 (41 通过,1 跳过) ✅ 新增文件: - CodePlay.Core/Services/CodeFormatter.cs - CodePlay.WebAPI/Hubs/ConversionHub.cs - CodePlay.Core/Parsers/PythonParser.cs - CodePlay.Core/Converters/PythonToCSharpConverter.cs 延后任务: ⏸️ 差异对比功能:需要 Monaco Diff Editor 深度配置 ⏸️ 前端编译:Vite + Monaco 配置复杂,需专项处理 Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
217 lines
5.8 KiB
C#
217 lines
5.8 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace CodePlay.Core.Services;
|
|
|
|
/// <summary>
|
|
/// 代码格式化服务
|
|
/// 支持 C#/Java/C++ 代码格式化
|
|
/// </summary>
|
|
public interface ICodeFormatter
|
|
{
|
|
Task<string> FormatAsync(string code, string language, CancellationToken cancellationToken = default);
|
|
bool IsFormatterAvailable(string language);
|
|
}
|
|
|
|
public class CodeFormatter : ICodeFormatter
|
|
{
|
|
public async Task<string> FormatAsync(string code, string language, CancellationToken cancellationToken = default)
|
|
{
|
|
return language.ToLower() switch
|
|
{
|
|
"csharp" => await FormatCSharp(code, cancellationToken),
|
|
"java" => await FormatJava(code, cancellationToken),
|
|
"cpp" or "c++" => await FormatCpp(code, cancellationToken),
|
|
_ => code
|
|
};
|
|
}
|
|
|
|
public bool IsFormatterAvailable(string language)
|
|
{
|
|
return language.ToLower() switch
|
|
{
|
|
"csharp" => IsDotNetFormatAvailable(),
|
|
"java" => IsJavaFormatAvailable(),
|
|
"cpp" or "c++" => IsClangFormatAvailable(),
|
|
_ => false
|
|
};
|
|
}
|
|
|
|
private async Task<string> FormatCSharp(string code, CancellationToken ct)
|
|
{
|
|
if (!IsDotNetFormatAvailable())
|
|
return code;
|
|
|
|
try
|
|
{
|
|
var tempFile = Path.GetTempFileName() + ".cs";
|
|
await File.WriteAllTextAsync(tempFile, code, ct);
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "dotnet",
|
|
Arguments = $"format \"{tempFile}\" --no-restore",
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(startInfo);
|
|
if (process == null) return code;
|
|
|
|
await process.WaitForExitAsync(ct);
|
|
|
|
if (process.ExitCode == 0)
|
|
return await File.ReadAllTextAsync(tempFile, ct);
|
|
|
|
return code;
|
|
}
|
|
catch
|
|
{
|
|
return code;
|
|
}
|
|
}
|
|
|
|
private async Task<string> FormatJava(string code, CancellationToken ct)
|
|
{
|
|
if (!IsJavaFormatAvailable())
|
|
return code;
|
|
|
|
try
|
|
{
|
|
var tempFile = Path.GetTempFileName() + ".java";
|
|
await File.WriteAllTextAsync(tempFile, code, ct);
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "google-java-format",
|
|
Arguments = $"--replace \"{tempFile}\"",
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(startInfo);
|
|
if (process == null) return code;
|
|
|
|
await process.WaitForExitAsync(ct);
|
|
|
|
if (process.ExitCode == 0)
|
|
return await File.ReadAllTextAsync(tempFile, ct);
|
|
|
|
return code;
|
|
}
|
|
catch
|
|
{
|
|
return code;
|
|
}
|
|
}
|
|
|
|
private async Task<string> FormatCpp(string code, CancellationToken ct)
|
|
{
|
|
if (!IsClangFormatAvailable())
|
|
return code;
|
|
|
|
try
|
|
{
|
|
var tempFile = Path.GetTempFileName() + ".cpp";
|
|
await File.WriteAllTextAsync(tempFile, code, ct);
|
|
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "clang-format",
|
|
Arguments = $"-i -style=file \"{tempFile}\"",
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(startInfo);
|
|
if (process == null) return code;
|
|
|
|
await process.WaitForExitAsync(ct);
|
|
|
|
if (process.ExitCode == 0)
|
|
return await File.ReadAllTextAsync(tempFile, ct);
|
|
|
|
return code;
|
|
}
|
|
catch
|
|
{
|
|
return code;
|
|
}
|
|
}
|
|
|
|
private bool IsDotNetFormatAvailable()
|
|
{
|
|
try
|
|
{
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "dotnet",
|
|
Arguments = "tool list -g",
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(startInfo);
|
|
process?.WaitForExit(5000);
|
|
var output = process?.StandardOutput.ReadToEnd();
|
|
return output?.Contains("dotnet-format") == true;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private bool IsJavaFormatAvailable()
|
|
{
|
|
try
|
|
{
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "google-java-format",
|
|
Arguments = "--version",
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(startInfo);
|
|
process?.WaitForExit(5000);
|
|
return process?.ExitCode == 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private bool IsClangFormatAvailable()
|
|
{
|
|
try
|
|
{
|
|
var startInfo = new ProcessStartInfo
|
|
{
|
|
FileName = "clang-format",
|
|
Arguments = "--version",
|
|
RedirectStandardOutput = true,
|
|
UseShellExecute = false,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using var process = Process.Start(startInfo);
|
|
process?.WaitForExit(5000);
|
|
return process?.ExitCode == 0;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|