From c6ffa9ddf2d4e0544725a8ad2c08e78f6a5f7ae6 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Thu, 11 Jul 2019 14:48:12 +0300 Subject: [PATCH] Fix the regressed indentation of printed comments Signed-off-by: Dimitar Dobrev --- src/Generator.Tests/AST/TestAST.cs | 26 ++++++++++++------- src/Generator.Tests/Passes/TestPasses.cs | 6 +++-- .../Generators/CSharp/CSharpCommentPrinter.cs | 25 +++++++----------- src/Generator/Generators/CodeGenerator.cs | 2 +- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/Generator.Tests/AST/TestAST.cs b/src/Generator.Tests/AST/TestAST.cs index 2841aa4f..c2c2b05d 100644 --- a/src/Generator.Tests/AST/TestAST.cs +++ b/src/Generator.Tests/AST/TestAST.cs @@ -383,15 +383,18 @@ namespace CppSharp.Generator.Tests.AST public void TestComments() { var @class = AstContext.FindCompleteClass("TestComments"); - var commentClass = @class.Comment.FullComment.CommentToString(CommentKind.BCPLSlash); + var textGenerator = new TextGenerator(); + textGenerator.Print(@class.Comment.FullComment, CommentKind.BCPLSlash); Assert.AreEqual(@"/// /// Hash set/map base class. /// Note that to prevent extra memory use due to vtable pointer, %HashBase intentionally does not declare a virtual destructor /// and therefore %HashBase pointers should never be used. -/// ".Replace("\r", string.Empty), commentClass.Replace("\r", string.Empty)); +/// +".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString()); var method = @class.Methods.First(m => m.Name == "GetIOHandlerControlSequence"); - var commentMethod = method.Comment.FullComment.CommentToString(CommentKind.BCPL); + textGenerator.StringBuilder.Clear(); + textGenerator.Print(method.Comment.FullComment, CommentKind.BCPL); Assert.AreEqual(@"// // Get the string that needs to be written to the debugger stdin file // handle when a control character is typed. @@ -407,10 +410,12 @@ namespace CppSharp.Generator.Tests.AST // to have them do what normally would happen when using a real // terminal, so this function allows GUI programs to emulate this // functionality. -// ".Replace("\r", string.Empty), commentMethod.Replace("\r", string.Empty)); +// +".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString()); var methodTestDoxygen = @class.Methods.First(m => m.Name == "SBAttachInfo"); - var commentMethodDoxygen = methodTestDoxygen.Comment.FullComment.CommentToString(CommentKind.BCPLSlash); + textGenerator.StringBuilder.Clear(); + textGenerator.Print(methodTestDoxygen.Comment.FullComment, CommentKind.BCPLSlash); Assert.AreEqual(@"/// Attach to a process by name. /// A full or partial name for the process to attach to. /// @@ -420,11 +425,13 @@ namespace CppSharp.Generator.Tests.AST /// /// This function implies that a future call to SBTarget::Attach(...) /// will be synchronous. -/// ".Replace("\r", string.Empty), commentMethodDoxygen.Replace("\r", string.Empty)); +/// +".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString()); var methodDoxygenCustomTags = @class.Methods.First(m => m.Name == "glfwDestroyWindow"); - new CleanCommentsPass { }.VisitFull(methodDoxygenCustomTags.Comment.FullComment); - var commentMethodDoxygenCustomTag = methodDoxygenCustomTags.Comment.FullComment.CommentToString(CommentKind.BCPLSlash); + new CleanCommentsPass().VisitFull(methodDoxygenCustomTags.Comment.FullComment); + textGenerator.StringBuilder.Clear(); + textGenerator.Print(methodDoxygenCustomTags.Comment.FullComment, CommentKind.BCPLSlash); Assert.AreEqual(@"/// Destroys the specified window and its context. /// The window to destroy. /// @@ -437,7 +444,8 @@ namespace CppSharp.Generator.Tests.AST /// This function must not be called from a callback. /// This function must only be called from the main thread. /// Added in version 3.0. Replaces `glfwCloseWindow`. -/// ".Replace("\r", string.Empty), commentMethodDoxygenCustomTag.Replace("\r", string.Empty)); +/// +".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString()); } [Test] diff --git a/src/Generator.Tests/Passes/TestPasses.cs b/src/Generator.Tests/Passes/TestPasses.cs index 34f36e60..5416a711 100644 --- a/src/Generator.Tests/Passes/TestPasses.cs +++ b/src/Generator.Tests/Passes/TestPasses.cs @@ -71,9 +71,11 @@ namespace CppSharp.Generator.Tests.Passes passBuilder.RunPasses(pass => pass.VisitDeclaration(c)); var para = (ParagraphComment) c.Comment.FullComment.Blocks[0]; - var s = para.CommentToString(CommentKind.BCPLSlash); + var textGenerator = new TextGenerator(); + textGenerator.Print(para, CommentKind.BCPLSlash); - Assert.That(s, Is.EqualTo("/// A simple test.")); + Assert.That(textGenerator.StringBuilder.ToString().Trim(), + Is.EqualTo("/// A simple test.")); } [Test] diff --git a/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs b/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs index c5318a8b..7129fda1 100644 --- a/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs @@ -9,13 +9,13 @@ namespace CppSharp.Generators.CSharp { public static class CSharpCommentPrinter { - public static string CommentToString(this Comment comment, CommentKind kind) + public static void Print(this ITextGenerator textGenerator, Comment comment, CommentKind kind) { var sections = new List
{ new Section(CommentElement.Summary) }; GetCommentSections(comment, sections); foreach (var section in sections) TrimSection(section); - return FormatComment(sections, kind); + FormatComment(textGenerator, sections, kind); } private static void GetCommentSections(this Comment comment, List
sections) @@ -163,10 +163,9 @@ namespace CppSharp.Generators.CSharp } } - private static string FormatComment(List
sections, CommentKind kind) + private static void FormatComment(ITextGenerator textGenerator, List
sections, CommentKind kind) { var commentPrefix = Comment.GetMultiLineCommentPrologue(kind); - var commentBuilder = new StringBuilder(); sections.Sort((x, y) => x.Type.CompareTo(y.Type)); var remarks = sections.Where(s => s.Type == CommentElement.Remarks).ToList(); @@ -182,26 +181,20 @@ namespace CppSharp.Generators.CSharp var attributes = string.Empty; if (section.Attributes.Any()) attributes = ' ' + string.Join(" ", section.Attributes); - commentBuilder.Append($"{commentPrefix} <{tag}{attributes}>"); + textGenerator.Write($"{commentPrefix} <{tag}{attributes}>"); if (lines.Count == 1) { - commentBuilder.Append(lines[0]); + textGenerator.Write(lines[0]); } else { - commentBuilder.AppendLine(); + textGenerator.NewLine(); foreach (var line in lines) - commentBuilder.AppendLine($"{commentPrefix} {line}"); - commentBuilder.Append($"{commentPrefix} "); + textGenerator.WriteLine($"{commentPrefix} {line}"); + textGenerator.Write($"{commentPrefix} "); } - commentBuilder.AppendLine($""); + textGenerator.WriteLine($""); } - if (commentBuilder.Length > 0) - { - var newLineLength = Environment.NewLine.Length; - commentBuilder.Remove(commentBuilder.Length - newLineLength, newLineLength); - } - return commentBuilder.ToString(); } private class Section diff --git a/src/Generator/Generators/CodeGenerator.cs b/src/Generator/Generators/CodeGenerator.cs index 0f183002..acc02fd7 100644 --- a/src/Generator/Generators/CodeGenerator.cs +++ b/src/Generator/Generators/CodeGenerator.cs @@ -132,7 +132,7 @@ namespace CppSharp.Generators if (comment.FullComment != null) { PushBlock(BlockKind.BlockComment); - WriteLine(comment.FullComment.CommentToString(DocumentationCommentKind)); + ActiveBlock.Text.Print(comment.FullComment, DocumentationCommentKind); PopBlock(); return; }