From 1978e3b394d1a3fae0a38beb3f249aabff54cc96 Mon Sep 17 00:00:00 2001
From: duckdoom5 <duckdoom5@users.noreply.github.com>
Date: Mon, 10 Mar 2025 16:13:02 +0100
Subject: [PATCH] Code cleanup

---
 src/AST/Comment.cs                            | 23 ++---
 src/CppParser/Comments.cpp                    |  4 +-
 .../Generators/CSharp/CSharpCommentPrinter.cs | 89 ++++++++++++-------
 src/Generator/Generators/CodeGenerator.cs     |  4 +-
 src/Generator/Passes/CleanCommentsPass.cs     | 12 +--
 .../Passes/FixParameterUsageFromComments.cs   |  2 +-
 src/Generator/Utils/HtmlEncoder.cs            |  4 +-
 7 files changed, 77 insertions(+), 61 deletions(-)

diff --git a/src/AST/Comment.cs b/src/AST/Comment.cs
index e45db3ee..02b2dd4b 100644
--- a/src/AST/Comment.cs
+++ b/src/AST/Comment.cs
@@ -109,7 +109,7 @@ namespace CppSharp.AST
         T VisitTParamCommand(TParamCommandComment comment);
         T VisitVerbatimBlock(VerbatimBlockComment comment);
         T VisitVerbatimLine(VerbatimLineComment comment);
-        T VisitParagraphCommand(ParagraphComment comment);
+        T VisitParagraph(ParagraphComment comment);
         T VisitFull(FullComment comment);
         T VisitHTMLStartTag(HTMLStartTagComment comment);
         T VisitHTMLEndTag(HTMLEndTagComment comment);
@@ -129,20 +129,13 @@ namespace CppSharp.AST
 
         public static string GetMultiLineCommentPrologue(CommentKind kind)
         {
-            switch (kind)
+            return kind switch
             {
-                case CommentKind.BCPL:
-                case CommentKind.BCPLExcl:
-                    return "//";
-                case CommentKind.C:
-                case CommentKind.JavaDoc:
-                case CommentKind.Qt:
-                    return " *";
-                case CommentKind.BCPLSlash:
-                    return "///";
-                default:
-                    throw new ArgumentOutOfRangeException();
-            }
+                CommentKind.BCPL or CommentKind.BCPLExcl => "//",
+                CommentKind.C or CommentKind.JavaDoc or CommentKind.Qt => " *",
+                CommentKind.BCPLSlash => "///",
+                _ => throw new ArgumentOutOfRangeException()
+            };
         }
 
         public static string GetLineCommentPrologue(CommentKind kind)
@@ -375,7 +368,7 @@ namespace CppSharp.AST
 
         public override void Visit<T>(ICommentVisitor<T> visitor)
         {
-            visitor.VisitParagraphCommand(this);
+            visitor.VisitParagraph(this);
         }
     }
 
