Skip to content

Commit bcf30d7

Browse files
committed
Integrated document converter tool
1 parent 486e4f5 commit bcf30d7

File tree

3 files changed

+133
-108
lines changed

3 files changed

+133
-108
lines changed

Box2D.NET.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Box2D.NET.Benchmark", "tool
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Box2D.NET.Copyright", "tools\Box2D.NET.Copyright\Box2D.NET.Copyright.csproj", "{84571CD4-3824-4D9C-98C7-19CEB8B8A835}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Box2D.NET.CommentConverter", "tools\Box2D.NET.CommentConverter\Box2D.NET.CommentConverter.csproj", "{46AE2C18-D4A3-4EC8-9A9E-4F6FA3E86EFF}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -54,6 +56,10 @@ Global
5456
{84571CD4-3824-4D9C-98C7-19CEB8B8A835}.Debug|Any CPU.Build.0 = Debug|Any CPU
5557
{84571CD4-3824-4D9C-98C7-19CEB8B8A835}.Release|Any CPU.ActiveCfg = Release|Any CPU
5658
{84571CD4-3824-4D9C-98C7-19CEB8B8A835}.Release|Any CPU.Build.0 = Release|Any CPU
59+
{46AE2C18-D4A3-4EC8-9A9E-4F6FA3E86EFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
60+
{46AE2C18-D4A3-4EC8-9A9E-4F6FA3E86EFF}.Debug|Any CPU.Build.0 = Debug|Any CPU
61+
{46AE2C18-D4A3-4EC8-9A9E-4F6FA3E86EFF}.Release|Any CPU.ActiveCfg = Release|Any CPU
62+
{46AE2C18-D4A3-4EC8-9A9E-4F6FA3E86EFF}.Release|Any CPU.Build.0 = Release|Any CPU
5763
EndGlobalSection
5864
GlobalSection(NestedProjects) = preSolution
5965
{11849830-82C5-4BC0-BE87-16D90243FD78} = {1858BB70-0C9E-47DD-B372-BCA0DAA985E6}
@@ -62,5 +68,6 @@ Global
6268
{770247E4-3E73-4C78-B052-9FE2C41995BB} = {CB12270F-4747-4936-B26D-2581A8F7D0E1}
6369
{ADD9F942-4272-4034-BA77-03098273400D} = {1858BB70-0C9E-47DD-B372-BCA0DAA985E6}
6470
{84571CD4-3824-4D9C-98C7-19CEB8B8A835} = {CB12270F-4747-4936-B26D-2581A8F7D0E1}
71+
{46AE2C18-D4A3-4EC8-9A9E-4F6FA3E86EFF} = {CB12270F-4747-4936-B26D-2581A8F7D0E1}
6572
EndGlobalSection
6673
EndGlobal

tools/Box2D.NET.CommentConverter/Box2D.NET.CommentConverter.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
6-
<ImplicitUsings>enable</ImplicitUsings>
7-
<Nullable>enable</Nullable>
5+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
6+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
7+
<IsPackable>false</IsPackable>
88
</PropertyGroup>
99

1010
</Project>
Lines changed: 123 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,177 @@
1-
const string DoubleSlashCommentPrefix = "// ";
2-
const string TripleSlashCommentPrefix = "///";
3-
const string SummaryStart = "/// <summary>";
4-
const string SummaryEnd = "/// </summary>";
5-
const string FileFilter = "*.cs";
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
66

7-
var commentStarts = new HashSet<string> { DoubleSlashCommentPrefix, TripleSlashCommentPrefix };
8-
var repoRoot = Directory.GetCurrentDirectory();
7+
namespace Box2D.NET.CommentConverter;
8+
9+
public static class Program
10+
{
11+
public const string DoubleSlashCommentPrefix = "// ";
12+
public const string TripleSlashCommentPrefix = "///";
13+
public const string SummaryStart = "/// <summary>";
14+
public const string SummaryEnd = "/// </summary>";
15+
public const string FileFilter = "*.cs";
16+
public static readonly HashSet<string> commentStarts = new HashSet<string> { DoubleSlashCommentPrefix, TripleSlashCommentPrefix };
17+
18+
public static async Task<int> Main(string[] args)
19+
{
20+
var repoRoot = Directory.GetCurrentDirectory();
921

1022
#if DEBUG
11-
repoRoot = Path.GetFullPath(Path.Combine(repoRoot, "..", "..", "..", "..", ".."));
23+
repoRoot = Path.GetFullPath(Path.Combine(repoRoot, "..", "..", "..", "..", ".."));
1224
#endif
1325

14-
var folderPath = Path.Combine(repoRoot, "src", "Box2D.NET");
26+
var folderPath = Path.Combine(repoRoot, "src", "Box2D.NET");
1527

16-
// Tests: B2BodySim, B2World, B2WorldId
17-
var files = Directory.GetFiles(folderPath, FileFilter, SearchOption.AllDirectories)
18-
//.Where(w => w.Contains("B2PrismaticJointDef"))
19-
//.Take(50)
20-
.ToList();
21-
22-
Console.WriteLine($"Found {files.Count} C# files in the folder: {folderPath}");
23-
24-
foreach (var filePath in files)
25-
{
26-
await ProcessFileAsync(filePath);
27-
}
28+
// Tests: B2BodySim, B2World, B2WorldId
29+
var files = Directory.GetFiles(folderPath, FileFilter, SearchOption.AllDirectories)
30+
//.Where(w => w.Contains("B2PrismaticJointDef"))
31+
//.Take(50)
32+
.ToList();
2833

29-
Console.WriteLine("*** Processing completed ***");
34+
Console.WriteLine($"Found {files.Count} C# files in the folder: {folderPath}");
3035

31-
async Task ProcessFileAsync(string filePath)
32-
{
33-
Console.WriteLine($"\nProcessing file: {filePath}");
36+
foreach (var filePath in files)
37+
{
38+
await ProcessFileAsync(filePath);
39+
}
3440

35-
var content = await File.ReadAllTextAsync(filePath);
36-
var lines = content.Split(["\r\n", "\n"], StringSplitOptions.None).ToList();
37-
var commentLineIndexes = ExtractCommentLineIndexes(lines);
41+
Console.WriteLine("*** Processing completed ***");
3842

39-
RemovePreNamespaceComments(lines, commentLineIndexes);
43+
return 0;
44+
}
4045

41-
if (commentLineIndexes.Count == 0)
46+
public static async Task ProcessFileAsync(string filePath)
4247
{
43-
Console.WriteLine("No comment lines found in the file.");
44-
return;
45-
}
48+
Console.WriteLine($"\nProcessing file: {filePath}");
4649

47-
var commentBlocks = ExtractCommentBlocks(lines, commentLineIndexes);
50+
var content = await File.ReadAllTextAsync(filePath);
51+
var lines = content.Split(["\r\n", "\n"], StringSplitOptions.None).ToList();
52+
var commentLineIndexes = ExtractCommentLineIndexes(lines);
4853

49-
if (commentBlocks.Count == 0)
50-
{
51-
Console.WriteLine("No comment blocks found.");
52-
return;
53-
}
54+
RemovePreNamespaceComments(lines, commentLineIndexes);
5455

55-
ConvertCommentsToTripleSlash(lines, commentBlocks);
56+
if (commentLineIndexes.Count == 0)
57+
{
58+
Console.WriteLine("No comment lines found in the file.");
59+
return;
60+
}
5661

57-
WrapCommentsWithSummaryTags(lines, commentBlocks);
62+
var commentBlocks = ExtractCommentBlocks(lines, commentLineIndexes);
5863

59-
//File.WriteAllText(filePath.Replace(".cs", ".xmlcomments.cs"), string.Join(Environment.NewLine, lines));
60-
File.WriteAllText(filePath, string.Join(Environment.NewLine, lines));
64+
if (commentBlocks.Count == 0)
65+
{
66+
Console.WriteLine("No comment blocks found.");
67+
return;
68+
}
6169

62-
Console.WriteLine($"Output written to: {filePath}");
63-
}
70+
ConvertCommentsToTripleSlash(lines, commentBlocks);
6471

65-
static void RemovePreNamespaceComments(List<string> lines, List<int> commentLineIndexes)
66-
{
67-
var namespaceLineIndex = lines.FindIndex(line => line.TrimStart().StartsWith("namespace "));
72+
WrapCommentsWithSummaryTags(lines, commentBlocks);
6873

69-
commentLineIndexes.RemoveAll(index => index < namespaceLineIndex);
70-
}
74+
//File.WriteAllText(filePath.Replace(".cs", ".xmlcomments.cs"), string.Join(Environment.NewLine, lines));
75+
File.WriteAllText(filePath, string.Join(Environment.NewLine, lines));
7176

72-
static List<CommentBlock> ExtractCommentBlocks(List<string> lines, List<int> commentLineIndexes)
73-
{
74-
var commentBlocks = new List<CommentBlock>();
75-
var startIndex = commentLineIndexes[0];
76-
var endIndex = commentLineIndexes[0];
77+
Console.WriteLine($"Output written to: {filePath}");
78+
}
7779

78-
if (commentLineIndexes.Count == 1)
80+
public static void RemovePreNamespaceComments(List<string> lines, List<int> commentLineIndexes)
7981
{
80-
AddBlockIfFollowedByPublic(startIndex, endIndex);
82+
var namespaceLineIndex = lines.FindIndex(line => line.TrimStart().StartsWith("namespace "));
8183

82-
return commentBlocks;
84+
commentLineIndexes.RemoveAll(index => index < namespaceLineIndex);
8385
}
8486

85-
for (int i = 1; i < commentLineIndexes.Count; i++)
87+
public static List<CommentBlock> ExtractCommentBlocks(List<string> lines, List<int> commentLineIndexes)
8688
{
87-
var nextIndex = commentLineIndexes[i];
89+
var commentBlocks = new List<CommentBlock>();
90+
var startIndex = commentLineIndexes[0];
91+
var endIndex = commentLineIndexes[0];
8892

89-
if (nextIndex - endIndex != 1)
93+
if (commentLineIndexes.Count == 1)
9094
{
9195
AddBlockIfFollowedByPublic(startIndex, endIndex);
92-
startIndex = nextIndex;
93-
}
9496

95-
endIndex = nextIndex;
97+
return commentBlocks;
98+
}
9699

97-
if (i == commentLineIndexes.Count - 1)
100+
for (int i = 1; i < commentLineIndexes.Count; i++)
98101
{
99-
AddBlockIfFollowedByPublic(startIndex, endIndex);
102+
var nextIndex = commentLineIndexes[i];
103+
104+
if (nextIndex - endIndex != 1)
105+
{
106+
AddBlockIfFollowedByPublic(startIndex, endIndex);
107+
startIndex = nextIndex;
108+
}
109+
110+
endIndex = nextIndex;
111+
112+
if (i == commentLineIndexes.Count - 1)
113+
{
114+
AddBlockIfFollowedByPublic(startIndex, endIndex);
115+
}
100116
}
101-
}
102117

103-
return commentBlocks;
118+
return commentBlocks;
104119

105-
void AddBlockIfFollowedByPublic(int startIndex, int endIndex)
106-
{
107-
if (endIndex + 1 < lines.Count && lines[endIndex + 1].Contains("public"))
120+
void AddBlockIfFollowedByPublic(int startIndex, int endIndex)
108121
{
109-
commentBlocks.Add(new CommentBlock(startIndex, endIndex));
122+
if (endIndex + 1 < lines.Count && lines[endIndex + 1].Contains("public"))
123+
{
124+
commentBlocks.Add(new CommentBlock(startIndex, endIndex));
125+
}
110126
}
111127
}
112-
}
113128

