Browse Source

Added support for "return" elements in code comments in the C# end.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/642/head
Dimitar Dobrev 10 years ago
parent
commit
1f105e53a8
  1. 4
      src/AST/Comment.cs
  2. 106
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs

4
src/AST/Comment.cs

@ -492,6 +492,7 @@ namespace CppSharp.AST
Constant, Constant,
Copyright, Copyright,
Date, Date,
Def,
Defgroup, Defgroup,
Dependency, Dependency,
Deprecated, Deprecated,
@ -502,6 +503,7 @@ namespace CppSharp.AST
E, E,
Em, Em,
Enum, Enum,
Exception,
Flbrace, Flbrace,
Frbrace, Frbrace,
Flsquare, Flsquare,
@ -574,6 +576,8 @@ namespace CppSharp.AST
Templatefield, Templatefield,
Textblock, Textblock,
Slashtextblock, Slashtextblock,
Throw,
Throws,
Todo, Todo,
Tparam, Tparam,
Typedef, Typedef,

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

@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Web.Util; using System.Web.Util;
using CppSharp.AST; using CppSharp.AST;
@ -9,26 +11,32 @@ namespace CppSharp.Generators.CSharp
{ {
public static string CommentToString(this Comment comment, string commentPrefix) public static string CommentToString(this Comment comment, string commentPrefix)
{ {
int boundary = 0; var sections = new List<Section> { new Section(CommentElement.Summary) };
var commentLines = GetCommentLines(comment, ref boundary); GetCommentSections(comment, sections);
TrimSection(commentLines, 0, boundary); foreach (var section in sections)
TrimSection(commentLines, boundary, commentLines.Count); TrimSection(section);
return FormatComment(commentLines, boundary, commentPrefix); return FormatComment(sections, commentPrefix);
} }
private static List<string> GetCommentLines(Comment comment, ref int boundary) private static void GetCommentSections(this Comment comment, List<Section> sections)
{ {
var commentLines = new List<string>();
switch (comment.Kind) switch (comment.Kind)
{ {
case CommentKind.FullComment: case CommentKind.FullComment:
var fullComment = (FullComment) comment; var fullComment = (FullComment) comment;
foreach (var block in fullComment.Blocks) foreach (var block in fullComment.Blocks)
commentLines.AddRange(GetCommentLines(block, ref boundary)); block.GetCommentSections(sections);
break; break;
case CommentKind.BlockCommandComment: case CommentKind.BlockCommandComment:
var blockCommandComment = (BlockCommandComment) comment;
if (blockCommandComment.CommandKind == CommentCommandKind.Return)
{
sections.Add(new Section(CommentElement.Returns));
blockCommandComment.ParagraphComment.GetCommentSections(sections);
}
break; break;
case CommentKind.ParamCommandComment: case CommentKind.ParamCommandComment:
var paramCommandComment = (ParamCommandComment) comment;
break; break;
case CommentKind.TParamCommandComment: case CommentKind.TParamCommandComment:
break; break;
@ -37,12 +45,16 @@ namespace CppSharp.Generators.CSharp
case CommentKind.VerbatimLineComment: case CommentKind.VerbatimLineComment:
break; break;
case CommentKind.ParagraphComment: case CommentKind.ParagraphComment:
var summaryParagraph = boundary == 0; var summaryParagraph = sections.Count == 1;
var paragraphComment = (ParagraphComment) comment; var paragraphComment = (ParagraphComment) comment;
foreach (var inlineContentComment in paragraphComment.Content) foreach (var inlineContentComment in paragraphComment.Content)
commentLines.AddRange(GetCommentLines(inlineContentComment, ref boundary)); inlineContentComment.GetCommentSections(sections);
if (summaryParagraph) if (summaryParagraph)
boundary = commentLines.Count; {
sections[0].Lines.AddRange(sections.Skip(1).SelectMany(s => s.Lines));
sections.RemoveRange(1, sections.Count - 1);
sections.Add(new Section(CommentElement.Remarks));
}
break; break;
case CommentKind.HTMLTagComment: case CommentKind.HTMLTagComment:
break; break;
@ -51,9 +63,9 @@ namespace CppSharp.Generators.CSharp
case CommentKind.HTMLEndTagComment: case CommentKind.HTMLEndTagComment:
break; break;
case CommentKind.TextComment: case CommentKind.TextComment:
commentLines.Add(GetText(comment)); sections.Last().Lines.Add(GetText(comment, sections.Last().Type == CommentElement.Returns));
if (boundary == 0) if (sections.Count == 1)
boundary = commentLines.Count; sections.Add(new Section(CommentElement.Remarks));
break; break;
case CommentKind.InlineContentComment: case CommentKind.InlineContentComment:
break; break;
@ -62,57 +74,79 @@ namespace CppSharp.Generators.CSharp
case CommentKind.VerbatimBlockLineComment: case CommentKind.VerbatimBlockLineComment:
break; break;
} }
return commentLines;
} }
private static string GetText(Comment comment) private static string GetText(Comment comment, bool trim = false)
{ {
var textComment = ((TextComment) comment); var textComment = ((TextComment) comment);
var text = textComment.Text; var text = textComment.Text;
if (trim)
text = text.Trim();
return HtmlEncoder.HtmlEncode( return HtmlEncoder.HtmlEncode(
text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text); text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text);
} }
private static void TrimSection(List<string> commentLines, int start, int end) private static void TrimSection(Section section)
{ {
for (int i = start; i < end; i++) for (int i = 0; i < section.Lines.Count - 1; i++)
{ {
if (string.IsNullOrWhiteSpace(commentLines[i])) if (string.IsNullOrWhiteSpace(section.Lines[i]))
commentLines.RemoveAt(i--); section.Lines.RemoveAt(i--);
else else
break; break;
} }
for (int i = end - 1; i >= start; i--) for (int i = section.Lines.Count - 1; i >= 0; i--)
{ {
if (string.IsNullOrWhiteSpace(commentLines[i])) if (string.IsNullOrWhiteSpace(section.Lines[i]))
commentLines.RemoveAt(i); section.Lines.RemoveAt(i);
else else
break; break;
} }
} }
private static string FormatComment(List<string> commentLines, int boundary, string commentPrefix) private static string FormatComment(List<Section> sections, string commentPrefix)
{ {
var commentBuilder = new StringBuilder(); var commentBuilder = new StringBuilder();
commentBuilder.AppendLine("<summary>"); foreach (var section in sections.Where(s => s.Lines.Count > 0))
for (int i = 0; i < boundary; i++)
{ {
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, commentLines[i]); var tag = section.Type.ToString().ToLowerInvariant();
commentBuilder.AppendFormat("<{0}>", tag);
commentBuilder.AppendLine(); commentBuilder.AppendLine();
} foreach (var line in section.Lines)
commentBuilder.Append("</summary>");
if (boundary < commentLines.Count)
{ {
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, line);
commentBuilder.AppendLine(); commentBuilder.AppendLine();
commentBuilder.AppendLine("<remarks>"); }
for (int i = boundary; i < commentLines.Count; i++) commentBuilder.AppendFormat("</{0}>", tag);
{
commentBuilder.AppendFormat("{0} <para>{1}</para>", commentPrefix, commentLines[i]);
commentBuilder.AppendLine(); commentBuilder.AppendLine();
} }
commentBuilder.Append("</remarks>"); if (commentBuilder.Length > 0)
{
var newLineLength = Environment.NewLine.Length;
commentBuilder.Remove(commentBuilder.Length - newLineLength, newLineLength);
} }
return commentBuilder.ToString(); return commentBuilder.ToString();
} }
private class Section
{
public Section(CommentElement type)
{
Type = type;
Lines = new List<string>();
}
public CommentElement Type { get; set; }
public List<string> Lines { get; private set; }
}
private enum CommentElement
{
Summary,
Remarks,
Param,
Returns
}
} }
} }

Loading…
Cancel
Save