Skip to content

Commit 56da993

Browse files
konardclaude
andcommitted
Fix template class/struct line formatting
Place class/struct definition on the next line after template declaration for better readability. This change affects both the C# and Python implementations. Changes: - Modified C# regex pattern for class/struct template declarations to add newline before class/struct - Updated Python regex patterns consistently - Added comprehensive test cases covering both struct and interface template transformations Before: template <typename TValue> struct ISetter<TValue> { After: template <typename TValue> struct ISetter<TValue> { 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0c8a433 commit 56da993

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

csharp/Platform.RegularExpressions.Transformer.CSharpToCpp.Tests/CSharpToCppTransformerTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,50 @@ public static void Main(string[] args)
3535
var actualResult = transformer.Transform(helloWorldCode);
3636
Assert.Equal(expectedResult, actualResult);
3737
}
38+
39+
[Fact]
40+
public void TemplateLineBreakTest()
41+
{
42+
const string inputCode = @"
43+
struct ISetter<TValue>
44+
{
45+
virtual void Set(TValue value) = 0;
46+
47+
virtual ~ISetter<TValue>() = default;
48+
};";
49+
const string expectedResult = @"
50+
template <typename ...> struct ISetter;
51+
template <typename TValue>
52+
struct ISetter<TValue>
53+
{
54+
virtual void Set(TValue value) = 0;
55+
56+
virtual ~ISetter<TValue>() = default;
57+
};";
58+
var transformer = new CSharpToCppTransformer();
59+
var actualResult = transformer.Transform(inputCode);
60+
Assert.Equal(expectedResult, actualResult);
61+
}
62+
63+
[Fact]
64+
public void InterfaceTemplateLineBreakTest()
65+
{
66+
const string inputCode = @"
67+
interface IFactory<TProduct>
68+
{
69+
TProduct Create();
70+
};";
71+
const string expectedResult = @"
72+
template <typename ...> class IFactory;
73+
template <typename TProduct>
74+
class IFactory<TProduct>
75+
{
76+
public:
77+
TProduct Create();
78+
};";
79+
var transformer = new CSharpToCppTransformer();
80+
var actualResult = transformer.Transform(inputCode);
81+
Assert.Equal(expectedResult, actualResult);
82+
}
3883
}
3984
}

csharp/Platform.RegularExpressions.Transformer.CSharpToCpp/CSharpToCppTransformer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ public class CSharpToCppTransformer : TextTransformer
8787
(new Regex(@"((public|protected|private|internal|abstract|static) )*(?<category>interface|class|struct)"), "${category}", 0),
8888
// class GenericCollectionMethodsBase<TElement> {
8989
// template <typename TElement> class GenericCollectionMethodsBase {
90-
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)(?<type>class|struct) (?<typeName>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> ${type} ${typeName};" + Environment.NewLine + "${indent}template <typename ${typeParameters}> ${type} ${typeName}<${typeParameters}>${typeDefinitionEnding}{", 0),
90+
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)(?<type>class|struct) (?<typeName>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> ${type} ${typeName};" + Environment.NewLine + "${indent}template <typename ${typeParameters}>" + Environment.NewLine + "${indent}${type} ${typeName}<${typeParameters}>${typeDefinitionEnding}{", 0),
9191
// static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
9292
// template<typename T> static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
9393
(new Regex(@"static ([a-zA-Z0-9]+) ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>\(([^\)\r\n]+)\)"), "template <typename $3> static $1 $2($4)", 0),
9494
// interface IFactory<out TProduct> {
9595
// template <typename...> class IFactory;\ntemplate <typename TProduct> class IFactory<TProduct>
96-
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)interface (?<interface>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> class ${interface};" + Environment.NewLine + "${indent}template <typename ${typeParameters}> class ${interface}<${typeParameters}>${typeDefinitionEnding}{" + Environment.NewLine + " public:", 0),
96+
(new Regex(@"(?<before>\r?\n)(?<indent>[ \t]*)interface (?<interface>[a-zA-Z0-9]+)<(?<typeParameters>[a-zA-Z0-9 ,]+)>(?<typeDefinitionEnding>[^{]+){"), "${before}${indent}template <typename ...> class ${interface};" + Environment.NewLine + "${indent}template <typename ${typeParameters}>" + Environment.NewLine + "${indent}class ${interface}<${typeParameters}>${typeDefinitionEnding}{" + Environment.NewLine + " public:", 0),
9797
// template <typename TObject, TProperty, TValue>
9898
// template <typename TObject, typename TProperty, typename TValue>
9999
(new Regex(@"(?<before>template <((, )?typename [a-zA-Z0-9]+)+, )(?<typeParameter>[a-zA-Z0-9]+)(?<after>(,|>))"), "${before}typename ${typeParameter}${after}", 10),

python/cs2cpp/cs2cpp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ def __init__(
8686
SubRule(r"((public|protected|private|internal|abstract|static) )*(?P<category>interface|class|struct)", r"\g<category>", max_repeat=0),
8787
# class GenericCollectionMethodsBase<TElement> {
8888
# template <typename TElement> class GenericCollectionMethodsBase {
89-
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)(?P<type>class|struct) (?P<typeName>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> \g<type> \g<typeName>;\n" + "\g<indent>template <typename \g<typeParameters>> \g<type> \g<typeName><\g<typeParameters>>\g<typeDefinitionEnding>{", max_repeat=0),
89+
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)(?P<type>class|struct) (?P<typeName>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> \g<type> \g<typeName>;\n" + "\g<indent>template <typename \g<typeParameters>>\n" + "\g<indent>\g<type> \g<typeName><\g<typeParameters>>\g<typeDefinitionEnding>{", max_repeat=0),
9090
# static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
9191
# template<typename T> static void TestMultipleCreationsAndDeletions<TElement>(SizedBinaryTreeMethodsBase<TElement> tree, TElement* root)
9292
SubRule(r"static ([a-zA-Z0-9]+) ([a-zA-Z0-9]+)<([a-zA-Z0-9]+)>\(([^\)\r\n]+)\)", r"template <typename \3> static \1 \2(\4)", max_repeat=0),
9393
# interface IFactory<out TProduct> {
9494
# template <typename...> class IFactory;\ntemplate <typename TProduct> class IFactory<TProduct>
95-
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)interface (?P<interface>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> class \g<interface>;\n" + "\g<indent>template <typename \g<typeParameters>> class \g<interface><\g<typeParameters>>\g<typeDefinitionEnding>{\n" + " public:", max_repeat=0),
95+
SubRule(r"(?P<before>\r?\n)(?P<indent>[ \t]*)interface (?P<interface>[a-zA-Z0-9]+)<(?P<typeParameters>[a-zA-Z0-9 ,]+)>(?P<typeDefinitionEnding>[^{]+){", r"\g<before>\g<indent>template <typename ...> class \g<interface>;\n" + "\g<indent>template <typename \g<typeParameters>>\n" + "\g<indent>class \g<interface><\g<typeParameters>>\g<typeDefinitionEnding>{\n" + " public:", max_repeat=0),
9696
# template <typename TObject, TProperty, TValue>
9797
# template <typename TObject, typename TProperty, typename TValue>
9898
SubRule(r"(?P<before>template <((, )?typename [a-zA-Z0-9]+)+, )(?P<typeParameter>[a-zA-Z0-9]+)(?P<after>(,|>))", r"\g<before>typename \g<typeParameter>\g<after>", max_repeat=10),

0 commit comments

Comments
 (0)