Browse Source

Properly printed complex comments.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/573/head
Dimitar Dobrev 10 years ago
parent
commit
287ad2394d
  1. 64
      src/AST/Comment.cs
  2. 85
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
  3. 40
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 6
      tests/Common/Common.h

64
src/AST/Comment.cs

@ -25,6 +25,25 @@ namespace CppSharp.AST @@ -25,6 +25,25 @@ namespace CppSharp.AST
Merged
}
public enum CommentKind
{
FullComment,
BlockContentComment,
BlockCommandComment,
ParamCommandComment,
TParamCommandComment,
VerbatimBlockComment,
VerbatimLineComment,
ParagraphComment,
HTMLTagComment,
HTMLStartTagComment,
HTMLEndTagComment,
TextComment,
InlineContentComment,
InlineCommandComment,
VerbatimBlockLineComment,
}
/// <summary>
/// Represents a raw C++ comment.
/// </summary>
@ -103,6 +122,8 @@ namespace CppSharp.AST @@ -103,6 +122,8 @@ namespace CppSharp.AST
/// </summary>
public abstract class Comment
{
public CommentKind Kind { get; set; }
public abstract void Visit<T>(ICommentVisitor<T> visitor);
}
@ -157,6 +178,7 @@ namespace CppSharp.AST @@ -157,6 +178,7 @@ namespace CppSharp.AST
public BlockCommandComment()
{
Kind = CommentKind.BlockCommandComment;
Arguments = new List<Argument>();
}
@ -174,6 +196,11 @@ namespace CppSharp.AST @@ -174,6 +196,11 @@ namespace CppSharp.AST
public const uint InvalidParamIndex = ~0U;
public const uint VarArgParamIndex = ~0U/*InvalidParamIndex*/ - 1U;
public ParamCommandComment()
{
Kind = CommentKind.ParamCommandComment;
}
public enum PassDirection
{
In,
@ -222,6 +249,7 @@ namespace CppSharp.AST @@ -222,6 +249,7 @@ namespace CppSharp.AST
public TParamCommandComment()
{
Kind = CommentKind.TParamCommandComment;
Position = new List<uint>();
}
@ -242,6 +270,7 @@ namespace CppSharp.AST @@ -242,6 +270,7 @@ namespace CppSharp.AST
public VerbatimBlockComment()
{
Kind = CommentKind.VerbatimBlockComment;
Lines = new List<VerbatimBlockLineComment>();
}
@ -260,6 +289,11 @@ namespace CppSharp.AST @@ -260,6 +289,11 @@ namespace CppSharp.AST
{
public string Text;
public VerbatimLineComment()
{
Kind = CommentKind.VerbatimLineComment;
}
public override void Visit<T>(ICommentVisitor<T> visitor)
{
visitor.VisitVerbatimLine(this);
@ -277,6 +311,7 @@ namespace CppSharp.AST @@ -277,6 +311,7 @@ namespace CppSharp.AST
public ParagraphComment()
{
Kind = CommentKind.ParagraphComment;
Content = new List<InlineContentComment>();
}
@ -291,7 +326,10 @@ namespace CppSharp.AST @@ -291,7 +326,10 @@ namespace CppSharp.AST
/// </summary>
public abstract class InlineContentComment : Comment
{
protected InlineContentComment()
{
Kind = CommentKind.InlineContentComment;
}
}
/// <summary>
@ -302,6 +340,11 @@ namespace CppSharp.AST @@ -302,6 +340,11 @@ namespace CppSharp.AST
public abstract class HTMLTagComment : InlineContentComment
{
public string TagName;
protected HTMLTagComment()
{
Kind = CommentKind.HTMLTagComment;
}
}
/// <summary>
@ -319,6 +362,7 @@ namespace CppSharp.AST @@ -319,6 +362,7 @@ namespace CppSharp.AST
public HTMLStartTagComment()
{
Kind = CommentKind.HTMLStartTagComment;
Attributes = new List<Attribute>();
}
@ -333,6 +377,11 @@ namespace CppSharp.AST @@ -333,6 +377,11 @@ namespace CppSharp.AST
/// </summary>
public class HTMLEndTagComment : HTMLTagComment
{
public HTMLEndTagComment()
{
Kind = CommentKind.HTMLEndTagComment;
}
public override void Visit<T>(ICommentVisitor<T> visitor)
{
visitor.VisitHTMLEndTag(this);
@ -346,6 +395,11 @@ namespace CppSharp.AST @@ -346,6 +395,11 @@ namespace CppSharp.AST
{
public string Text;
public TextComment()
{
Kind = CommentKind.TextComment;
}
public override void Visit<T>(ICommentVisitor<T> visitor)
{
visitor.VisitText(this);
@ -370,12 +424,13 @@ namespace CppSharp.AST @@ -370,12 +424,13 @@ namespace CppSharp.AST
RenderEmphasized
}
public RenderKind Kind;
public RenderKind CommentRenderKind;
public List<Argument> Arguments;
public InlineCommandComment()
{
Kind = CommentKind.InlineCommandComment;
Arguments = new List<Argument>();
}
@ -392,6 +447,11 @@ namespace CppSharp.AST @@ -392,6 +447,11 @@ namespace CppSharp.AST
{
public string Text;
public VerbatimBlockLineComment()
{
Kind = CommentKind.VerbatimBlockLineComment;
}
public override void Visit<T>(ICommentVisitor<T> visitor)
{
visitor.VisitVerbatimBlockLine(this);

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

@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
using System.Text;
using System.Web.Util;
using CppSharp.AST;
namespace CppSharp.Generators.CSharp
{
public static class CSharpCommentPrinter
{
public static string CommentToString(this Comment comment)
{
var summaryAdded = false;
var remarksAdded = false;
return CommentToString(comment, ref summaryAdded, ref remarksAdded).ToString();
}
private static StringBuilder CommentToString(Comment comment,
ref bool summaryAdded, ref bool remarksAdded)
{
var commentBuilder = new StringBuilder();
switch (comment.Kind)
{
case CommentKind.FullComment:
var fullComment = (FullComment) comment;
foreach (var block in fullComment.Blocks)
commentBuilder.Append(CommentToString(block,
ref summaryAdded, ref remarksAdded));
if (remarksAdded)
commentBuilder.Append("</remarks>");
break;
case CommentKind.BlockCommandComment:
break;
case CommentKind.ParamCommandComment:
break;
case CommentKind.TParamCommandComment:
break;
case CommentKind.VerbatimBlockComment:
break;
case CommentKind.VerbatimLineComment:
break;
case CommentKind.ParagraphComment:
var paragraphComment = (ParagraphComment) comment;
foreach (var inlineContentComment in paragraphComment.Content)
commentBuilder.Append(CommentToString(inlineContentComment,
ref summaryAdded, ref remarksAdded));
break;
case CommentKind.HTMLTagComment:
break;
case CommentKind.HTMLStartTagComment:
break;
case CommentKind.HTMLEndTagComment:
break;
case CommentKind.TextComment:
if (!summaryAdded)
commentBuilder.AppendLine("<summary>");
if (summaryAdded && !remarksAdded)
{
commentBuilder.AppendLine("<remarks>");
remarksAdded = true;
}
commentBuilder.Append("<para>" + GetText(comment) + "</para>").AppendLine();
if (!summaryAdded)
{
commentBuilder.AppendLine("</summary>");
summaryAdded = true;
}
break;
case CommentKind.InlineContentComment:
break;
case CommentKind.InlineCommandComment:
break;
case CommentKind.VerbatimBlockLineComment:
break;
}
return commentBuilder;
}
private static string GetText(Comment comment)
{
var textComment = ((TextComment) comment);
var text = textComment.Text;
return HtmlEncoder.HtmlEncode(
text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text);
}
}
}

40
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -306,25 +306,25 @@ namespace CppSharp.Generators.CSharp @@ -306,25 +306,25 @@ namespace CppSharp.Generators.CSharp
public void GenerateComment(RawComment comment)
{
if (string.IsNullOrWhiteSpace(comment.BriefText))
return;
PushBlock(BlockKind.BlockComment);
WriteLine("/// <summary>");
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
WriteLine("/// <para>{0}</para>", line);
WriteLine("/// </summary>");
if (!string.IsNullOrWhiteSpace(comment.Text))
if (comment.FullComment != null)
{
WriteLine("/// <remarks>");
foreach (string line in HtmlEncoder.HtmlEncode(comment.Text).Split(
PushBlock(BlockKind.BlockComment);
WriteLine(comment.FullComment.CommentToString());
PopBlock();
}
else
{
if (string.IsNullOrWhiteSpace(comment.BriefText))
return;
PushBlock(BlockKind.BlockComment);
WriteLine("<summary>");
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
WriteLine("/// <para>{0}</para>", line);
WriteLine("/// </remarks>");
WriteLine("<para>{0}</para>", line);
WriteLine("</summary>");
PopBlock();
}
PopBlock();
}
public void GenerateInlineSummary(RawComment comment)
@ -337,15 +337,15 @@ namespace CppSharp.Generators.CSharp @@ -337,15 +337,15 @@ namespace CppSharp.Generators.CSharp
PushBlock(BlockKind.InlineComment);
if (comment.BriefText.Contains("\n"))
{
WriteLine("/// <summary>");
WriteLine("{0} <summary>", Options.CommentPrefix);
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
WriteLine("/// <para>{0}</para>", line);
WriteLine("/// </summary>");
WriteLine("{0} <para>{1}</para>", Options.CommentPrefix, line);
WriteLine("{0} </summary>", Options.CommentPrefix);
}
else
{
WriteLine("/// <summary>{0}</summary>", comment.BriefText);
WriteLine("{0} <summary>{1}</summary>", Options.CommentPrefix, comment.BriefText);
}
PopBlock();
}

6
tests/Common/Common.h

@ -927,3 +927,9 @@ template <typename T> @@ -927,3 +927,9 @@ template <typename T>
AbstractTemplate<T>::AbstractTemplate()
{
}
/// 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.
*/
class DLL_API TestComments {};

Loading…
Cancel
Save