diff --git a/src/CppParser/Comments.cpp b/src/CppParser/Comments.cpp
index 71e6a55e..799a0985 100644
--- a/src/CppParser/Comments.cpp
+++ b/src/CppParser/Comments.cpp
@@ -263,8 +263,8 @@ void Parser::HandleComments(const clang::Decl* D, Declaration* Decl)
 {
     using namespace clang;
 
-    const clang::RawComment* RC = 0;
-    if (!(RC = c->getASTContext().getRawCommentForAnyRedecl(D)))
+    const clang::RawComment* RC = c->getASTContext().getRawCommentForAnyRedecl(D);
+    if (!RC)
         return;
 
     auto RawComment = WalkRawComment(RC);
diff --git a/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs b/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
index 6f7fe7e2..85ec9342 100644
--- a/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
+++ b/src/Generator/Generators/CSharp/CSharpCommentPrinter.cs
@@ -11,7 +11,7 @@ namespace CppSharp.Generators.CSharp
     {
         public static void Print(this ITextGenerator textGenerator, Comment comment, CommentKind kind)
         {
-            var sections = new List<Section> { new Section(CommentElement.Summary) };
+            var sections = new List<Section>();
             GetCommentSections(comment, sections);
             foreach (var section in sections)
                 TrimSection(section);
@@ -23,14 +23,17 @@ namespace CppSharp.Generators.CSharp
             switch (comment.Kind)
             {
                 case DocumentationCommentKind.FullComment:
-                    var fullComment = (FullComment)comment;
-                    foreach (var block in fullComment.Blocks)
+                {
+                    foreach (var block in ((FullComment)comment).Blocks)
                         block.GetCommentSections(sections);
                     break;
+                }
                 case DocumentationCommentKind.BlockCommandComment:
+                {
                     var blockCommandComment = (BlockCommandComment)comment;
                     if (blockCommandComment.ParagraphComment == null)
                         break;
+
                     switch (blockCommandComment.CommandKind)
                     {
                         case CommentCommandKind.Brief:
@@ -49,6 +52,7 @@ namespace CppSharp.Generators.CSharp
                                 if (inlineContentComment.HasTrailingNewline)
                                     lastBlockSection.NewLine();
                             }
+
                             break;
                         default:
                             sections.Add(new Section(CommentElement.Remarks));
@@ -56,31 +60,30 @@ namespace CppSharp.Generators.CSharp
                             break;
                     }
 
-
                     break;
+                }
                 case DocumentationCommentKind.ParamCommandComment:
+                {
                     var paramCommandComment = (ParamCommandComment)comment;
                     var param = new Section(CommentElement.Param);
                     sections.Add(param);
                     if (paramCommandComment.Arguments.Count > 0)
-                        param.Attributes.Add(
-                            string.Format("name=\"{0}\"", paramCommandComment.Arguments[0].Text));
+                        param.Attributes.Add($"name=\"{paramCommandComment.Arguments[0].Text}\"");
+
                     if (paramCommandComment.ParagraphComment != null)
+                    {
                         foreach (var inlineContentComment in paramCommandComment.ParagraphComment.Content)
                         {
                             inlineContentComment.GetCommentSections(sections);
                             if (inlineContentComment.HasTrailingNewline)
                                 sections.Last().NewLine();
                         }
+                    }
+
                     if (!string.IsNullOrEmpty(sections.Last().CurrentLine.ToString()))
                         sections.Add(new Section(CommentElement.Remarks));
                     break;
-                case DocumentationCommentKind.TParamCommandComment:
-                    break;
-                case DocumentationCommentKind.VerbatimBlockComment:
-                    break;
-                case DocumentationCommentKind.VerbatimLineComment:
-                    break;
+                }
                 case DocumentationCommentKind.ParagraphComment:
                     var summaryParagraph = sections.Count == 1;
                     var paragraphComment = (ParagraphComment)comment;
@@ -101,22 +104,21 @@ namespace CppSharp.Generators.CSharp
                         sections.RemoveRange(1, sections.Count - 1);
                         sections.Add(new Section(CommentElement.Remarks));
                     }
+
                     break;
-                case DocumentationCommentKind.HTMLTagComment:
-                    break;
-                case DocumentationCommentKind.HTMLStartTagComment:
-                    break;
-                case DocumentationCommentKind.HTMLEndTagComment:
-                    break;
+                }
                 case DocumentationCommentKind.TextComment:
-                    var lastTextsection = sections.Last();
-                    lastTextsection.CurrentLine.Append(GetText(comment,
-                        lastTextsection.Type == CommentElement.Returns ||
-                        lastTextsection.Type == CommentElement.Param).Trim());
-                    break;
-                case DocumentationCommentKind.InlineContentComment:
+                {
+                    var lastTextSection = sections.Last();
+                    lastTextSection.CurrentLine
+                        .Append(
+                            GetText(comment, lastTextSection.Type is CommentElement.Returns or CommentElement.Param)
+                                .TrimStart()
+                        );
                     break;
+                }
                 case DocumentationCommentKind.InlineCommandComment:
+                {
                     var lastInlineSection = sections.Last();
                     var inlineCommand = (InlineCommandComment)comment;
 
@@ -125,9 +127,27 @@ namespace CppSharp.Generators.CSharp
                         var argText = $" <c>{inlineCommand.Arguments[0].Text}</c> ";
                         lastInlineSection.CurrentLine.Append(argText);
                     }
+
+                    break;
+                }
+                case DocumentationCommentKind.HTMLStartTagComment:
+                {
+                    break;
+                }
+                case DocumentationCommentKind.HTMLEndTagComment:
+                {
                     break;
+                }
+                case DocumentationCommentKind.HTMLTagComment:
+                case DocumentationCommentKind.TParamCommandComment:
+                case DocumentationCommentKind.VerbatimBlockComment:
+                case DocumentationCommentKind.VerbatimLineComment:
+                case DocumentationCommentKind.InlineContentComment:
                 case DocumentationCommentKind.VerbatimBlockLineComment:
+                case DocumentationCommentKind.BlockContentComment:
                     break;
+                default:
+                    throw new ArgumentOutOfRangeException();
             }
         }
 
@@ -139,10 +159,10 @@ namespace CppSharp.Generators.CSharp
                 text = text.Trim();
 
             if (Helpers.RegexTag.IsMatch(text))
-                return String.Empty;
+                return string.Empty;
 
             return HtmlEncoder.HtmlEncode(
-                text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text);
+                text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text[1..] : text);
         }
 
         private static void TrimSection(Section section)
