Skip to content

Commit

Permalink
Add support for formatting the new Doxygen types using MarkupFormatter (
Browse files Browse the repository at this point in the history
#163)

* Add the new commands to the docs

* Remove duplicate code from DoxygenParameter/DoxygenReturns formatters

* Add support for formatting DoxygenDiscussion/DoxygenNote

* Add tests for DoxygenDiscussion/DoxygenNote printing

* Add a test to verify printing both Doxygen prefixes works

* Update copyright years
  • Loading branch information
j-f1 authored Feb 6, 2024
1 parent cfe8c84 commit 75bd319
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Sources/Markdown/Markdown.docc/Markdown/DoxygenCommands.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Doxygen commands are not parsed within code blocks or block directive content.

### Commands

- ``DoxygenDiscussion``
- ``DoxygenNote``
- ``DoxygenParam``
- ``DoxygenReturns``

Expand Down
26 changes: 20 additions & 6 deletions Sources/Markdown/Walker/Walkers/MarkupFormatter.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -1165,15 +1165,29 @@ public struct MarkupFormatter: MarkupWalker {
}
}

public mutating func visitDoxygenParameter(_ doxygenParam: DoxygenParameter) -> () {
print("\(formattingOptions.doxygenCommandPrefix.rawValue)param", for: doxygenParam)
print(" \(doxygenParam.name) ", for: doxygenParam)
private mutating func printDoxygenStart(_ name: String, for element: Markup) {
print(formattingOptions.doxygenCommandPrefix.rawValue + name + " ", for: element)
}

public mutating func visitDoxygenDiscussion(_ doxygenDiscussion: DoxygenDiscussion) {
printDoxygenStart("discussion", for: doxygenDiscussion)
descendInto(doxygenDiscussion)
}

public mutating func visitDoxygenNote(_ doxygenNote: DoxygenNote) {
printDoxygenStart("note", for: doxygenNote)
descendInto(doxygenNote)
}

public mutating func visitDoxygenParameter(_ doxygenParam: DoxygenParameter) {
printDoxygenStart("param", for: doxygenParam)
print("\(doxygenParam.name) ", for: doxygenParam)
descendInto(doxygenParam)
}

public mutating func visitDoxygenReturns(_ doxygenReturns: DoxygenReturns) -> () {
public mutating func visitDoxygenReturns(_ doxygenReturns: DoxygenReturns) {
// FIXME: store the actual command name used in the original markup
print("\(formattingOptions.doxygenCommandPrefix.rawValue)returns ", for: doxygenReturns)
printDoxygenStart("returns", for: doxygenReturns)
descendInto(doxygenReturns)
}
}
52 changes: 51 additions & 1 deletion Tests/MarkdownTests/Visitors/MarkupFormatterTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Copyright (c) 2021-2024 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -289,6 +289,56 @@ class MarkupFormatterSingleElementTests: XCTestCase {
XCTAssertEqual(expected, printed)
}

func testPrintDoxygenPrefix() {
let expectedSlash = #"\discussion Discussion"#
let printedSlash = DoxygenDiscussion(children: Paragraph(Text("Discussion")))
.format(options: .init(doxygenCommandPrefix: .backslash))
XCTAssertEqual(expectedSlash, printedSlash)

let expectedAt = "@discussion Discussion"
let printedAt = DoxygenDiscussion(children: Paragraph(Text("Discussion")))
.format(options: .init(doxygenCommandPrefix: .at))
XCTAssertEqual(expectedAt, printedAt)
}

func testPrintDoxygenDiscussion() {
let expected = #"\discussion Another thing."#
let printed = DoxygenDiscussion(children: Paragraph(Text("Another thing."))).format()
XCTAssertEqual(expected, printed)
}

func testPrintDoxygenDiscussionMultiline() {
let expected = #"""
\discussion Another thing.
This is an extended discussion.
"""#
let printed = DoxygenDiscussion(children: Paragraph(
Text("Another thing."),
SoftBreak(),
Text("This is an extended discussion.")
)).format()
XCTAssertEqual(expected, printed)
}

func testPrintDoxygenNote() {
let expected = #"\note Another thing."#
let printed = DoxygenNote(children: Paragraph(Text("Another thing."))).format()
XCTAssertEqual(expected, printed)
}

func testPrintDoxygenNoteMultiline() {
let expected = #"""
\note Another thing.
This is an extended discussion.
"""#
let printed = DoxygenNote(children: Paragraph(
Text("Another thing."),
SoftBreak(),
Text("This is an extended discussion.")
)).format()
XCTAssertEqual(expected, printed)
}

func testPrintDoxygenParameter() {
let expected = #"\param thing The thing."#
let printed = DoxygenParameter(name: "thing", children: Paragraph(Text("The thing."))).format()
Expand Down

0 comments on commit 75bd319

Please sign in to comment.