1
+ using CSharpier . DocTypes ;
2
+ using System ;
3
+ using System . Collections ;
4
+ using System . Runtime . CompilerServices ;
1
5
using System . Text ;
6
+ using System . Xml . Linq ;
2
7
3
8
namespace CSharpier . DocPrinter ;
4
9
@@ -67,6 +72,8 @@ private void EnsureOutputEndsWithSingleNewLine()
67
72
this . Output . Append ( this . EndOfLine ) ;
68
73
}
69
74
75
+
76
+
70
77
private void ProcessNextCommand ( )
71
78
{
72
79
var ( indent , mode , doc ) = this . RemainingCommands . Pop ( ) ;
@@ -186,6 +193,47 @@ private void ProcessNextCommand()
186
193
}
187
194
}
188
195
196
+ private List < string > BreakCommentLine ( string comment )
197
+ {
198
+ List < string > result = new List < string > ( ) ;
199
+ if ( comment . Length > this . PrinterOptions . Width )
200
+ {
201
+ string [ ] comments = comment . Split ( ' ' ) ;
202
+ StringBuilder singleLine = new StringBuilder ( ) ;
203
+ bool firstLine = true ;
204
+ foreach ( var word in comments )
205
+ {
206
+ if ( singleLine . Length + word . Length >= this . PrinterOptions . Width )
207
+ {
208
+ result . Add ( singleLine . ToString ( ) ) ;
209
+ singleLine . Clear ( ) ;
210
+ if ( firstLine )
211
+ {
212
+ firstLine = false ;
213
+ }
214
+ }
215
+
216
+ if ( singleLine . Length > 0 )
217
+ {
218
+ singleLine . Append ( ' ' ) ;
219
+ }
220
+
221
+ singleLine . Append ( word ) ;
222
+ }
223
+
224
+ if ( singleLine . Length > 0 )
225
+ {
226
+ result . Add ( singleLine . ToString ( ) ) ;
227
+ }
228
+ }
229
+ else
230
+ {
231
+ result . Add ( comment ) ;
232
+ }
233
+
234
+ return result ;
235
+ }
236
+
189
237
private void AppendComment ( LeadingComment leadingComment , Indent indent )
190
238
{
191
239
int CalculateIndentLength ( string line ) =>
@@ -206,42 +254,68 @@ int CalculateIndentLength(string line) =>
206
254
207
255
while ( line != null )
208
256
{
209
- if ( leadingComment . Type == CommentType . SingleLine )
210
- {
211
- this . Output . Append ( indent . Value ) ;
212
- }
213
- else
257
+ string entireLine = line ;
258
+ List < string > lines = BreakCommentLine ( line . Trim ( ) ) ;
259
+
260
+ var nextLine = stringReader . ReadLine ( ) ;
261
+
262
+ int total = lines . Count ;
263
+ int lineNumber = 0 ;
264
+ foreach ( var singleLine in lines )
214
265
{
215
- var spacesToAppend = CalculateIndentLength ( line ) + numberOfSpacesToAddOrRemove ;
216
- if ( this . PrinterOptions . UseTabs )
266
+ if ( leadingComment . Type == CommentType . SingleLine )
217
267
{
218
- var indentLength = CalculateIndentLength ( indent . Value ) ;
219
- if ( spacesToAppend >= indentLength )
268
+ this . Output . Append ( indent . Value ) ;
269
+ }
270
+ else
271
+ {
272
+ var spacesToAppend = CalculateIndentLength ( singleLine ) + numberOfSpacesToAddOrRemove ;
273
+ if ( this . PrinterOptions . UseTabs )
220
274
{
221
- this . Output . Append ( indent . Value ) ;
222
- spacesToAppend -= indentLength ;
223
- }
275
+ var indentLength = CalculateIndentLength ( indent . Value ) ;
276
+ if ( spacesToAppend >= indentLength )
277
+ {
278
+ this . Output . Append ( indent . Value ) ;
279
+ spacesToAppend -= indentLength ;
280
+ }
224
281
225
- while ( spacesToAppend > 0 && spacesToAppend >= this . PrinterOptions . IndentSize )
282
+ while ( spacesToAppend > 0 && spacesToAppend >= this . PrinterOptions . IndentSize )
283
+ {
284
+ this . Output . Append ( '\t ' ) ;
285
+ spacesToAppend -= this . PrinterOptions . IndentSize ;
286
+ }
287
+ }
288
+ if ( spacesToAppend > 0 )
226
289
{
227
- this . Output . Append ( '\t ' ) ;
228
- spacesToAppend -= this . PrinterOptions . IndentSize ;
290
+ this . Output . Append ( ' ' , spacesToAppend ) ;
229
291
}
230
292
}
231
- if ( spacesToAppend > 0 )
293
+
294
+ if ( leadingComment . Type == CommentType . SingleLine && lineNumber > 0 )
295
+ {
296
+ this . Output . Append ( "// " ) ;
297
+ }
298
+
299
+ this . Output . Append ( singleLine . Trim ( ) ) ;
300
+
301
+ // Printer will generate a new line for the next line after the comment.
302
+ if ( nextLine != null || lineNumber < total - 1 )
232
303
{
233
- this . Output . Append ( ' ' , spacesToAppend ) ;
304
+ this . Output . Append ( this . EndOfLine ) ;
234
305
}
306
+
307
+ ++ lineNumber ;
235
308
}
236
309
237
- this . Output . Append ( line . Trim ( ) ) ;
238
- line = stringReader . ReadLine ( ) ;
239
- if ( line == null )
310
+
311
+ if ( nextLine == null )
240
312
{
241
313
return ;
242
314
}
243
-
244
- this . Output . Append ( this . EndOfLine ) ;
315
+ else
316
+ {
317
+ line = nextLine ;
318
+ }
245
319
}
246
320
}
247
321
0 commit comments