114-
List<int> ExtractCommentLineIndexes(List<string> lines)
115-
{
116-
return lines
117-
.Select((line, index) => (line, index))
118-
.Where(item => commentStarts.Any(commentStart =>
119-
item.line.TrimStart().StartsWith(commentStart)))
120-
.Select(item => item.index)
121-
.ToList();
122-
}
123-
124-
static void ConvertCommentsToTripleSlash(List<string> lines, List<CommentBlock> commentBlocks)
125-
{
126-
foreach (var block in commentBlocks)
129+
public static List<int> ExtractCommentLineIndexes(List<string> lines)
127130
{
128-
Console.WriteLine($"Comment block from {block.StartIndex + 1} to {block.EndIndex + 1} (length: {block.Length})");
131+
132+
return lines
133+
.Select((line, index) => (line, index))
134+
.Where(item => commentStarts.Any(commentStart =>
135+
item.line.TrimStart().StartsWith(commentStart)))
136+
.Select(item => item.index)
137+
.ToList();
138+
}
129139

130-
for (int i = 0; i < block.Length; i++)
140+
public static void ConvertCommentsToTripleSlash(List<string> lines, List<CommentBlock> commentBlocks)
141+
{
142+
foreach (var block in commentBlocks)
131143
{
132-
int lineIndex = block.StartIndex + i;
144+
Console.WriteLine($"Comment block from {block.StartIndex + 1} to {block.EndIndex + 1} (length: {block.Length})");
133145

134-
if (!lines[lineIndex].TrimStart().StartsWith(TripleSlashCommentPrefix))
146+
for (int i = 0; i < block.Length; i++)
135147
{
136-
lines[lineIndex] = lines[lineIndex].Replace(DoubleSlashCommentPrefix, "/// ");
148+
int lineIndex = block.StartIndex + i;
149+
150+
if (!lines[lineIndex].TrimStart().StartsWith(TripleSlashCommentPrefix))
151+
{
152+
lines[lineIndex] = lines[lineIndex].Replace(DoubleSlashCommentPrefix, "/// ");
153+
}
137154
}
138155
}
139156
}
140-
}
141157

