Skip to content

Commit 1978e3b

Browse files
committed
Code cleanup
1 parent 6fe8c66 commit 1978e3b

File tree

7 files changed

+77
-61
lines changed

7 files changed

+77
-61
lines changed

src/AST/Comment.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public interface ICommentVisitor<out T>
109109
T VisitTParamCommand(TParamCommandComment comment);
110110
T VisitVerbatimBlock(VerbatimBlockComment comment);
111111
T VisitVerbatimLine(VerbatimLineComment comment);
112-
T VisitParagraphCommand(ParagraphComment comment);
112+
T VisitParagraph(ParagraphComment comment);
113113
T VisitFull(FullComment comment);
114114
T VisitHTMLStartTag(HTMLStartTagComment comment);
115115
T VisitHTMLEndTag(HTMLEndTagComment comment);
@@ -129,20 +129,13 @@ public abstract class Comment
129129

130130
public static string GetMultiLineCommentPrologue(CommentKind kind)
131131
{
132-
switch (kind)
132+
return kind switch
133133
{
134-
case CommentKind.BCPL:
135-
case CommentKind.BCPLExcl:
136-
return "//";
137-
case CommentKind.C:
138-
case CommentKind.JavaDoc:
139-
case CommentKind.Qt:
140-
return " *";
141-
case CommentKind.BCPLSlash:
142-
return "///";
143-
default:
144-
throw new ArgumentOutOfRangeException();
145-
}
134+
CommentKind.BCPL or CommentKind.BCPLExcl => "//",
135+
CommentKind.C or CommentKind.JavaDoc or CommentKind.Qt => " *",
136+
CommentKind.BCPLSlash => "///",
137+
_ => throw new ArgumentOutOfRangeException()
138+
};
146139
}
147140

148141
public static string GetLineCommentPrologue(CommentKind kind)
@@ -375,7 +368,7 @@ public ParagraphComment()
375368

376369
public override void Visit<T>(ICommentVisitor<T> visitor)
377370
{
378-
visitor.VisitParagraphCommand(this);
371+
visitor.VisitParagraph(this);
379372
}
380373
}
381374

