Browse Source

Fixed #453 - incorrect comment generation.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/642/head
Dimitar Dobrev 9 years ago
parent
commit
1d3e821093
  1. 90
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
  2. 5
      tests/Common/Common.cpp
  3. 23
      tests/Common/Common.h

90
src/Generator/Generators/CSharp/CSharpCommentPrinter.cs

@ -1,4 +1,5 @@ @@ -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 @@ -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<string> GetCommentLines(Comment comment, ref int boundary)
{
var commentBuilder = new StringBuilder();
var commentLines = new List<string>();
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} </remarks>", commentPrefix);
commentLines.AddRange(GetCommentLines(block, ref boundary));
break;
case CommentKind.BlockCommandComment:
break;
@ -39,10 +37,12 @@ namespace CppSharp.Generators.CSharp @@ -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 @@ -51,21 +51,9 @@ namespace CppSharp.Generators.CSharp
case CommentKind.HTMLEndTagComment:
break;
case CommentKind.TextComment:
if (!summaryAdded)
commentBuilder.AppendFormat("{0} <summary>", commentPrefix).AppendLine();
if (summaryAdded && !remarksAdded)
{
commentBuilder.AppendFormat("{0} <remarks>", commentPrefix).AppendLine();
remarksAdded = true;
}
commentBuilder.AppendFormat(
"{0} <para>{1}</para>", commentPrefix, GetText(comment));
commentBuilder.AppendLine();
if (!summaryAdded)
{
commentBuilder.AppendFormat("{0} </summary>", 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 @@ -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 @@ -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<string> 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<string> commentLines, int boundary, string commentPrefix)
{
var commentBuilder = new StringBuilder();
commentBuilder.AppendLine("<summary>");
for (int i = 0; i < boundary; i++)
{
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, commentLines[i]);
commentBuilder.AppendLine();
}
commentBuilder.Append("</summary>");
if (boundary < commentLines.Count)
{
commentBuilder.AppendLine();
commentBuilder.AppendLine("<remarks>");
for (int i = boundary; i < commentLines.Count; i++)
{
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, commentLines[i]);
commentBuilder.AppendLine();
}
commentBuilder.Append("</remarks>");
}
return commentBuilder.ToString();
}
}
}

5
tests/Common/Common.cpp

@ -581,6 +581,11 @@ int OverridesNonDirectVirtual::retInt() @@ -581,6 +581,11 @@ int OverridesNonDirectVirtual::retInt()
return 3;
}
const char * TestComments::GetIOHandlerControlSequence(char ch)
{
return 0;
}
AbstractWithVirtualDtor::AbstractWithVirtualDtor()
{
}

23
tests/Common/Common.h

@ -987,7 +987,28 @@ AbstractTemplate<T>::AbstractTemplate() @@ -987,7 +987,28 @@ AbstractTemplate<T>::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
{

Loading…
Cancel
Save