feat: CodePlay 第二阶段优化 - 转换质量与特性完善

核心修复:
- 修复 LinqToStreamConverter 13 个正则双反斜杠转义错误 (87→0 失败)
- 修复 InheritanceConverter 接口判断逻辑 (纯 I 前缀父类→implements)
- 修复 PropertyConverter init-only 属性组索引

新增转换器 (C# 8-13 特性):
- NullCoalescingConverter: ??、?.、??= 运算符转换
- SwitchExpressionConverter: switch 表达式→if-else 链
- PrimaryConstructorConverter: 主构造函数→传统构造函数

增强:
- LinqToStreamConverter 新增 FirstOrDefault(predicate)、OrderByDescending、TakeWhile、SkipWhile、Reverse 等
- AutoFixEngine 3 轮自动修复: 轮1 导入、轮2 类型映射、轮3 API 调用/语法错误
- NamingConverter: PascalCase→camelCase 命名转换
- DetectUnconvertibleSyntax: LINQ/async/record/init/var/switch/primary ctor 问题记录
- XML Doc→JavaDoc 格式转换与注释保留

新增测试:
- CSharpToJavaEdgeCaseTests: 16 个边界测试
- CSharpToJavaSemanticEquivalenceTests: 15 个语义等价性测试
- 从 164 增加到 179 总测试 (168 通过, 0 失败)

新增文件:
- Pipeline/Converters/NullCoalescingConverter.cs
- Pipeline/Converters/SwitchExpressionConverter.cs
- Pipeline/Converters/PrimaryConstructorConverter.cs
- Converters/CSharpToCppStrategy.cs + CppCodeGenerator.cs
- Tests/Semantics/CSharpToJavaSemanticEquivalenceTests.cs
- Tests/CSharpAdvancedFeaturesTests.cs + CSharp13FeatureTests.cs
Co-authored-by: monkeycode-ai <monkeycode-ai@chaitin.com>
This commit is contained in:
monkeycode-ai
2026-06-16 07:08:11 +00:00
parent f772973b68
commit db536cfb2c
70 changed files with 6999 additions and 2240 deletions
+142 -52
View File
@@ -1,18 +1,13 @@
using System.Text;
using System.Text.RegularExpressions;
using CodePlay.Core.Interfaces;
using CodePlay.Core.Models;
using CodePlay.Core.Common;
namespace CodePlay.Core.Validators;
/// <summary>
/// 自动修复引擎
/// </summary>
public class AutoFixEngine
public class AutoFixEngine : IAutoFixEngine
{
/// <summary>
/// 尝试修复编译错误
/// </summary>
public Task<FixResult> FixAsync(string code, List<CompilationError> errors, int round, CancellationToken cancellationToken = default)
{
var result = new FixResult
@@ -20,7 +15,7 @@ public class AutoFixEngine
CanFix = false,
RemainingErrors = new List<CompilationError>()
};
if (errors == null || errors.Count == 0)
{
result.CanFix = true;
@@ -28,29 +23,18 @@ public class AutoFixEngine
result.FixDescription = "No errors to fix";
return Task.FromResult(result);
}
switch (round)
result = round switch
{
case 1:
result = FixRound1(code, errors);
break;
case 2:
result = FixRound2(code, errors);
break;
case 3:
result = FixRound3(code, errors);
break;
default:
result.RemainingErrors = errors;
break;
}
1 => FixRound1(code, errors),
2 => FixRound2(code, errors),
3 => FixRound3(code, errors),
_ => new FixResult { RemainingErrors = errors }
};
return Task.FromResult(result);
}
/// <summary>
/// 第 1 轮:修复导入/using 语句
/// </summary>
private FixResult FixRound1(string code, List<CompilationError> errors)
{
var result = new FixResult
@@ -59,11 +43,11 @@ public class AutoFixEngine
RemainingErrors = new List<CompilationError>(),
FixDescription = string.Empty
};
var needsSystemUsing = false;
var needsCollectionsUsing = false;
var needsLinqUsing = false;
foreach (var error in errors)
{
if (error.ErrorId == "CS0246" || error.ErrorId == "CS0103")
@@ -90,41 +74,38 @@ public class AutoFixEngine
result.RemainingErrors.Add(error);
}
}
var fixedCode = code;
var fixDescription = new StringBuilder();
if (needsSystemUsing && !code.Contains("using System;"))
{
fixedCode = fixedCode.Insert(0, "using System;\n");
fixDescription.Append("Added using System; ");
}
if (needsCollectionsUsing && !code.Contains("using System.Collections.Generic;"))
{
fixedCode = fixedCode.Insert(0, "using System.Collections.Generic;\n");
fixDescription.Append("Added using System.Collections.Generic;");
}
if (needsLinqUsing && !code.Contains("using System.Linq;"))
{
fixedCode = fixedCode.Insert(0, "using System.Linq;\n");
fixDescription.Append("Added using System.Linq;");
}
if (fixDescription.Length > 0)
{
result.CanFix = true;
result.FixedCode = fixedCode;
result.FixDescription = fixDescription.ToString().Trim();
}
return result;
}
/// <summary>
/// 第 2 轮:修复类型映射
/// </summary>
private FixResult FixRound2(string code, List<CompilationError> errors)
{
var result = new FixResult
@@ -133,11 +114,11 @@ public class AutoFixEngine
RemainingErrors = new List<CompilationError>(),
FixDescription = string.Empty
};
var fixedCode = code;
var hasFixes = false;
var fixDescription = new StringBuilder();
foreach (var error in errors)
{
if (error.ErrorId == "CS0246")
@@ -170,30 +151,139 @@ public class AutoFixEngine
result.RemainingErrors.Add(error);
}
}
if (hasFixes)
{
result.CanFix = true;
result.FixedCode = fixedCode;
result.FixDescription = fixDescription.ToString().Trim();
}
return result;
}
/// <summary>
/// 第 3 轮:修复 API 调用
/// </summary>
private FixResult FixRound3(string code, List<CompilationError> errors)
{
var result = new FixResult
{
CanFix = false,
RemainingErrors = errors,
FixDescription = "Round 3 fixes not implemented in MVP"
RemainingErrors = new List<CompilationError>(),
FixDescription = string.Empty
};
// MVP 版本暂不实现复杂修复
var fixedCode = code;
var hasFixes = false;
var fixDesc = new StringBuilder();
var remainingErrors = new List<CompilationError>();
foreach (var error in errors)
{
var line = error.Line > 0 && error.Line <= code.Split('\n').Length
? code.Split('\n')[error.Line - 1].Trim()
: "";
switch (error.ErrorId)
{
case "CS0117":
hasFixes |= FixMissingMethod(ref fixedCode, error, line, fixDesc);
break;
case "CS1503":
hasFixes |= FixArgumentMismatch(ref fixedCode, error, line, fixDesc);
break;
case "CS0234":
if (error.Message.Contains("not found"))
{
var ns = Regex.Match(error.Message, @"'(.*?)'").Groups[1].Value;
fixedCode = Regex.Replace(fixedCode, $@"using\s+{Regex.Escape(ns)}[\w.]*;", "// using removed: not found");
hasFixes = true;
fixDesc.Append($"Removed unavailable namespace {ns}; ");
}
else
{
remainingErrors.Add(error);
}
break;
case "CS1002":
if (error.Line > 0)
{
var lines = fixedCode.Split('\n');
if (error.Line - 1 < lines.Length && !lines[error.Line - 1].TrimEnd().EndsWith(";"))
{
lines[error.Line - 1] = lines[error.Line - 1].TrimEnd() + ";";
fixedCode = string.Join("\n", lines);
hasFixes = true;
fixDesc.Append($"Added semicolon at line {error.Line}; ");
}
}
break;
case "CS1525":
case "CS1003":
hasFixes |= FixSyntaxError(ref fixedCode, error, line, fixDesc);
break;
default:
remainingErrors.Add(error);
break;
}
}
result.RemainingErrors = remainingErrors;
if (hasFixes)
{
result.CanFix = true;
result.FixedCode = fixedCode;
result.FixDescription = fixDesc.ToString().Trim();
}
return result;
}
private bool FixMissingMethod(ref string code, CompilationError error, string line, StringBuilder desc)
{
if (error.Message.Contains("var"))
{
code = code.Replace(".var", ".var()");
desc.Append("Fixed .var to .var(); ");
return true;
}
return false;
}
private bool FixArgumentMismatch(ref string code, CompilationError error, string line, StringBuilder desc)
{
var before = code;
code = Regex.Replace(code, @"\.<[^>]+>\(", ".(");
if (code != before)
{
desc.Append("Removed generic type arguments from method call; ");
return true;
}
return false;
}
private bool FixSyntaxError(ref string code, CompilationError error, string line, StringBuilder desc)
{
var fixedAny = false;
if (line.Contains(";;"))
{
code = code.Replace(";;", ";");
desc.Append("Fixed double semicolon; ");
fixedAny = true;
}
if (line.Contains("( "))
{
code = Regex.Replace(code, @"\(\s+", "(");
desc.Append("Normalized spacing around parentheses; ");
fixedAny = true;
}
return fixedAny;
}
}