@@ -345,8 +345,19 @@ public struct MarkupFormatter: MarkupWalker {
345
345
/// The length of the last line.
346
346
var lastLineLength = 0
347
347
348
- /// The current line number.
348
+ /// The line number of the most recently printed content.
349
+ ///
350
+ /// This is updated in `addressPendingNewlines(for:)` when line breaks are printed.
349
351
var lineNumber = 0
352
+
353
+ /// The "effective" line number, taking into account any queued newlines which have not
354
+ /// been printed.
355
+ ///
356
+ /// This property allows line number comparisons between different formatter states to
357
+ /// accommodate queued soft line breaks as well as printed content.
358
+ var effectiveLineNumber : Int {
359
+ lineNumber + queuedNewlines
360
+ }
350
361
}
351
362
352
363
/// The state of the formatter.
@@ -769,7 +780,7 @@ public struct MarkupFormatter: MarkupWalker {
769
780
// If printing with automatic wrapping still put us over the line,
770
781
// prefer to print it on the next line to give as much opportunity
771
782
// to keep the contents on one line.
772
- if inlineCode. indexInParent > 0 && ( isOverPreferredLineLimit || state. lineNumber > savedState. lineNumber ) {
783
+ if inlineCode. indexInParent > 0 && ( isOverPreferredLineLimit || state. effectiveLineNumber > savedState. effectiveLineNumber ) {
773
784
restoreState ( to: savedState)
774
785
queueNewline ( )
775
786
softWrapPrint ( " ` \( inlineCode. code) ` " , for: inlineCode)
@@ -800,7 +811,7 @@ public struct MarkupFormatter: MarkupWalker {
800
811
// Image elements' source URLs can't be split. If wrapping the alt text
801
812
// of an image still put us over the line, prefer to print it on the
802
813
// next line to give as much opportunity to keep the alt text contents on one line.
803
- if image. indexInParent > 0 && ( isOverPreferredLineLimit || state. lineNumber > savedState. lineNumber ) {
814
+ if image. indexInParent > 0 && ( isOverPreferredLineLimit || state. effectiveLineNumber > savedState. effectiveLineNumber ) {
804
815
restoreState ( to: savedState)
805
816
queueNewline ( )
806
817
printImage ( )
@@ -837,7 +848,7 @@ public struct MarkupFormatter: MarkupWalker {
837
848
// Link elements' destination URLs can't be split. If wrapping the link text
838
849
// of a link still put us over the line, prefer to print it on the
839
850
// next line to give as much opportunity to keep the link text contents on one line.
840
- if link. indexInParent > 0 && ( isOverPreferredLineLimit || state. lineNumber > savedState. lineNumber ) {
851
+ if link. indexInParent > 0 && ( isOverPreferredLineLimit || state. effectiveLineNumber > savedState. effectiveLineNumber ) {
841
852
restoreState ( to: savedState)
842
853
queueNewline ( )
843
854
printRegularLink ( )
@@ -1133,7 +1144,7 @@ public struct MarkupFormatter: MarkupWalker {
1133
1144
public mutating func visitInlineAttributes( _ attributes: InlineAttributes ) {
1134
1145
let savedState = state
1135
1146
func printInlineAttributes( ) {
1136
- print ( " [ " , for: attributes)
1147
+ print ( " ^ [" , for: attributes)
1137
1148
descendInto ( attributes)
1138
1149
print ( " ]( " , for: attributes)
1139
1150
print ( attributes. attributes, for: attributes)
@@ -1147,7 +1158,7 @@ public struct MarkupFormatter: MarkupWalker {
1147
1158
// gets into the realm of JSON formatting which might be out of scope of
1148
1159
// this formatter. Therefore if exceeded, prefer to print it on the next
1149
1160
// line to give as much opportunity to keep the attributes on one line.
1150
- if attributes. indexInParent > 0 && ( isOverPreferredLineLimit || state. lineNumber > savedState. lineNumber ) {
1161
+ if attributes. indexInParent > 0 && ( isOverPreferredLineLimit || state. effectiveLineNumber > savedState. effectiveLineNumber ) {
1151
1162
restoreState ( to: savedState)
1152
1163
queueNewline ( )
1153
1164
printInlineAttributes ( )
0 commit comments