@@ -498,10 +498,18 @@ open class BasicFormat: SyntaxRewriter {
498498 }
499499 }
500500
501- let isEmptyLine = token. leadingTrivia. isEmpty && leadingTriviaIsFollowedByNewline
502- if leadingTrivia. indentation ( isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [ ] && !isEmptyLine {
501+ let isStringSegment : Bool
502+ if case . stringSegment = token. tokenKind {
503+ isStringSegment = true
504+ } else {
505+ isStringSegment = false
506+ }
507+ if leadingTrivia. indentation ( isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [ ] && !isStringSegment {
503508 // If the token starts on a new line and does not have indentation, this
504- // is the last non-indented token. Store its indentation level
509+ // is the last non-indented token. Store its indentation level.
510+ // But never consider string segments as anchor points since you can’t
511+ // indent individual lines of a multi-line string literals without breaking
512+ // their integrity.
505513 anchorPoints [ token] = currentIndentationLevel
506514 }
507515
@@ -529,14 +537,17 @@ open class BasicFormat: SyntaxRewriter {
529537 var leadingTriviaIndentation = self . currentIndentationLevel
530538 var trailingTriviaIndentation = self . currentIndentationLevel
531539
532- // If the trivia contain user-defined indentation, find their anchor point
540+ // If the trivia contains user-defined indentation, find their anchor point
533541 // and indent the token relative to that anchor point.
534- if leadingTrivia. containsIndentation ( isOnNewline: previousTokenWillEndWithNewline) ,
542+ // Always indent string segments relative to their anchor point because
543+ // their indentation has structural meaning and we just want to maintain
544+ // what the user wrote.
545+ if leadingTrivia. containsIndentation ( isOnNewline: previousTokenWillEndWithNewline) || isStringSegment,
535546 let anchorPointIndentation = self . anchorPointIndentation ( for: token)
536547 {
537548 leadingTriviaIndentation = anchorPointIndentation
538549 }
539- if combinedTrailingTrivia. containsIndentation ( isOnNewline: previousTokenWillEndWithNewline) ,
550+ if combinedTrailingTrivia. containsIndentation ( isOnNewline: previousTokenWillEndWithNewline) || isStringSegment ,
540551 let anchorPointIndentation = self . anchorPointIndentation ( for: token)
541552 {
542553 trailingTriviaIndentation = anchorPointIndentation
0 commit comments