@@ -3194,7 +3194,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3194
3194
true ,
3195
3195
[
3196
3196
. space( size: config. spacesBeforeEndOfLineComments, flexible: true ) ,
3197
- . comment( Comment ( kind: . line, text: text) , wasEndOfLine: true ) ,
3197
+ . comment( Comment ( kind: . line, leadingIndent : nil , text: text) , wasEndOfLine: true ) ,
3198
3198
// There must be a break with a soft newline after the comment, but it's impossible to
3199
3199
// know which kind of break must be used. Adding this newline is deferred until the
3200
3200
// comment is added to the token stream.
@@ -3205,7 +3205,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3205
3205
false ,
3206
3206
[
3207
3207
. space( size: 1 , flexible: true ) ,
3208
- . comment( Comment ( kind: . block, text: text) , wasEndOfLine: false ) ,
3208
+ . comment( Comment ( kind: . block, leadingIndent : nil , text: text) , wasEndOfLine: false ) ,
3209
3209
// We place a size-0 break after the comment to allow a discretionary newline after
3210
3210
// the comment if the user places one here but the comment is otherwise adjacent to a
3211
3211
// text token.
@@ -3294,24 +3294,29 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3294
3294
// example, even if discretionary newlines are discarded). This is the case when the preceding
3295
3295
// trivia was a line comment or garbage text.
3296
3296
var requiresNextNewline = false
3297
+ // Tracking whether or not the last piece was leading indentation. A newline is considered
3298
+ // a 0-space indentation; used for nesting/un-nesting block comments during formatting.
3299
+ var leadingIndent : Indent ? = nil
3297
3300
3298
3301
for (index, piece) in trivia. enumerated ( ) {
3299
3302
if let cutoff = cutoffIndex, index == cutoff { break }
3303
+
3300
3304
switch piece {
3301
3305
case . lineComment( let text) :
3302
3306
if index > 0 || isStartOfFile {
3303
3307
generateEnableFormattingIfNecessary ( position ..< position + piece. sourceLength)
3304
- appendToken ( . comment( Comment ( kind: . line, text: text) , wasEndOfLine: false ) )
3308
+ appendToken ( . comment( Comment ( kind: . line, leadingIndent : leadingIndent , text: text) , wasEndOfLine: false ) )
3305
3309
generateDisableFormattingIfNecessary ( position + piece. sourceLength)
3306
3310
appendNewlines ( . soft)
3307
3311
isStartOfFile = false
3308
3312
}
3309
3313
requiresNextNewline = true
3314
+ leadingIndent = nil
3310
3315
3311
3316
case . blockComment( let text) :
3312
3317
if index > 0 || isStartOfFile {
3313
3318
generateEnableFormattingIfNecessary ( position ..< position + piece. sourceLength)
3314
- appendToken ( . comment( Comment ( kind: . block, text: text) , wasEndOfLine: false ) )
3319
+ appendToken ( . comment( Comment ( kind: . block, leadingIndent : leadingIndent , text: text) , wasEndOfLine: false ) )
3315
3320
generateDisableFormattingIfNecessary ( position + piece. sourceLength)
3316
3321
// There is always a break after the comment to allow a discretionary newline after it.
3317
3322
var breakSize = 0
@@ -3325,24 +3330,28 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3325
3330
isStartOfFile = false
3326
3331
}
3327
3332
requiresNextNewline = false
3333
+ leadingIndent = nil
3328
3334
3329
3335
case . docLineComment( let text) :
3330
3336
generateEnableFormattingIfNecessary ( position ..< position + piece. sourceLength)
3331
- appendToken ( . comment( Comment ( kind: . docLine, text: text) , wasEndOfLine: false ) )
3337
+ appendToken ( . comment( Comment ( kind: . docLine, leadingIndent : leadingIndent , text: text) , wasEndOfLine: false ) )
3332
3338
generateDisableFormattingIfNecessary ( position + piece. sourceLength)
3333
3339
appendNewlines ( . soft)
3334
3340
isStartOfFile = false
3335
3341
requiresNextNewline = true
3342
+ leadingIndent = nil
3336
3343
3337
3344
case . docBlockComment( let text) :
3338
3345
generateEnableFormattingIfNecessary ( position ..< position + piece. sourceLength)
3339
- appendToken ( . comment( Comment ( kind: . docBlock, text: text) , wasEndOfLine: false ) )
3346
+ appendToken ( . comment( Comment ( kind: . docBlock, leadingIndent : leadingIndent , text: text) , wasEndOfLine: false ) )
3340
3347
generateDisableFormattingIfNecessary ( position + piece. sourceLength)
3341
3348
appendNewlines ( . soft)
3342
3349
isStartOfFile = false
3343
3350
requiresNextNewline = false
3351
+ leadingIndent = nil
3344
3352
3345
3353
case . newlines( let count) , . carriageReturns( let count) , . carriageReturnLineFeeds( let count) :
3354
+ leadingIndent = . spaces( 0 )
3346
3355
guard !isStartOfFile else { break }
3347
3356
3348
3357
if requiresNextNewline ||
@@ -3372,9 +3381,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
3372
3381
let isBOM = text == " \u{feff} "
3373
3382
requiresNextNewline = !isBOM
3374
3383
isStartOfFile = isStartOfFile && isBOM
3384
+ leadingIndent = nil
3375
3385
3376
- default :
3377
- break
3386
+ case . backslashes, . formfeeds, . pounds, . verticalTabs:
3387
+ leadingIndent = nil
3388
+
3389
+ case . spaces( let n) :
3390
+ guard leadingIndent == . spaces( 0 ) else { break }
3391
+ leadingIndent = . spaces( n)
3392
+ case . tabs( let n) :
3393
+ guard leadingIndent == . spaces( 0 ) else { break }
3394
+ leadingIndent = . tabs( n)
3378
3395
}
3379
3396
position += piece. sourceLength
3380
3397
}
0 commit comments