Browse Source

Fix the regressed indentation of printed comments

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1237/head
Dimitar Dobrev 6 years ago
parent
commit
c6ffa9ddf2
  1. 26
      src/Generator.Tests/AST/TestAST.cs
  2. 6
      src/Generator.Tests/Passes/TestPasses.cs
  3. 25
      src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
  4. 2
      src/Generator/Generators/CodeGenerator.cs

26
src/Generator.Tests/AST/TestAST.cs

@ -383,15 +383,18 @@ namespace CppSharp.Generator.Tests.AST @@ -383,15 +383,18 @@ namespace CppSharp.Generator.Tests.AST
public void TestComments()
{
var @class = AstContext.FindCompleteClass("TestComments");
var commentClass = @class.Comment.FullComment.CommentToString(CommentKind.BCPLSlash);
var textGenerator = new TextGenerator();
textGenerator.Print(@class.Comment.FullComment, CommentKind.BCPLSlash);
Assert.AreEqual(@"/// <summary>
/// <para>Hash set/map base class.</para>
/// <para>Note that to prevent extra memory use due to vtable pointer, %HashBase intentionally does not declare a virtual destructor</para>
/// <para>and therefore %HashBase pointers should never be used.</para>
/// </summary>".Replace("\r", string.Empty), commentClass.Replace("\r", string.Empty));
/// </summary>
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
var method = @class.Methods.First(m => m.Name == "GetIOHandlerControlSequence");
var commentMethod = method.Comment.FullComment.CommentToString(CommentKind.BCPL);
textGenerator.StringBuilder.Clear();
textGenerator.Print(method.Comment.FullComment, CommentKind.BCPL);
Assert.AreEqual(@"// <summary>
// <para>Get the string that needs to be written to the debugger stdin file</para>
// <para>handle when a control character is typed.</para>
@ -407,10 +410,12 @@ namespace CppSharp.Generator.Tests.AST @@ -407,10 +410,12 @@ namespace CppSharp.Generator.Tests.AST
// <para>to have them do what normally would happen when using a real</para>
// <para>terminal, so this function allows GUI programs to emulate this</para>
// <para>functionality.</para>
// </remarks>".Replace("\r", string.Empty), commentMethod.Replace("\r", string.Empty));
// </remarks>
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
var methodTestDoxygen = @class.Methods.First(m => m.Name == "SBAttachInfo");
var commentMethodDoxygen = methodTestDoxygen.Comment.FullComment.CommentToString(CommentKind.BCPLSlash);
textGenerator.StringBuilder.Clear();
textGenerator.Print(methodTestDoxygen.Comment.FullComment, CommentKind.BCPLSlash);
Assert.AreEqual(@"/// <summary>Attach to a process by name.</summary>
/// <param name=""path"">A full or partial name for the process to attach to.</param>
/// <param name=""wait_for"">
@ -420,11 +425,13 @@ namespace CppSharp.Generator.Tests.AST @@ -420,11 +425,13 @@ namespace CppSharp.Generator.Tests.AST
/// <remarks>
/// <para>This function implies that a future call to SBTarget::Attach(...)</para>
/// <para>will be synchronous.</para>
/// </remarks>".Replace("\r", string.Empty), commentMethodDoxygen.Replace("\r", string.Empty));
/// </remarks>
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
var methodDoxygenCustomTags = @class.Methods.First(m => m.Name == "glfwDestroyWindow");
new CleanCommentsPass { }.VisitFull(methodDoxygenCustomTags.Comment.FullComment);
var commentMethodDoxygenCustomTag = methodDoxygenCustomTags.Comment.FullComment.CommentToString(CommentKind.BCPLSlash);
new CleanCommentsPass().VisitFull(methodDoxygenCustomTags.Comment.FullComment);
textGenerator.StringBuilder.Clear();
textGenerator.Print(methodDoxygenCustomTags.Comment.FullComment, CommentKind.BCPLSlash);
Assert.AreEqual(@"/// <summary>Destroys the specified window and its context.</summary>
/// <param name=""window"">The window to destroy.</param>
/// <remarks>
@ -437,7 +444,8 @@ namespace CppSharp.Generator.Tests.AST @@ -437,7 +444,8 @@ namespace CppSharp.Generator.Tests.AST
/// <para>This function must not be called from a callback.</para>
/// <para>This function must only be called from the main thread.</para>
/// <para>Added in version 3.0. Replaces `glfwCloseWindow`.</para>
/// </remarks>".Replace("\r", string.Empty), commentMethodDoxygenCustomTag.Replace("\r", string.Empty));
/// </remarks>
".Replace("\r", string.Empty), textGenerator.StringBuilder.Replace("\r", string.Empty).ToString());
}
[Test]

6
src/Generator.Tests/Passes/TestPasses.cs

@ -71,9 +71,11 @@ namespace CppSharp.Generator.Tests.Passes @@ -71,9 +71,11 @@ namespace CppSharp.Generator.Tests.Passes
passBuilder.RunPasses(pass => pass.VisitDeclaration(c));
var para = (ParagraphComment) c.Comment.FullComment.Blocks[0];
var s = para.CommentToString(CommentKind.BCPLSlash);
var textGenerator = new TextGenerator();
textGenerator.Print(para, CommentKind.BCPLSlash);
Assert.That(s, Is.EqualTo("/// <summary>A simple test.</summary>"));
Assert.That(textGenerator.StringBuilder.ToString().Trim(),
Is.EqualTo("/// <summary>A simple test.</summary>"));
}
[Test]

25
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, CommentKind kind)
public static void Print(this ITextGenerator textGenerator, 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, kind);
FormatComment(textGenerator, sections, kind);
}
private static void GetCommentSections(this Comment comment, List<Section> sections)
@ -163,10 +163,9 @@ namespace CppSharp.Generators.CSharp @@ -163,10 +163,9 @@ namespace CppSharp.Generators.CSharp
}
}
private static string FormatComment(List<Section> sections, CommentKind kind)
private static void FormatComment(ITextGenerator textGenerator, List<Section> sections, CommentKind kind)
{
var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
var commentBuilder = new StringBuilder();
sections.Sort((x, y) => x.Type.CompareTo(y.Type));
var remarks = sections.Where(s => s.Type == CommentElement.Remarks).ToList();
@ -182,26 +181,20 @@ namespace CppSharp.Generators.CSharp @@ -182,26 +181,20 @@ namespace CppSharp.Generators.CSharp
var attributes = string.Empty;
if (section.Attributes.Any())
attributes = ' ' + string.Join(" ", section.Attributes);
commentBuilder.Append($"{commentPrefix} <{tag}{attributes}>");
textGenerator.Write($"{commentPrefix} <{tag}{attributes}>");
if (lines.Count == 1)
{
commentBuilder.Append(lines[0]);
textGenerator.Write(lines[0]);
}
else
{
commentBuilder.AppendLine();
textGenerator.NewLine();
foreach (var line in lines)
commentBuilder.AppendLine($"{commentPrefix} <para>{line}</para>");
commentBuilder.Append($"{commentPrefix} ");
textGenerator.WriteLine($"{commentPrefix} <para>{line}</para>");
textGenerator.Write($"{commentPrefix} ");
}
commentBuilder.AppendLine($"</{tag}>");
textGenerator.WriteLine($"</{tag}>");
}
if (commentBuilder.Length > 0)
{
var newLineLength = Environment.NewLine.Length;
commentBuilder.Remove(commentBuilder.Length - newLineLength, newLineLength);
}
return commentBuilder.ToString();
}
private class Section

2
src/Generator/Generators/CodeGenerator.cs

@ -132,7 +132,7 @@ namespace CppSharp.Generators @@ -132,7 +132,7 @@ namespace CppSharp.Generators
if (comment.FullComment != null)
{
PushBlock(BlockKind.BlockComment);
WriteLine(comment.FullComment.CommentToString(DocumentationCommentKind));
ActiveBlock.Text.Print(comment.FullComment, DocumentationCommentKind);
PopBlock();
return;
}

Loading…
Cancel
Save