Skip to content

Commit c6ffa9d

Browse files
committed
Fix the regressed indentation of printed comments
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 96d231e commit c6ffa9d

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

src/Generator.Tests/AST/TestAST.cs

+17-9
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,18 @@ public void TestFunctionInstantiatedFrom()
383383
public void TestComments()
384384
{
385385
var @class = AstContext.FindCompleteClass("TestComments");
386-
var commentClass = @class.Comment.FullComment.CommentToString(CommentKind.BCPLSlash);
386+
var textGenerator = new TextGenerator();
387+
textGenerator.Print(@class.Comment.FullComment, CommentKind.BCPLSlash);
387388
Assert.AreEqual(@"/// <summary>
388389
/// <para>Hash set/map base class.</para>
389390
/// <para>Note that to prevent extra memory use due to vtable pointer, %HashBase intentionally does not declare a virtual destructor</para>
390391
/// <para>and therefore %HashBase pointers should never be used.</para>
391-
/// </summary>".Replace("\r", string.Empty), commentClass.Replace("\r", string.Empty));
392+
/// </summary>
393+
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
392394

393395
var method = @class.Methods.First(m => m.Name == "GetIOHandlerControlSequence");
394-
var commentMethod = method.Comment.FullComment.CommentToString(CommentKind.BCPL);
396+
textGenerator.StringBuilder.Clear();
397+
textGenerator.Print(method.Comment.FullComment, CommentKind.BCPL);
395398
Assert.AreEqual(@"// <summary>
396399
// <para>Get the string that needs to be written to the debugger stdin file</para>
397400
// <para>handle when a control character is typed.</para>
@@ -407,10 +410,12 @@ public void TestComments()
407410
// <para>to have them do what normally would happen when using a real</para>
408411
// <para>terminal, so this function allows GUI programs to emulate this</para>
409412
// <para>functionality.</para>
410-
// </remarks>".Replace("\r", string.Empty), commentMethod.Replace("\r", string.Empty));
413+
// </remarks>
414+
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
411415

412416
var methodTestDoxygen = @class.Methods.First(m => m.Name == "SBAttachInfo");
413-
var commentMethodDoxygen = methodTestDoxygen.Comment.FullComment.CommentToString(CommentKind.BCPLSlash);
417+
textGenerator.StringBuilder.Clear();
418+
textGenerator.Print(methodTestDoxygen.Comment.FullComment, CommentKind.BCPLSlash);
414419
Assert.AreEqual(@"/// <summary>Attach to a process by name.</summary>
415420
/// <param name=""path"">A full or partial name for the process to attach to.</param>
416421
/// <param name=""wait_for"">
@@ -420,11 +425,13 @@ public void TestComments()
420425
/// <remarks>
421426
/// <para>This function implies that a future call to SBTarget::Attach(...)</para>
422427
/// <para>will be synchronous.</para>
423-
/// </remarks>".Replace("\r", string.Empty), commentMethodDoxygen.Replace("\r", string.Empty));
428+
/// </remarks>
429+
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
424430

425431
var methodDoxygenCustomTags = @class.Methods.First(m => m.Name == "glfwDestroyWindow");
426-
new CleanCommentsPass { }.VisitFull(methodDoxygenCustomTags.Comment.FullComment);
427-
var commentMethodDoxygenCustomTag = methodDoxygenCustomTags.Comment.FullComment.CommentToString(CommentKind.BCPLSlash);
432+
new CleanCommentsPass().VisitFull(methodDoxygenCustomTags.Comment.FullComment);
433+
textGenerator.StringBuilder.Clear();
434+
textGenerator.Print(methodDoxygenCustomTags.Comment.FullComment, CommentKind.BCPLSlash);
428435
Assert.AreEqual(@"/// <summary>Destroys the specified window and its context.</summary>
429436
/// <param name=""window"">The window to destroy.</param>
430437
/// <remarks>
@@ -437,7 +444,8 @@ public void TestComments()
437444
/// <para>This function must not be called from a callback.</para>
438445
/// <para>This function must only be called from the main thread.</para>
439446
/// <para>Added in version 3.0. Replaces `glfwCloseWindow`.</para>
440-
/// </remarks>".Replace("\r", string.Empty), commentMethodDoxygenCustomTag.Replace("\r", string.Empty));
447+
/// </remarks>
448+
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
441449
}
442450

443451
[Test]

src/Generator.Tests/Passes/TestPasses.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ public void TestCleanCommentsPass()
7171
passBuilder.RunPasses(pass => pass.VisitDeclaration(c));
7272

7373
var para = (ParagraphComment) c.Comment.FullComment.Blocks[0];
74-
var s = para.CommentToString(CommentKind.BCPLSlash);
74+
var textGenerator = new TextGenerator();
75+
textGenerator.Print(para, CommentKind.BCPLSlash);
7576

76-
Assert.That(s, Is.EqualTo("/// <summary>A simple test.</summary>"));
77+
Assert.That(textGenerator.StringBuilder.ToString().Trim(),
78+
Is.EqualTo("/// <summary>A simple test.</summary>"));
7779
}
7880

7981
[Test]

src/Generator/Generators/CSharp/CSharpCommentPrinter.cs

+9-16
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace CppSharp.Generators.CSharp
99
{
1010
public static class CSharpCommentPrinter
1111
{
12-
public static string CommentToString(this Comment comment, CommentKind kind)
12+
public static void Print(this ITextGenerator textGenerator, Comment comment, CommentKind kind)
1313
{
1414
var sections = new List<Section> { new Section(CommentElement.Summary) };
1515
GetCommentSections(comment, sections);
1616
foreach (var section in sections)
1717
TrimSection(section);
18-
return FormatComment(sections, kind);
18+
FormatComment(textGenerator, sections, kind);
1919
}
2020

2121
private static void GetCommentSections(this Comment comment, List<Section> sections)
@@ -163,10 +163,9 @@ private static void TrimSection(Section section)
163163
}
164164
}
165165

166-
private static string FormatComment(List<Section> sections, CommentKind kind)
166+
private static void FormatComment(ITextGenerator textGenerator, List<Section> sections, CommentKind kind)
167167
{
168168
var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
169-
var commentBuilder = new StringBuilder();
170169

171170
sections.Sort((x, y) => x.Type.CompareTo(y.Type));
172171
var remarks = sections.Where(s => s.Type == CommentElement.Remarks).ToList();
@@ -182,26 +181,20 @@ private static string FormatComment(List<Section> sections, CommentKind kind)
182181
var attributes = string.Empty;
183182
if (section.Attributes.Any())
184183
attributes = ' ' + string.Join(" ", section.Attributes);
185-
commentBuilder.Append($"{commentPrefix} <{tag}{attributes}>");
184+
textGenerator.Write($"{commentPrefix} <{tag}{attributes}>");
186185
if (lines.Count == 1)
187186
{
188-
commentBuilder.Append(lines[0]);
187+
textGenerator.Write(lines[0]);
189188
}
190189
else
191190
{
192-
commentBuilder.AppendLine();
191+
textGenerator.NewLine();
193192
foreach (var line in lines)
194-
commentBuilder.AppendLine($"{commentPrefix} <para>{line}</para>");
195-
commentBuilder.Append($"{commentPrefix} ");
193+
textGenerator.WriteLine($"{commentPrefix} <para>{line}</para>");
194+
textGenerator.Write($"{commentPrefix} ");
196195
}
197-
commentBuilder.AppendLine($"</{tag}>");
196+
textGenerator.WriteLine($"</{tag}>");
198197
}
199-
if (commentBuilder.Length > 0)
200-
{
201-
var newLineLength = Environment.NewLine.Length;
202-
commentBuilder.Remove(commentBuilder.Length - newLineLength, newLineLength);
203-
}
204-
return commentBuilder.ToString();
205198
}
206199

207200
private class Section

src/Generator/Generators/CodeGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public virtual void GenerateComment(RawComment comment)
132132
if (comment.FullComment != null)
133133
{
134134
PushBlock(BlockKind.BlockComment);
135-
WriteLine(comment.FullComment.CommentToString(DocumentationCommentKind));
135+
ActiveBlock.Text.Print(comment.FullComment, DocumentationCommentKind);
136136
PopBlock();
137137
return;
138138
}

0 commit comments

Comments
 (0)