src/CppParser/Comments.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ void Parser::HandleComments(const clang::Decl* D, Declaration* Decl)
263263
{
264264
using namespace clang;
265265

266-
const clang::RawComment* RC = 0;
267-
if (!(RC = c->getASTContext().getRawCommentForAnyRedecl(D)))
266+
const clang::RawComment* RC = c->getASTContext().getRawCommentForAnyRedecl(D);
267+
if (!RC)
268268
return;
269269

270270
auto RawComment = WalkRawComment(RC);

src/Generator/Generators/CSharp/CSharpCommentPrinter.cs

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class CSharpCommentPrinter
1111
{
1212
public static void Print(this ITextGenerator textGenerator, Comment comment, CommentKind kind)
1313
{
14-
var sections = new List<Section> { new Section(CommentElement.Summary) };
14+
var sections = new List<Section>();
1515
GetCommentSections(comment, sections);
1616
foreach (var section in sections)
1717
TrimSection(section);
@@ -23,14 +23,17 @@ private static void GetCommentSections(this Comment comment, List<Section> secti
2323
switch (comment.Kind)
2424
{
2525
case DocumentationCommentKind.FullComment:
26-
var fullComment = (FullComment)comment;
27-
foreach (var block in fullComment.Blocks)
26+
{
27+
foreach (var block in ((FullComment)comment).Blocks)
2828
block.GetCommentSections(sections);
2929
break;
30+
}
3031
case DocumentationCommentKind.BlockCommandComment:
32+
{
3133
var blockCommandComment = (BlockCommandComment)comment;
3234
if (blockCommandComment.ParagraphComment == null)
3335
break;
36+
3437
switch (blockCommandComment.CommandKind)
3538
{
3639
case CommentCommandKind.Brief:
@@ -49,38 +52,38 @@ private static void GetCommentSections(this Comment comment, List<Section> secti
4952
if (inlineContentComment.HasTrailingNewline)
5053
lastBlockSection.NewLine();
5154
}
55+
5256
break;
5357
default:
5458
sections.Add(new Section(CommentElement.Remarks));
5559
blockCommandComment.ParagraphComment.GetCommentSections(sections);
5660
break;
5761
}
5862

59-
6063
break;
64+
}
6165
case DocumentationCommentKind.ParamCommandComment:
66+
{
6267
var paramCommandComment = (ParamCommandComment)comment;
6368
var param = new Section(CommentElement.Param);
6469
sections.Add(param);
6570
if (paramCommandComment.Arguments.Count > 0)
66-
param.Attributes.Add(
67-
string.Format("name=\"{0}\"", paramCommandComment.Arguments[0].Text));
71+
param.Attributes.Add($"name=\"{paramCommandComment.Arguments[0].Text}\"");
72+
6873
if (paramCommandComment.ParagraphComment != null)
74+
{
6975
foreach (var inlineContentComment in paramCommandComment.ParagraphComment.Content)
7076
{
7177
inlineContentComment.GetCommentSections(sections);
7278
if (inlineContentComment.HasTrailingNewline)
7379
sections.Last().NewLine();
7480
}
81+
}
82+
7583
if (!string.IsNullOrEmpty(sections.Last().CurrentLine.ToString()))
7684
sections.Add(new Section(CommentElement.Remarks));
7785
break;
78-
case DocumentationCommentKind.TParamCommandComment:
79-
break;
80-
case DocumentationCommentKind.VerbatimBlockComment:
81-
break;
82-
case DocumentationCommentKind.VerbatimLineComment:
83-
break;
86+
}
8487
case DocumentationCommentKind.ParagraphComment:
8588
var summaryParagraph = sections.Count == 1;
8689
var paragraphComment = (ParagraphComment)comment;
@@ -101,22 +104,21 @@ private static void GetCommentSections(this Comment comment, List<Section> secti
101104
sections.RemoveRange(1, sections.Count - 1);
102105
sections.Add(new Section(CommentElement.Remarks));
103106
}
107+
104108
break;
105-
case DocumentationCommentKind.HTMLTagComment:
106-
break;
107-
case DocumentationCommentKind.HTMLStartTagComment:
108-
break;
109-
case DocumentationCommentKind.HTMLEndTagComment:
110-
break;
109+
}
111110
case DocumentationCommentKind.TextComment:
112-
var lastTextsection = sections.Last();
113-
lastTextsection.CurrentLine.Append(GetText(comment,
114-
lastTextsection.Type == CommentElement.Returns ||
115-
lastTextsection.Type == CommentElement.Param).Trim());
116-
break;
117-
case DocumentationCommentKind.InlineContentComment:
111+
{
112+
var lastTextSection = sections.Last();
113+
lastTextSection.CurrentLine
114+
.Append(
115+
GetText(comment, lastTextSection.Type is CommentElement.Returns or CommentElement.Param)
116+
.TrimStart()
117+
);
118118
break;
119+
}
119120
case DocumentationCommentKind.InlineCommandComment:
121+
{
120122
var lastInlineSection = sections.Last();
121123
var inlineCommand = (InlineCommandComment)comment;
122124

@@ -125,9 +127,27 @@ private static void GetCommentSections(this Comment comment, List<Section> secti
125127
var argText = $" <c>{inlineCommand.Arguments[0].Text}</c> ";
126128
lastInlineSection.CurrentLine.Append(argText);
127129
}
130+
131+
break;
132+
}
133+
case DocumentationCommentKind.HTMLStartTagComment:
134+
{
135+
break;
136+
}
137+
case DocumentationCommentKind.HTMLEndTagComment:
138+
{
128139
break;
140+
}
141+
case DocumentationCommentKind.HTMLTagComment:
142+
case DocumentationCommentKind.TParamCommandComment:
143+
case DocumentationCommentKind.VerbatimBlockComment:
144+
case DocumentationCommentKind.VerbatimLineComment:
145+
case DocumentationCommentKind.InlineContentComment:
129146
case DocumentationCommentKind.VerbatimBlockLineComment:
147+
case DocumentationCommentKind.BlockContentComment:
130148
break;
149+
default:
150+
throw new ArgumentOutOfRangeException();
131151
}
132152
}
133153

@@ -139,10 +159,10 @@ private static string GetText(Comment comment, bool trim = false)
139159
text = text.Trim();
140160

141161
if (Helpers.RegexTag.IsMatch(text))
142-
return String.Empty;
162+
return string.Empty;
143163

144164
return HtmlEncoder.HtmlEncode(
145-
text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text.Substring(1) : text);
165+
text.Length > 1 && text[0] == ' ' && text[1] != ' ' ? text[1..] : text);
146166
}
147167

148168
private static void TrimSection(Section section)
@@ -166,22 +186,25 @@ private static void TrimSection(Section section)
166186