@@ -166,22 +186,25 @@ namespace CppSharp.Generators.CSharp
 
         private static void FormatComment(ITextGenerator textGenerator, List<Section> sections, CommentKind kind)
         {
-            var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
-
             sections.Sort((x, y) => x.Type.CompareTo(y.Type));
+
             var remarks = sections.Where(s => s.Type == CommentElement.Remarks).ToList();
-            if (remarks.Any())
+            if (remarks.Count != 0)
                 remarks.First().GetLines().AddRange(remarks.Skip(1).SelectMany(s => s.GetLines()));
+
             if (remarks.Count > 1)
                 sections.RemoveRange(sections.IndexOf(remarks.First()) + 1, remarks.Count - 1);
 
+            var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
             foreach (var section in sections.Where(s => s.HasLines))
             {
                 var lines = section.GetLines();
                 var tag = section.Type.ToString().ToLowerInvariant();
+
                 var attributes = string.Empty;
-                if (section.Attributes.Any())
+                if (section.Attributes.Count != 0)
                     attributes = ' ' + string.Join(" ", section.Attributes);
+
                 textGenerator.Write($"{commentPrefix} <{tag}{attributes}>");
                 if (lines.Count == 1)
                 {
@@ -205,13 +228,13 @@ namespace CppSharp.Generators.CSharp
                 Type = type;
             }
 
-            public StringBuilder CurrentLine { get; set; } = new StringBuilder();
+            public StringBuilder CurrentLine { get; set; } = new();
 
             public CommentElement Type { get; set; }
 
-            public List<string> Attributes { get; } = new List<string>();
+            public List<string> Attributes { get; init; } = new();
 
-            private List<string> lines { get; } = new List<string>();
+            private List<string> lines { get; } = new();
 
             public bool HasLines => lines.Any();
 
diff --git a/src/Generator/Generators/CodeGenerator.cs b/src/Generator/Generators/CodeGenerator.cs
index 793b9873..baede2ae 100644
--- a/src/Generator/Generators/CodeGenerator.cs
+++ b/src/Generator/Generators/CodeGenerator.cs
@@ -1333,8 +1333,8 @@ namespace CppSharp.Generators
 
     public static class Helpers
     {
-        public static Regex RegexTag = new Regex(@"^(<|</)[a-zA-Z][\w\-]*?>?$");
-        public static Regex RegexCommentCommandLeftover = new Regex(@"^\S*");
+        public static Regex RegexTag = new(@"^(<|</)[a-zA-Z][\w\-]*?>?$");
+        public static Regex RegexCommentCommandLeftover = new(@"^\S*");
         public static readonly string InternalStruct = Generator.GeneratedIdentifier("Internal");
         public static readonly string InstanceField = Generator.GeneratedIdentifier("instance");
         public static readonly string InstanceIdentifier = Generator.GeneratedIdentifier("Instance");
diff --git a/src/Generator/Passes/CleanCommentsPass.cs b/src/Generator/Passes/CleanCommentsPass.cs
index 79039549..dffa273c 100644
--- a/src/Generator/Passes/CleanCommentsPass.cs
+++ b/src/Generator/Passes/CleanCommentsPass.cs
@@ -21,12 +21,11 @@ namespace CppSharp.Passes
             if (!base.VisitDeclaration(decl))
                 return false;
 
-            if (decl.Comment != null)
-            {
-                var fullComment = decl.Comment.FullComment;
-                VisitFull(fullComment);
+            if (decl.Comment == null)
+                return true;
 
-            }
+            var fullComment = decl.Comment.FullComment;
+            VisitFull(fullComment);
             return true;
         }
 
@@ -37,6 +36,7 @@ namespace CppSharp.Passes
 
             return true;
         }
+
         #region Comments Visit
         public bool VisitHTMLEndTag(HTMLEndTagComment comment) => true;
 
@@ -44,7 +44,7 @@ namespace CppSharp.Passes
 
         public bool VisitInlineCommand(InlineCommandComment comment) => true;
 
-        public bool VisitParagraphCommand(ParagraphComment comment)
+        public bool VisitParagraph(ParagraphComment comment)
         {
             for (int i = 0; i < comment.Content.Count; i++)
             {
diff --git a/src/Generator/Passes/FixParameterUsageFromComments.cs b/src/Generator/Passes/FixParameterUsageFromComments.cs
index afe36fa1..a68167e6 100644
--- a/src/Generator/Passes/FixParameterUsageFromComments.cs
+++ b/src/Generator/Passes/FixParameterUsageFromComments.cs
@@ -71,7 +71,7 @@ namespace CppSharp.Passes
             return true;
         }
 
-        public bool VisitParagraphCommand(ParagraphComment comment)
+        public bool VisitParagraph(ParagraphComment comment)
         {
             return true;
         }
diff --git a/src/Generator/Utils/HtmlEncoder.cs b/src/Generator/Utils/HtmlEncoder.cs
index 4c332101..4f557ca8 100644
--- a/src/Generator/Utils/HtmlEncoder.cs
+++ b/src/Generator/Utils/HtmlEncoder.cs
@@ -53,7 +53,7 @@ namespace System.Web.Util
                 return null;
 
             if (s.Length == 0)
-                return String.Empty;
+                return string.Empty;
 
             bool needEncode = s.Any(c => c == '&' || c == '"' || c == '<' || c == '>' || c > 159);
 
@@ -93,7 +93,7 @@ namespace System.Web.Util
                         {
                             output.Append("&#");
                             output.Append(((int)ch).ToString(CultureInfo.InvariantCulture));
-                            output.Append(";");
+                            output.Append(';');
                         }
                         else
                             output.Append(ch);