@@ -33,6 +33,21 @@ fileprivate extension Markup {
33
33
}
34
34
return nil
35
35
}
36
+
37
+ /// Previous sibling of this element in its parent, or `nil` if it's the first child.
38
+ var previousSibling : Markup ? {
39
+ guard let parent, indexInParent > 0 else {
40
+ return nil
41
+ }
42
+
43
+ return parent. child ( at: indexInParent - 1 )
44
+ }
45
+
46
+ /// Whether this element is a Doxygen command.
47
+ var isDoxygenCommand : Bool {
48
+ return self is DoxygenDiscussion || self is DoxygenNote || self is DoxygenAbstract
49
+ || self is DoxygenParameter || self is DoxygenReturns
50
+ }
36
51
}
37
52
38
53
fileprivate extension String {
@@ -239,6 +254,15 @@ public struct MarkupFormatter: MarkupWalker {
239
254
case at = " @ "
240
255
}
241
256
257
+ /// The spacing to use when formatting adjacent Doxygen commands.
258
+ public enum AdjacentDoxygenCommandsSpacing : String , CaseIterable {
259
+ /// Separate adjacent Doxygen commands with a single newline.
260
+ case singleNewline = " single-newline "
261
+
262
+ /// Keep a blank line between adjacent Doxygen commands creating separate paragraphs.
263
+ case separateParagraphs = " separate-paragraphs "
264
+ }
265
+
242
266
// MARK: Option Properties
243
267
244
268
var orderedListNumerals : OrderedListNumerals
@@ -253,6 +277,7 @@ public struct MarkupFormatter: MarkupWalker {
253
277
var preferredLineLimit : PreferredLineLimit ?
254
278
var customLinePrefix : String
255
279
var doxygenCommandPrefix : DoxygenCommandPrefix
280
+ var adjacentDoxygenCommandsSpacing : AdjacentDoxygenCommandsSpacing
256
281
257
282
/**
258
283
Create a set of formatting options to use when printing an element.
@@ -270,6 +295,7 @@ public struct MarkupFormatter: MarkupWalker {
270
295
- preferredLineLimit: The preferred maximum line length and method for splitting ``Text`` elements in an attempt to maintain that line length.
271
296
- customLinePrefix: An addition prefix to print at the start of each line, useful for adding documentation comment markers.
272
297
- doxygenCommandPrefix: The command command prefix, which defaults to ``DoxygenCommandPrefix/backslash``.
298
+ - adjacentDoxygenCommandsSpacing: The spacing to use when formatting adjacent Doxygen commands.
273
299
*/
274
300
public init ( unorderedListMarker: UnorderedListMarker = . dash,
275
301
orderedListNumerals: OrderedListNumerals = . allSame( 1 ) ,
@@ -282,7 +308,8 @@ public struct MarkupFormatter: MarkupWalker {
282
308
preferredHeadingStyle: PreferredHeadingStyle = . atx,
283
309
preferredLineLimit: PreferredLineLimit ? = nil ,
284
310
customLinePrefix: String = " " ,
285
- doxygenCommandPrefix: DoxygenCommandPrefix = . backslash) {
311
+ doxygenCommandPrefix: DoxygenCommandPrefix = . backslash,
312
+ adjacentDoxygenCommandsSpacing: AdjacentDoxygenCommandsSpacing = . singleNewline) {
286
313
self . unorderedListMarker = unorderedListMarker
287
314
self . orderedListNumerals = orderedListNumerals
288
315
self . useCodeFence = useCodeFence
@@ -297,6 +324,7 @@ public struct MarkupFormatter: MarkupWalker {
297
324
self . thematicBreakLength = max ( 3 , thematicBreakLength)
298
325
self . customLinePrefix = customLinePrefix
299
326
self . doxygenCommandPrefix = doxygenCommandPrefix
327
+ self . adjacentDoxygenCommandsSpacing = adjacentDoxygenCommandsSpacing
300
328
}
301
329
302
330
/// The default set of formatting options.
@@ -1173,48 +1201,47 @@ public struct MarkupFormatter: MarkupWalker {
1173
1201
print ( formattingOptions. doxygenCommandPrefix. rawValue + name + " " , for: element)
1174
1202
}
1175
1203
1176
- public mutating func visitDoxygenDiscussion( _ doxygenDiscussion: DoxygenDiscussion ) {
1177
- if doxygenDiscussion. indexInParent > 0 {
1204
+ private mutating func ensureDoxygenCommandPrecedingNewline( for element: Markup ) {
1205
+ guard let previousSibling = element. previousSibling else {
1206
+ return
1207
+ }
1208
+
1209
+ guard formattingOptions. adjacentDoxygenCommandsSpacing == . singleNewline else {
1178
1210
ensurePrecedingNewlineCount ( atLeast: 2 )
1211
+ return
1179
1212
}
1180
1213
1214
+ let newlineCount = previousSibling. isDoxygenCommand ? 1 : 2
1215
+ ensurePrecedingNewlineCount ( atLeast: newlineCount)
1216
+ }
1217
+
1218
+ public mutating func visitDoxygenDiscussion( _ doxygenDiscussion: DoxygenDiscussion ) {
1219
+ ensureDoxygenCommandPrecedingNewline ( for: doxygenDiscussion)
1181
1220
printDoxygenStart ( " discussion " , for: doxygenDiscussion)
1182
1221
descendInto ( doxygenDiscussion)
1183
1222
}
1184
1223
1185
1224
public mutating func visitDoxygenAbstract( _ doxygenAbstract: DoxygenAbstract ) {
1186
- if doxygenAbstract. indexInParent > 0 {
1187
- ensurePrecedingNewlineCount ( atLeast: 2 )
1188
- }
1189
-
1225
+ ensureDoxygenCommandPrecedingNewline ( for: doxygenAbstract)
1190
1226
printDoxygenStart ( " abstract " , for: doxygenAbstract)
1191
1227
descendInto ( doxygenAbstract)
1192
1228
}
1193
1229
1194
1230
public mutating func visitDoxygenNote( _ doxygenNote: DoxygenNote ) {
1195
- if doxygenNote. indexInParent > 0 {
1196
- ensurePrecedingNewlineCount ( atLeast: 2 )
1197
- }
1198
-
1231
+ ensureDoxygenCommandPrecedingNewline ( for: doxygenNote)
1199
1232
printDoxygenStart ( " note " , for: doxygenNote)
1200
1233
descendInto ( doxygenNote)
1201
1234
}
1202
1235
1203
1236
public mutating func visitDoxygenParameter( _ doxygenParam: DoxygenParameter ) {
1204
- if doxygenParam. indexInParent > 0 {
1205
- ensurePrecedingNewlineCount ( atLeast: 2 )
1206
- }
1207
-
1237
+ ensureDoxygenCommandPrecedingNewline ( for: doxygenParam)
1208
1238
printDoxygenStart ( " param " , for: doxygenParam)
1209
1239
print ( " \( doxygenParam. name) " , for: doxygenParam)
1210
1240
descendInto ( doxygenParam)
1211
1241
}
1212
1242
1213
1243
public mutating func visitDoxygenReturns( _ doxygenReturns: DoxygenReturns ) {
1214
- if doxygenReturns. indexInParent > 0 {
1215
- ensurePrecedingNewlineCount ( atLeast: 2 )
1216
- }
1217
-
1244
+ ensureDoxygenCommandPrecedingNewline ( for: doxygenReturns)
1218
1245
// FIXME: store the actual command name used in the original markup
1219
1246
printDoxygenStart ( " returns " , for: doxygenReturns)
1220
1247
descendInto ( doxygenReturns)
0 commit comments