Skip to content

Commit 4cbd46e

Browse files
support doxygen param/returns in MarkupFormatter
1 parent 9b6d64e commit 4cbd46e

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

Sources/Markdown/Block Nodes/Block Container Blocks/Doxygen Commands/DoxygenParameter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct DoxygenParameter: BlockContainer {
2727

2828
init(_ raw: RawMarkup) throws {
2929
guard case .doxygenParam = raw.data else {
30-
throw RawMarkup.Error.concreteConversionError(from: raw, to: BlockDirective.self)
30+
throw RawMarkup.Error.concreteConversionError(from: raw, to: DoxygenParameter.self)
3131
}
3232
let absoluteRaw = AbsoluteRawMarkup(markup: raw, metadata: MarkupMetadata(id: .newRoot(), indexInParent: 0))
3333
self.init(_MarkupData(absoluteRaw))

Sources/Markdown/Block Nodes/Block Container Blocks/Doxygen Commands/DoxygenReturns.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public struct DoxygenReturns: BlockContainer {
2323
public var _data: _MarkupData
2424

2525
init(_ raw: RawMarkup) throws {
26-
guard case .doxygenParam = raw.data else {
27-
throw RawMarkup.Error.concreteConversionError(from: raw, to: BlockDirective.self)
26+
guard case .doxygenReturns = raw.data else {
27+
throw RawMarkup.Error.concreteConversionError(from: raw, to: DoxygenReturns.self)
2828
}
2929
let absoluteRaw = AbsoluteRawMarkup(markup: raw, metadata: MarkupMetadata(id: .newRoot(), indexInParent: 0))
3030
self.init(_MarkupData(absoluteRaw))

Sources/Markdown/Walker/Walkers/MarkupFormatter.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -230,6 +230,15 @@ public struct MarkupFormatter: MarkupWalker {
230230
}
231231
}
232232

233+
/// The character to use when formatting Doxygen commands.
234+
public enum DoxygenCommandPrefix: String, CaseIterable {
235+
/// Precede Doxygen commands with a backslash (`\`).
236+
case backslash = "\\"
237+
238+
/// Precede Doxygen commands with an at-sign (`@`).
239+
case at = "@"
240+
}
241+
233242
// MARK: Option Properties
234243

235244
var orderedListNumerals: OrderedListNumerals
@@ -243,6 +252,7 @@ public struct MarkupFormatter: MarkupWalker {
243252
var preferredHeadingStyle: PreferredHeadingStyle
244253
var preferredLineLimit: PreferredLineLimit?
245254
var customLinePrefix: String
255+
var doxygenCommandPrefix: DoxygenCommandPrefix
246256

247257
/**
248258
Create a set of formatting options to use when printing an element.
@@ -270,7 +280,8 @@ public struct MarkupFormatter: MarkupWalker {
270280
condenseAutolinks: Bool = true,
271281
preferredHeadingStyle: PreferredHeadingStyle = .atx,
272282
preferredLineLimit: PreferredLineLimit? = nil,
273-
customLinePrefix: String = "") {
283+
customLinePrefix: String = "",
284+
doxygenCommandPrefix: DoxygenCommandPrefix = .backslash) {
274285
self.unorderedListMarker = unorderedListMarker
275286
self.orderedListNumerals = orderedListNumerals
276287
self.useCodeFence = useCodeFence
@@ -284,6 +295,7 @@ public struct MarkupFormatter: MarkupWalker {
284295
// three characters long.
285296
self.thematicBreakLength = max(3, thematicBreakLength)
286297
self.customLinePrefix = customLinePrefix
298+
self.doxygenCommandPrefix = doxygenCommandPrefix
287299
}
288300

289301
/// The default set of formatting options.
@@ -1144,4 +1156,16 @@ public struct MarkupFormatter: MarkupWalker {
11441156
printInlineAttributes()
11451157
}
11461158
}
1159+
1160+
public mutating func visitDoxygenParameter(_ doxygenParam: DoxygenParameter) -> () {
1161+
print("\(formattingOptions.doxygenCommandPrefix.rawValue)param", for: doxygenParam)
1162+
print(" \(doxygenParam.name) ", for: doxygenParam)
1163+
descendInto(doxygenParam)
1164+
}
1165+
1166+
public mutating func visitDoxygenReturns(_ doxygenReturns: DoxygenReturns) -> () {
1167+
// FIXME: store the actual command name used in the original markup
1168+
print("\(formattingOptions.doxygenCommandPrefix.rawValue)returns ", for: doxygenReturns)
1169+
descendInto(doxygenReturns)
1170+
}
11471171
}

Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2021 Apple Inc. and the Swift project authors
4+
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See https://swift.org/LICENSE.txt for license information
@@ -288,6 +288,44 @@ class MarkupFormatterSingleElementTests: XCTestCase {
288288
let printed = SymbolLink(destination: "foo()").format()
289289
XCTAssertEqual(expected, printed)
290290
}
291+
292+
func testPrintDoxygenParameter() {
293+
let expected = #"\param thing The thing."#
294+
let printed = DoxygenParameter(name: "thing", children: Paragraph(Text("The thing."))).format()
295+
XCTAssertEqual(expected, printed)
296+
}
297+
298+
func testPrintDoxygenParameterMultiline() {
299+
let expected = #"""
300+
\param thing The thing.
301+
This is an extended discussion.
302+
"""#
303+
let printed = DoxygenParameter(name: "thing", children: Paragraph(
304+
Text("The thing."),
305+
SoftBreak(),
306+
Text("This is an extended discussion.")
307+
)).format()
308+
XCTAssertEqual(expected, printed)
309+
}
310+
311+
func testPrintDoxygenReturns() {
312+
let expected = #"\returns Another thing."#
313+
let printed = DoxygenReturns(children: Paragraph(Text("Another thing."))).format()
314+
XCTAssertEqual(expected, printed)
315+
}
316+
317+
func testPrintDoxygenReturnsMultiline() {
318+
let expected = #"""
319+
\returns Another thing.
320+
This is an extended discussion.
321+
"""#
322+
let printed = DoxygenReturns(children: Paragraph(
323+
Text("Another thing."),
324+
SoftBreak(),
325+
Text("This is an extended discussion.")
326+
)).format()
327+
XCTAssertEqual(expected, printed)
328+
}
291329
}
292330

293331
/// Tests that formatting options work correctly.
@@ -469,6 +507,23 @@ class MarkupFormatterOptionsTests: XCTestCase {
469507
XCTAssertEqual(incrementing, printed)
470508
}
471509
}
510+
511+
func testDoxygenCommandPrefix() {
512+
let backslash = #"\param thing The thing."#
513+
let at = "@param thing The thing."
514+
515+
do {
516+
let document = Document(parsing: backslash, options: [.parseMinimalDoxygen, .parseBlockDirectives])
517+
let printed = document.format(options: .init(doxygenCommandPrefix: .at))
518+
XCTAssertEqual(at, printed)
519+
}
520+
521+
do {
522+
let document = Document(parsing: at, options: [.parseMinimalDoxygen, .parseBlockDirectives])
523+
let printed = document.format(options: .init(doxygenCommandPrefix: .backslash))
524+
XCTAssertEqual(backslash, printed)
525+
}
526+
}
472527
}
473528

474529
/// Tests that an printed and reparsed element has the same structure as
@@ -646,7 +701,8 @@ class MarkupFormatterSimpleRoundTripTests: XCTestCase {
646701
let document = try Document(parsing: readMeURL)
647702
// try document.format().write(toFile: "/tmp/test.md", atomically: true, encoding: .utf8)
648703
checkRoundTrip(for: document)
649-
}}
704+
}
705+
}
650706

651707
/**
652708
Test enforcement of a preferred maximum line length.

0 commit comments

Comments
 (0)