167187
private static void FormatComment(ITextGenerator textGenerator, List<Section> sections, CommentKind kind)
168188
{
169-
var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
170-
171189
sections.Sort((x, y) => x.Type.CompareTo(y.Type));
190+
172191
var remarks = sections.Where(s => s.Type == CommentElement.Remarks).ToList();
173-
if (remarks.Any())
192+
if (remarks.Count != 0)
174193
remarks.First().GetLines().AddRange(remarks.Skip(1).SelectMany(s => s.GetLines()));
194+
175195
if (remarks.Count > 1)
176196
sections.RemoveRange(sections.IndexOf(remarks.First()) + 1, remarks.Count - 1);
177197

198+
var commentPrefix = Comment.GetMultiLineCommentPrologue(kind);
178199
foreach (var section in sections.Where(s => s.HasLines))
179200
{
180201
var lines = section.GetLines();
181202
var tag = section.Type.ToString().ToLowerInvariant();
203+
182204
var attributes = string.Empty;
183-
if (section.Attributes.Any())
205+
if (section.Attributes.Count != 0)
184206
attributes = ' ' + string.Join(" ", section.Attributes);
207+
185208
textGenerator.Write($"{commentPrefix} <{tag}{attributes}>");
186209
if (lines.Count == 1)
187210
{
@@ -205,13 +228,13 @@ public Section(CommentElement type)
205228
Type = type;
206229
}
207230

208-
public StringBuilder CurrentLine { get; set; } = new StringBuilder();
231+
public StringBuilder CurrentLine { get; set; } = new();
209232

210233
public CommentElement Type { get; set; }
211234

212-
public List<string> Attributes { get; } = new List<string>();
235+
public List<string> Attributes { get; init; } = new();
213236

214-
private List<string> lines { get; } = new List<string>();
237+
private List<string> lines { get; } = new();
215238

216239
public bool HasLines => lines.Any();
217240

src/Generator/Generators/CodeGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,8 +1333,8 @@ public virtual bool VisitCoyieldExpr(CoyieldExpr stmt)
13331333

13341334
public static class Helpers
13351335
{
1336-
public static Regex RegexTag = new Regex(@"^(<|</)[a-zA-Z][\w\-]*?>?$");
1337-
public static Regex RegexCommentCommandLeftover = new Regex(@"^\S*");
1336+
public static Regex RegexTag = new(@"^(<|</)[a-zA-Z][\w\-]*?>?$");
1337+
public static Regex RegexCommentCommandLeftover = new(@"^\S*");
13381338
public static readonly string InternalStruct = Generator.GeneratedIdentifier("Internal");
13391339
public static readonly string InstanceField = Generator.GeneratedIdentifier("instance");
13401340
public static readonly string InstanceIdentifier = Generator.GeneratedIdentifier("Instance");

src/Generator/Passes/CleanCommentsPass.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ public override bool VisitDeclaration(Declaration decl)
2121
if (!base.VisitDeclaration(decl))
2222
return false;
2323

24-
if (decl.Comment != null)
25-
{
26-
var fullComment = decl.Comment.FullComment;
27-
VisitFull(fullComment);
24+
if (decl.Comment == null)
25+
return true;
2826

29-
}
27+
var fullComment = decl.Comment.FullComment;
28+
VisitFull(fullComment);
3029
return true;
3130
}
3231

@@ -37,14 +36,15 @@ public bool VisitFull(FullComment comment)
3736

3837
return true;
3938
}
39+
4040
#region Comments Visit
4141
public bool VisitHTMLEndTag(HTMLEndTagComment comment) => true;
4242

4343
public bool VisitHTMLStartTag(HTMLStartTagComment comment) => true;
4444

4545
public bool VisitInlineCommand(InlineCommandComment comment) => true;
4646

47-
public bool VisitParagraphCommand(ParagraphComment comment)
47+
public bool VisitParagraph(ParagraphComment comment)
4848
{
4949
for (int i = 0; i < comment.Content.Count; i++)
5050
{

src/Generator/Passes/FixParameterUsageFromComments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public bool VisitBlockCommand(BlockCommandComment comment)
7171
return true;
7272
}
7373

74-
public bool VisitParagraphCommand(ParagraphComment comment)
74+
public bool VisitParagraph(ParagraphComment comment)
7575
{
7676
return true;
7777
}

src/Generator/Utils/HtmlEncoder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static string HtmlEncode(string s)
5353
return null;
5454

5555
if (s.Length == 0)
56-
return String.Empty;
56+
return string.Empty;
5757

5858
bool needEncode = s.Any(c => c == '&' || c == '"' || c == '<' || c == '>' || c > 159);
5959

@@ -93,7 +93,7 @@ public static string HtmlEncode(string s)
9393
{
9494
output.Append("&#");
9595
output.Append(((int)ch).ToString(CultureInfo.InvariantCulture));
96-
output.Append(";");
96+
output.Append(';');
9797
}
9898
else
9999
output.Append(ch);

0 commit comments

Comments
 (0)