142-
void WrapCommentsWithSummaryTags(List<string> lines, List<CommentBlock> commentBlocks)
143-
{
144-
for (int i = commentBlocks.Count - 1; i >= 0; i--)
158+
public static void WrapCommentsWithSummaryTags(List<string> lines, List<CommentBlock> commentBlocks)
145159
{
146-
var block = commentBlocks[i];
147-
var indentation = GetIndentation(lines[block.StartIndex]);
160+
for (int i = commentBlocks.Count - 1; i >= 0; i--)
161+
{
162+
var block = commentBlocks[i];
163+
var indentation = GetIndentation(lines[block.StartIndex]);
164+
165+
lines.Insert(block.EndIndex + 1, $"{indentation}{SummaryEnd}");
166+
lines.Insert(block.StartIndex, $"{indentation}{SummaryStart}");
167+
}
148168

149-
lines.Insert(block.EndIndex + 1, $"{indentation}{SummaryEnd}");
150-
lines.Insert(block.StartIndex, $"{indentation}{SummaryStart}");
169+
string GetIndentation(string line) => new(' ', line.Length - line.TrimStart().Length);
151170
}
152171

153-
string GetIndentation(string line) => new(' ', line.Length - line.TrimStart().Length);
154-
}
172+
public record class CommentBlock(int StartIndex, int EndIndex)
173+
{
174+
public int Length => EndIndex - StartIndex + 1;
175+
}
155176

156-
record class CommentBlock(int StartIndex, int EndIndex)
157-
{
158-
public int Length => EndIndex - StartIndex + 1;
159-
}
177+
}

0 commit comments

Comments
 (0)