diff --git a/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs b/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs index e40521fc..c243a9a5 100644 --- a/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Collections.Generic; +using System.Text; using System.Web.Util; using CppSharp.AST; @@ -8,25 +9,22 @@ namespace CppSharp.Generators.CSharp { public static string CommentToString(this Comment comment, string commentPrefix) { - var summaryAdded = false; - var remarksAdded = false; - return CommentToString( - comment, ref summaryAdded, ref remarksAdded, commentPrefix).ToString(); + int boundary = 0; + var commentLines = GetCommentLines(comment, ref boundary); + TrimSection(commentLines, 0, boundary); + TrimSection(commentLines, boundary, commentLines.Count); + return FormatComment(commentLines, boundary, commentPrefix); } - private static StringBuilder CommentToString(Comment comment, - ref bool summaryAdded, ref bool remarksAdded, string commentPrefix) + private static List GetCommentLines(Comment comment, ref int boundary) { - var commentBuilder = new StringBuilder(); + var commentLines = new List(); switch (comment.Kind) { case CommentKind.FullComment: var fullComment = (FullComment) comment; foreach (var block in fullComment.Blocks) - commentBuilder.Append(CommentToString(block, - ref summaryAdded, ref remarksAdded, commentPrefix)); - if (remarksAdded) - commentBuilder.AppendFormat("{0} ", commentPrefix); + commentLines.AddRange(GetCommentLines(block, ref boundary)); break; case CommentKind.BlockCommandComment: break; @@ -39,10 +37,12 @@ namespace CppSharp.Generators.CSharp case CommentKind.VerbatimLineComment: break; case CommentKind.ParagraphComment: + var summaryParagraph = boundary == 0; var paragraphComment = (ParagraphComment) comment; foreach (var inlineContentComment in paragraphComment.Content) - commentBuilder.Append(CommentToString(inlineContentComment, - ref summaryAdded, ref remarksAdded, commentPrefix)); + commentLines.AddRange(GetCommentLines(inlineContentComment, ref boundary)); + if (summaryParagraph) + boundary = commentLines.Count; break; case CommentKind.HTMLTagComment: break; @@ -51,21 +51,9 @@ namespace CppSharp.Generators.CSharp case CommentKind.HTMLEndTagComment: break; case CommentKind.TextComment: - if (!summaryAdded) - commentBuilder.AppendFormat("{0} ", commentPrefix).AppendLine(); - if (summaryAdded && !remarksAdded) - { - commentBuilder.AppendFormat("{0} ", commentPrefix).AppendLine(); - remarksAdded = true; - } - commentBuilder.AppendFormat( - "{0} {1}", commentPrefix, GetText(comment)); - commentBuilder.AppendLine(); - if (!summaryAdded) - { - commentBuilder.AppendFormat("{0} ", commentPrefix).AppendLine(); - summaryAdded = true; - } + commentLines.Add(GetText(comment)); + if (boundary == 0) + boundary = commentLines.Count; break; case CommentKind.InlineContentComment: break; @@ -74,7 +62,7 @@ namespace CppSharp.Generators.CSharp case CommentKind.VerbatimBlockLineComment: break; } - return commentBuilder; + return commentLines; } private static string GetText(Comment comment) @@ -84,5 +72,47 @@ namespace CppSharp.Generators.CSharp return HtmlEncoder.HtmlEncode( text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text); } + + private static void TrimSection(List commentLines, int start, int end) + { + for (int i = start; i < end; i++) + { + if (string.IsNullOrWhiteSpace(commentLines[i])) + commentLines.RemoveAt(i--); + else + break; + } + for (int i = end - 1; i >= start; i--) + { + if (string.IsNullOrWhiteSpace(commentLines[i])) + commentLines.RemoveAt(i); + else + break; + } + } + + private static string FormatComment(List commentLines, int boundary, string commentPrefix) + { + var commentBuilder = new StringBuilder(); + commentBuilder.AppendLine(""); + for (int i = 0; i < boundary; i++) + { + commentBuilder.AppendFormat("{0} {1}", commentPrefix, commentLines[i]); + commentBuilder.AppendLine(); + } + commentBuilder.Append(""); + if (boundary < commentLines.Count) + { + commentBuilder.AppendLine(); + commentBuilder.AppendLine(""); + for (int i = boundary; i < commentLines.Count; i++) + { + commentBuilder.AppendFormat("{0} {1}", commentPrefix, commentLines[i]); + commentBuilder.AppendLine(); + } + commentBuilder.Append(""); + } + return commentBuilder.ToString(); + } } } diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 6248d528..2434959d 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -581,6 +581,11 @@ int OverridesNonDirectVirtual::retInt() return 3; } +const char * TestComments::GetIOHandlerControlSequence(char ch) +{ + return 0; +} + AbstractWithVirtualDtor::AbstractWithVirtualDtor() { } diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 50eb04ce..d0e302a0 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -987,7 +987,28 @@ AbstractTemplate::AbstractTemplate() /** 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. */ -class DLL_API TestComments {}; +class DLL_API TestComments +{ +public: + //---------------------------------------------------------------------- + /// Get the string that needs to be written to the debugger stdin file + /// handle when a control character is typed. + /// + /// Some GUI programs will intercept "control + char" sequences and want + /// to have them do what normally would happen when using a real + /// terminal, so this function allows GUI programs to emulate this + /// functionality. + /// + /// @param[in] ch + /// The character that was typed along with the control key + /// + /// @return + /// The string that should be written into the file handle that is + /// feeding the input stream for the debugger, or NULL if there is + /// no string for this control key. + //---------------------------------------------------------------------- + const char * GetIOHandlerControlSequence(char ch); +}; class DLL_API AbstractWithVirtualDtor {