@@ -3194,7 +3194,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
31943194 true ,
31953195 [
31963196 . 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 ) ,
31983198 // There must be a break with a soft newline after the comment, but it's impossible to
31993199 // know which kind of break must be used. Adding this newline is deferred until the
32003200 // comment is added to the token stream.
@@ -3205,7 +3205,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
32053205 false ,
32063206 [
32073207 . 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 ) ,
32093209 // We place a size-0 break after the comment to allow a discretionary newline after
32103210 // the comment if the user places one here but the comment is otherwise adjacent to a
32113211 // text token.
@@ -3294,24 +3294,29 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
32943294 // example, even if discretionary newlines are discarded). This is the case when the preceding
32953295 // trivia was a line comment or garbage text.
32963296 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
32973300
32983301 for (index, piece) in trivia. enumerated ( ) {
32993302 if let cutoff = cutoffIndex, index == cutoff { break }
3303+
33003304 switch piece {
33013305 case . lineComment( let text) :
33023306 if index > 0 || isStartOfFile {
33033307 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 ) )
33053309 generateDisableFormattingIfNecessary ( position + piece. sourceLength)
33063310 appendNewlines ( . soft)
33073311 isStartOfFile = false
33083312 }
33093313 requiresNextNewline = true
3314+ leadingIndent = nil
33103315
33113316 case . blockComment( let text) :
33123317 if index > 0 || isStartOfFile {
33133318 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 ) )
33153320 generateDisableFormattingIfNecessary ( position + piece. sourceLength)
33163321 // There is always a break after the comment to allow a discretionary newline after it.
33173322 var breakSize = 0
@@ -3325,24 +3330,28 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
33253330 isStartOfFile = false
33263331 }
33273332 requiresNextNewline = false
3333+ leadingIndent = nil
33283334
33293335 case . docLineComment( let text) :
33303336 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 ) )
33323338 generateDisableFormattingIfNecessary ( position + piece. sourceLength)
33333339 appendNewlines ( . soft)
33343340 isStartOfFile = false
33353341 requiresNextNewline = true
3342+ leadingIndent = nil
33363343
33373344 case . docBlockComment( let text) :
33383345 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 ) )
33403347 generateDisableFormattingIfNecessary ( position + piece. sourceLength)
33413348 appendNewlines ( . soft)
33423349 isStartOfFile = false
33433350 requiresNextNewline = false
3351+ leadingIndent = nil
33443352
33453353 case . newlines( let count) , . carriageReturns( let count) , . carriageReturnLineFeeds( let count) :
3354+ leadingIndent = . spaces( 0 )
33463355 guard !isStartOfFile else { break }
33473356
33483357 if requiresNextNewline ||
@@ -3372,9 +3381,17 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
33723381 let isBOM = text == " \u{feff} "
33733382 requiresNextNewline = !isBOM
33743383 isStartOfFile = isStartOfFile && isBOM
3384+ leadingIndent = nil
33753385
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)
33783395 }
33793396 position += piece. sourceLength
33803397 }
0 commit comments