Browse Source

Unify common CLI and C# comments and documentation generation.

pull/778/head
Joao Matos 9 years ago
parent
commit
77f809bce2
  1. 39
      src/Generator/Generators/CLI/CLITemplate.cs
  2. 8
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
  3. 75
      src/Generator/Generators/CodeGenerator.cs
  4. 9
      src/Generator/Options.cs

39
src/Generator/Generators/CLI/CLITemplate.cs

@ -108,45 +108,6 @@ namespace CppSharp.Generators.CLI @@ -108,45 +108,6 @@ namespace CppSharp.Generators.CLI
return method.Name;
}
public void GenerateDeclarationCommon(Declaration decl)
{
if (decl.Comment == null)
return;
GenerateSummary(decl.Comment.BriefText);
GenerateDebug(decl);
}
public void GenerateSummary(string comment)
{
if (string.IsNullOrWhiteSpace(comment))
return;
PushBlock(BlockKind.BlockComment);
WriteLine("/// <summary>");
WriteLine("/// {0}", comment);
WriteLine("/// </summary>");
PopBlock();
}
public void GenerateInlineSummary(RawComment comment)
{
if (comment == null) return;
if (String.IsNullOrWhiteSpace(comment.BriefText))
return;
PushBlock(BlockKind.InlineComment);
WriteLine("/// <summary> {0} </summary>", comment.BriefText);
PopBlock();
}
public void GenerateDebug(Declaration decl)
{
if (Options.GenerateDebugOutput && !string.IsNullOrWhiteSpace(decl.DebugText))
WriteLine("// DEBUG: " + decl.DebugText);
}
public void GenerateMethodParameters(Method method)
{
for (var i = 0; i < method.Parameters.Count; ++i)

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

@ -9,13 +9,13 @@ namespace CppSharp.Generators.CSharp @@ -9,13 +9,13 @@ namespace CppSharp.Generators.CSharp
{
public static class CSharpCommentPrinter
{
public static string CommentToString(this Comment comment, string commentPrefix)
public static string CommentToString(this Comment comment, CommentKind kind)
{
var sections = new List<Section> { new Section(CommentElement.Summary) };
GetCommentSections(comment, sections);
foreach (var section in sections)
TrimSection(section);
return FormatComment(sections, commentPrefix);
return FormatComment(sections, kind);
}
private static void GetCommentSections(this Comment comment, List<Section> sections)
@ -115,8 +115,10 @@ namespace CppSharp.Generators.CSharp @@ -115,8 +115,10 @@ namespace CppSharp.Generators.CSharp
}
}
private static string FormatComment(List<Section> sections, string commentPrefix)
private static string FormatComment(List<Section> sections, CommentKind kind)
{
var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
var commentBuilder = new StringBuilder();
foreach (var section in sections.Where(s => s.Lines.Count > 0))
{

75
src/Generator/Generators/CodeGenerator.cs

@ -21,6 +21,25 @@ namespace CppSharp.Generators @@ -21,6 +21,25 @@ namespace CppSharp.Generators
public virtual string FilePath =>
$"{TranslationUnit.FileNameWithoutExtension}.{FileExtension}";
/// <summary>
/// Gets the comment style kind for regular comments.
/// </summary>
public virtual CommentKind CommentKind
{
get
{
if (!Options.CommentKind.HasValue)
return CommentKind.BCPL;
return Options.CommentKind.Value;
}
}
/// <summary>
/// Gets the comment style kind for documentation comments.
/// </summary>
public virtual CommentKind DocumentationCommentKind => CommentKind.BCPLSlash;
protected CodeGenerator(BindingContext context, TranslationUnit unit)
: this(context, new List<TranslationUnit> { unit })
{
@ -57,35 +76,31 @@ namespace CppSharp.Generators @@ -57,35 +76,31 @@ namespace CppSharp.Generators
WriteLine("// DEBUG: " + decl.DebugText);
}
public void GenerateInlineSummary(RawComment comment)
{
if (comment == null) return;
#region Comment generation
if (string.IsNullOrWhiteSpace(comment.BriefText))
public virtual void GenerateSummary(string comment)
{
if (string.IsNullOrWhiteSpace(comment))
return;
PushBlock(BlockKind.InlineComment);
if (comment.BriefText.Contains("\n"))
{
WriteLine("{0} <summary>", Options.CommentPrefix);
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
WriteLine("{0} <para>{1}</para>", Options.CommentPrefix, line);
WriteLine("{0} </summary>", Options.CommentPrefix);
}
else
{
WriteLine("{0} <summary>{1}</summary>", Options.CommentPrefix, comment.BriefText);
}
PushBlock(BlockKind.BlockComment);
WriteLine("/// <summary>");
WriteLine("/// {0}", comment);
WriteLine("/// </summary>");
PopBlock();
}
public virtual void GenerateInlineSummary(RawComment comment)
{
GenerateComment(comment);
}
public virtual void GenerateComment(RawComment comment)
{
if (comment.FullComment != null)
{
PushBlock(BlockKind.BlockComment);
WriteLine(comment.FullComment.CommentToString(Options.CommentPrefix));
WriteLine(comment.FullComment.CommentToString(CommentKind));
PopBlock();
return;
}
@ -93,12 +108,24 @@ namespace CppSharp.Generators @@ -93,12 +108,24 @@ namespace CppSharp.Generators
if (string.IsNullOrWhiteSpace(comment.BriefText))
return;
var lines = new List<string>();
PushBlock(BlockKind.BlockComment);
WriteLine("{0} <summary>", Options.CommentPrefix);
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
WriteLine("{0} <para>{1}</para>", Options.CommentPrefix, line);
WriteLine("{0} </summary>", Options.CommentPrefix);
if (comment.BriefText.Contains("\n"))
{
lines.Add("<summary>");
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
lines.Add($"<para>{line}</para>");
lines.Add("</summary>");
}
else
{
lines.Add($"<summary>{comment.BriefText}</summary>");
}
PopBlock();
GenerateMultiLineComment(lines, CommentKind);
PopBlock();
}
@ -117,6 +144,8 @@ namespace CppSharp.Generators @@ -117,6 +144,8 @@ namespace CppSharp.Generators
WriteLine("{0}", lineCommentEpilogue);
}
#endregion
public virtual void GenerateFilePreamble(CommentKind kind)
{
var lines = new List<string>

9
src/Generator/Options.cs

@ -26,7 +26,6 @@ namespace CppSharp @@ -26,7 +26,6 @@ namespace CppSharp
GeneratorKind = GeneratorKind.CSharp;
OutputInteropIncludes = true;
CommentPrefix = "///";
Encoding = Encoding.ASCII;
@ -188,7 +187,13 @@ namespace CppSharp @@ -188,7 +187,13 @@ namespace CppSharp
public string IncludePrefix;
public Func<TranslationUnit, string> GenerateName;
public string CommentPrefix;
/// <summary>
/// Set this option to the kind of comments that you want generated
/// in the source code. This overrides the default kind set by the
/// target generator.
/// </summary>
public CommentKind? CommentKind;
public Encoding Encoding { get; set; }

Loading…
Cancel
Save