@@ -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
0 commit comments