From 306a44187f167703c4cf6ff29ebf59956b7a7bf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20St=C3=BChrk?= Date: Sat, 25 Apr 2020 16:58:21 +0200 Subject: [PATCH 1/6] Simply containers to use only a single container protocol. --- .../Supporting Types/Children.swift | 327 +++--------------- 1 file changed, 51 insertions(+), 276 deletions(-) diff --git a/Sources/CommonMark/Supporting Types/Children.swift b/Sources/CommonMark/Supporting Types/Children.swift index 78d0eee..60f2492 100644 --- a/Sources/CommonMark/Supporting Types/Children.swift +++ b/Sources/CommonMark/Supporting Types/Children.swift @@ -1,26 +1,22 @@ import cmark -struct Children: Sequence { - var cmark_node: OpaquePointer +private struct Children: Sequence { + let cmark_node: OpaquePointer init(of node: Node) { cmark_node = node.cmark_node } - init(of document: Document) { - cmark_node = document.cmark_node - } - - func makeIterator() -> AnyIterator { + func makeIterator() -> AnyIterator { var iterator = CMarkNodeChildIterator(cmark_node) return AnyIterator { guard let child = iterator.next() else { return nil } - return Node.create(for: child) + return Node.create(for: child) as? ChildNode } } } -struct CMarkNodeChildIterator: IteratorProtocol { +private struct CMarkNodeChildIterator: IteratorProtocol { var current: OpaquePointer! init(_ node: OpaquePointer!) { @@ -52,16 +48,15 @@ fileprivate func add(_ child: Child, with operation: () -> Int32) - // MARK: - -public protocol ContainerOfBlocks: Node {} - -extension Document: ContainerOfBlocks {} -extension BlockQuote: ContainerOfBlocks {} +public protocol Container: Node { + associatedtype ChildNode +} -extension ContainerOfBlocks { +extension Container where ChildNode: Node { /// The block's children. - public var children: [Block & Node] { + public var children: [ChildNode] { get { - return Children(of: self).compactMap { $0 as? Block & Node } + return Array(Children(of: self)) } set { @@ -83,7 +78,7 @@ extension ContainerOfBlocks { - Returns: `true` if successful, otherwise `false`. */ @discardableResult - public func prepend(child: Block & Node) -> Bool { + public func prepend(child: ChildNode) -> Bool { return add(child) { cmark_node_prepend_child(cmark_node, child.cmark_node) } } @@ -95,7 +90,7 @@ extension ContainerOfBlocks { - Returns: `true` if successful, otherwise `false`. */ @discardableResult - public func append(child: Block & Node) -> Bool { + public func append(child: ChildNode) -> Bool { return add(child) { cmark_node_append_child(cmark_node, child.cmark_node) } } @@ -108,7 +103,7 @@ extension ContainerOfBlocks { - Returns: `true` if successful, otherwise `false`. */ @discardableResult - public func insert(child: Block & Node, before sibling: Block & Node) -> Bool { + public func insert(child: ChildNode, before sibling: ChildNode) -> Bool { return add(child) { cmark_node_insert_before(sibling.cmark_node, child.cmark_node) } } @@ -121,7 +116,7 @@ extension ContainerOfBlocks { - Returns: `true` if successful, otherwise `false`. */ @discardableResult - public func insert(child: Block & Node, after sibling: Block & Node) -> Bool { + public func insert(child: ChildNode, after sibling: ChildNode) -> Bool { return add(child) { cmark_node_insert_after(sibling.cmark_node, child.cmark_node) } } @@ -133,7 +128,7 @@ extension ContainerOfBlocks { - Returns: `true` if successful, otherwise `false`. */ @discardableResult - public func remove(child: Block & Node) -> Bool { + public func remove(child: ChildNode) -> Bool { guard child.parent == self else { return false } cmark_node_unlink(child.cmark_node) child.managed = true @@ -141,271 +136,51 @@ extension ContainerOfBlocks { } } -// MARK: - - -public protocol ContainerOfInlineElements: Node {} - -extension Heading: ContainerOfInlineElements {} -extension Paragraph: ContainerOfInlineElements {} -extension HTMLBlock: ContainerOfInlineElements {} -extension CodeBlock: ContainerOfInlineElements {} -extension ThematicBreak: ContainerOfInlineElements {} -extension Strong: ContainerOfInlineElements {} -extension Emphasis: ContainerOfInlineElements {} -extension Link: ContainerOfInlineElements {} - -extension ContainerOfInlineElements { - /// The block's children. - public var children: [Inline & Node] { - get { - return Children(of: self).compactMap { $0 as? Inline & Node } - } - - set { - for child in children { - remove(child: child) - } - - for child in newValue { - append(child: child) - } - } - } - - /** - Adds an inline element to the beginning of the block's children. - - - Parameters: - - child: The inline element to add. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func prepend(child: Inline & Node) -> Bool { - return add(child) { cmark_node_prepend_child(cmark_node, child.cmark_node) } - } - - /** - Adds an inline element to the end of the block's children. - - - Parameters: - - child: The inline element to add. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func append(child: Inline & Node) -> Bool { - return add(child) { cmark_node_append_child(cmark_node, child.cmark_node) } - } - - /** - Inserts an inline element to the block's children before a specified sibling. - - - Parameters: - - child: The inline element to add. - - sibling: The child before which the inline element is added - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func insert(child: Inline & Node, before sibling: Inline & Node) -> Bool { - return add(child) { cmark_node_insert_before(sibling.cmark_node, child.cmark_node) } - } - - /** - Inserts an inline element to the block's children after a specified sibling. - - - Parameters: - - child: The inline element to add. - - sibling: The child after which the inline element is added - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func insert(child: Inline & Node, after sibling: Inline & Node) -> Bool { - return add(child) { cmark_node_insert_after(sibling.cmark_node, child.cmark_node) } - } - /** - Removes an inline element from the block's children. +// MARK: - - - Parameters: - - child: The inline element to remove. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func remove(child: Inline & Node) -> Bool { - guard child.parent == self else { return false } - cmark_node_unlink(child.cmark_node) - child.managed = true - return true - } +extension Document: Container { + public typealias ChildNode = Block & Node +} +extension BlockQuote: Container { + public typealias ChildNode = Block & Node } // MARK: - -extension List { - /// The block's children. - public var children: [Item] { - get { - return Children(of: self).compactMap { $0 as? Item } - } - - set { - for child in children { - remove(child: child) - } - - for child in newValue { - append(child: child) - } - } - } - - /** - Adds a block to the beginning of the block's children. - - - Parameters: - - child: The block to add. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func prepend(child: Item) -> Bool { - return add(child) { cmark_node_prepend_child(cmark_node, child.cmark_node) } - } - - /** - Adds a block to the end of the block's children. - - - Parameters: - - child: The block to add. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func append(child: Item) -> Bool { - return add(child) { cmark_node_append_child(cmark_node, child.cmark_node) } - } - - /** - Inserts a block to the block's children before a specified sibling. - - - Parameters: - - child: The block to add. - - sibling: The child before which the block is added - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func insert(child: Item, before sibling: Item) -> Bool { - return add(child) { cmark_node_insert_before(sibling.cmark_node, child.cmark_node) } - } - - /** - Inserts a block to the block's children after a specified sibling. - - - Parameters: - - child: The block to add. - - sibling: The child after which the block is added - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func insert(child: Item, after sibling: Item) -> Bool { - return add(child) { cmark_node_insert_after(sibling.cmark_node, child.cmark_node) } - } - - /** - Removes a block from the block's children. - - - Parameters: - - child: The block to remove. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func remove(child: Item) -> Bool { - guard child.parent == self else { return false } - cmark_node_unlink(child.cmark_node) - child.managed = true - return true - } +extension Heading: Container { + public typealias ChildNode = Inline & Node +} +extension Paragraph: Container { + public typealias ChildNode = Inline & Node +} +extension HTMLBlock: Container { + public typealias ChildNode = Inline & Node +} +extension CodeBlock: Container { + public typealias ChildNode = Inline & Node +} +extension ThematicBreak: Container { + public typealias ChildNode = Inline & Node +} +extension Strong: Container { + public typealias ChildNode = Inline & Node +} +extension Emphasis: Container { + public typealias ChildNode = Inline & Node +} +extension Link: Container { + public typealias ChildNode = Inline & Node } // MARK: - -extension List.Item { - /// The list item's children. - public var children: [Node] { - get { - return Children(of: self).map { $0 } - } - - set { - for child in children { - remove(child: child) - } - - for child in newValue { - append(child: child) - } - } - } - - /** - Adds a node to the beginning of the list item's children. - - - Parameters: - - child: The block to add. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func prepend(child: Node) -> Bool { - return add(child) { cmark_node_prepend_child(cmark_node, child.cmark_node) } - } - - /** - Adds a node to the end of the list item's children. - - - Parameters: - - child: The block to add. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func append(child: Node) -> Bool { - return add(child) { cmark_node_append_child(cmark_node, child.cmark_node) } - } - - /** - Inserts a node to the list item's children before a specified sibling. - - - Parameters: - - child: The block to add. - - sibling: The child before which the block is added - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func insert(child: Node, before sibling: Node) -> Bool { - return add(child) { cmark_node_insert_before(sibling.cmark_node, child.cmark_node) } - } - - /** - Inserts a node to the list item's children after a specified sibling. - - - Parameters: - - child: The block to add. - - sibling: The child after which the block is added - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func insert(child: Node, after sibling: Node) -> Bool { - return add(child) { cmark_node_insert_after(sibling.cmark_node, child.cmark_node) } - } +extension List: Container { + public typealias ChildNode = Item +} - /** - Removes a node from the list item's children. +// MARK: - - - Parameters: - - child: The block to remove. - - Returns: `true` if successful, otherwise `false`. - */ - @discardableResult - public func remove(child: Node) -> Bool { - guard child.parent == self else { return false } - cmark_node_unlink(child.cmark_node) - child.managed = true - return true - } +extension List.Item: Container { + public typealias ChildNode = Node } From c8de119cf7a2b72a18ea322ee1583cc4650bc989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20St=C3=BChrk?= Date: Tue, 28 Apr 2020 20:19:37 +0200 Subject: [PATCH 2/6] Make convenience initializers available automatically. --- Sources/CommonMark/Nodes/Block/BlockQuote.swift | 8 -------- Sources/CommonMark/Nodes/Block/List.swift | 12 ------------ Sources/CommonMark/Nodes/Block/Paragraph.swift | 8 -------- Sources/CommonMark/Nodes/Document.swift | 8 -------- Sources/CommonMark/Nodes/Inline/Emphasis.swift | 8 -------- Sources/CommonMark/Nodes/Inline/Strong.swift | 8 -------- Sources/CommonMark/Supporting Types/Children.swift | 11 +++++++++++ 7 files changed, 11 insertions(+), 52 deletions(-) diff --git a/Sources/CommonMark/Nodes/Block/BlockQuote.swift b/Sources/CommonMark/Nodes/Block/BlockQuote.swift index 8dd3113..696bb86 100644 --- a/Sources/CommonMark/Nodes/Block/BlockQuote.swift +++ b/Sources/CommonMark/Nodes/Block/BlockQuote.swift @@ -13,12 +13,4 @@ import cmark */ public final class BlockQuote: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_BLOCK_QUOTE } - - public convenience init(children: [Block & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } diff --git a/Sources/CommonMark/Nodes/Block/List.swift b/Sources/CommonMark/Nodes/Block/List.swift index 885baf8..cfe49bb 100644 --- a/Sources/CommonMark/Nodes/Block/List.swift +++ b/Sources/CommonMark/Nodes/Block/List.swift @@ -44,18 +44,6 @@ public final class List: Node { public final class Item: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_ITEM } - - public convenience init(children: [Inline & Node] = []) { - self.init(children: [Paragraph(children: children)]) - } - - public convenience init(children: [Block & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } override class var cmark_node_type: cmark_node_type { return CMARK_NODE_LIST } diff --git a/Sources/CommonMark/Nodes/Block/Paragraph.swift b/Sources/CommonMark/Nodes/Block/Paragraph.swift index 25ae7f0..0a954a1 100644 --- a/Sources/CommonMark/Nodes/Block/Paragraph.swift +++ b/Sources/CommonMark/Nodes/Block/Paragraph.swift @@ -29,12 +29,4 @@ public final class Paragraph: Node { self.init(children: children) } - - public convenience init(children: [Inline & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } diff --git a/Sources/CommonMark/Nodes/Document.swift b/Sources/CommonMark/Nodes/Document.swift index 7d24b2d..d3026b2 100644 --- a/Sources/CommonMark/Nodes/Document.swift +++ b/Sources/CommonMark/Nodes/Document.swift @@ -63,14 +63,6 @@ public final class Document: Node { self.init(cmark_node) } - - public convenience init(children: [Block & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } // MARK: - Comparable diff --git a/Sources/CommonMark/Nodes/Inline/Emphasis.swift b/Sources/CommonMark/Nodes/Inline/Emphasis.swift index a31c44c..52e20f1 100644 --- a/Sources/CommonMark/Nodes/Inline/Emphasis.swift +++ b/Sources/CommonMark/Nodes/Inline/Emphasis.swift @@ -13,12 +13,4 @@ public final class Emphasis: Node { public convenience init(text string: String) { self.init(children: [Text(literal: string)]) } - - public convenience init(children: [Inline & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } diff --git a/Sources/CommonMark/Nodes/Inline/Strong.swift b/Sources/CommonMark/Nodes/Inline/Strong.swift index 8bf82d0..8de8052 100644 --- a/Sources/CommonMark/Nodes/Inline/Strong.swift +++ b/Sources/CommonMark/Nodes/Inline/Strong.swift @@ -13,12 +13,4 @@ public final class Strong: Node { public convenience init(text string: String) { self.init(children: [Text(literal: string)]) } - - public convenience init(children: [Inline & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } diff --git a/Sources/CommonMark/Supporting Types/Children.swift b/Sources/CommonMark/Supporting Types/Children.swift index 60f2492..7cea422 100644 --- a/Sources/CommonMark/Supporting Types/Children.swift +++ b/Sources/CommonMark/Supporting Types/Children.swift @@ -137,6 +137,17 @@ extension Container where ChildNode: Node { } +extension Container where ChildNode: Node { + public init(children: [ChildNode]) { + self.init(cmark_node_new(type(of: self).cmark_node_type)) + guard !children.isEmpty else { return } + for child in children { + append(child: child) + } + } +} + + // MARK: - extension Document: Container { From b50ffbd59977ca6f53050ab35ea5d636a3bcd5a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20St=C3=BChrk?= Date: Thu, 30 Apr 2020 17:38:38 +0200 Subject: [PATCH 3/6] Upgrade required Swift version to 5.2. --- .github/workflows/ci.yml | 2 +- Package.swift | 4 ++-- README.md | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0ef3d75..dc71aef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: strategy: matrix: - swift: ["5.1"] + swift: ["5.2"] container: image: swift:${{ matrix.swift }} diff --git a/Package.swift b/Package.swift index 818ef45..d2acb9e 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.1 +// swift-tools-version:5.2 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -18,7 +18,7 @@ let package = Package( ], dependencies: [ // Dependencies declare other packages that this package depends on. - .package(url: "https://github.com/SwiftDocOrg/swift-cmark.git", from: Version(0, 28, 3, prereleaseIdentifiers: [], buildMetadataIdentifiers: ["20200207", "1168665"])), + .package(name: "cmark", url: "https://github.com/SwiftDocOrg/swift-cmark.git", from: Version(0, 28, 3, prereleaseIdentifiers: [], buildMetadataIdentifiers: ["20200207", "1168665"])), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. diff --git a/README.md b/README.md index 7369871..4ed5bd0 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ $ swift test ## Requirements -- Swift 5.1+ +- Swift 5.2+ ## Installation From 27dfa8f61841dfeed21f1e8763a49216d4c6d3e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20St=C3=BChrk?= Date: Sun, 3 May 2020 12:02:53 +0200 Subject: [PATCH 4/6] Automatically provide Literal initializers. --- Sources/CommonMark/Nodes/Block/CodeBlock.swift | 5 ----- Sources/CommonMark/Nodes/Block/HTMLBlock.swift | 13 ------------- Sources/CommonMark/Nodes/Inline/Code.swift | 5 ----- Sources/CommonMark/Nodes/Inline/RawHTML.swift | 5 ----- Sources/CommonMark/Nodes/Inline/Text.swift | 5 ----- Sources/CommonMark/Supporting Types/Children.swift | 11 +++++++++++ Sources/CommonMark/Supporting Types/Literal.swift | 7 +++++++ 7 files changed, 18 insertions(+), 33 deletions(-) diff --git a/Sources/CommonMark/Nodes/Block/CodeBlock.swift b/Sources/CommonMark/Nodes/Block/CodeBlock.swift index 7301acd..07a2d07 100644 --- a/Sources/CommonMark/Nodes/Block/CodeBlock.swift +++ b/Sources/CommonMark/Nodes/Block/CodeBlock.swift @@ -27,11 +27,6 @@ import cmark public final class CodeBlock: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_CODE_BLOCK } - public convenience init(literal: String? = nil) { - self.init() - self.literal = literal - } - public convenience init(literal: String, fenceInfo: String? = nil) { self.init() self.literal = literal diff --git a/Sources/CommonMark/Nodes/Block/HTMLBlock.swift b/Sources/CommonMark/Nodes/Block/HTMLBlock.swift index 77a9202..3d8c815 100644 --- a/Sources/CommonMark/Nodes/Block/HTMLBlock.swift +++ b/Sources/CommonMark/Nodes/Block/HTMLBlock.swift @@ -12,17 +12,4 @@ import cmark */ public final class HTMLBlock: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_HTML_BLOCK } - - public convenience init(literal: String? = nil) { - self.init() - self.literal = literal - } - - public convenience init(literal: String, children: [Inline & Node] = []) { - self.init() - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } } diff --git a/Sources/CommonMark/Nodes/Inline/Code.swift b/Sources/CommonMark/Nodes/Inline/Code.swift index 3004669..612ad8d 100644 --- a/Sources/CommonMark/Nodes/Inline/Code.swift +++ b/Sources/CommonMark/Nodes/Inline/Code.swift @@ -12,9 +12,4 @@ import cmark */ public final class Code: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_CODE } - - public convenience init(literal: String? = nil) { - self.init() - self.literal = literal - } } diff --git a/Sources/CommonMark/Nodes/Inline/RawHTML.swift b/Sources/CommonMark/Nodes/Inline/RawHTML.swift index 1b40b9c..c548397 100644 --- a/Sources/CommonMark/Nodes/Inline/RawHTML.swift +++ b/Sources/CommonMark/Nodes/Inline/RawHTML.swift @@ -15,9 +15,4 @@ import cmark */ public final class RawHTML: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_HTML_INLINE } - - public convenience init(literal: String?) { - self.init() - self.literal = literal - } } diff --git a/Sources/CommonMark/Nodes/Inline/Text.swift b/Sources/CommonMark/Nodes/Inline/Text.swift index 5fd41af..de44d55 100644 --- a/Sources/CommonMark/Nodes/Inline/Text.swift +++ b/Sources/CommonMark/Nodes/Inline/Text.swift @@ -12,9 +12,4 @@ import cmark */ public final class Text: Node { override class var cmark_node_type: cmark_node_type { return CMARK_NODE_TEXT } - - public convenience init(literal: String? = nil) { - self.init() - self.literal = literal - } } diff --git a/Sources/CommonMark/Supporting Types/Children.swift b/Sources/CommonMark/Supporting Types/Children.swift index 7cea422..633a7d5 100644 --- a/Sources/CommonMark/Supporting Types/Children.swift +++ b/Sources/CommonMark/Supporting Types/Children.swift @@ -147,6 +147,17 @@ extension Container where ChildNode: Node { } } +extension Container where ChildNode: Node, Self: Literal { + public init(literal: String, children: [ChildNode] = []) { + self.init(cmark_node_new(Self.cmark_node_type)) + self.literal = literal + guard !children.isEmpty else { return } + for child in children { + append(child: child) + } + } +} + // MARK: - diff --git a/Sources/CommonMark/Supporting Types/Literal.swift b/Sources/CommonMark/Supporting Types/Literal.swift index 24409e6..c36c7ac 100644 --- a/Sources/CommonMark/Supporting Types/Literal.swift +++ b/Sources/CommonMark/Supporting Types/Literal.swift @@ -5,6 +5,13 @@ public protocol Literal: Node { init(literal: String?) } +public extension Literal where Self: Node { + init(literal: String? = nil) { + self.init(cmark_node_new(Self.cmark_node_type)) + self.literal = literal + } +} + // MARK: - extension Literal { From 42b01391217b7862468ce391df4ea8616f76d030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20St=C3=BChrk?= Date: Sun, 3 May 2020 11:11:43 +0200 Subject: [PATCH 5/6] Mitigate Swift compiler crash. --- Sources/CommonMark/Supporting Types/Children.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CommonMark/Supporting Types/Children.swift b/Sources/CommonMark/Supporting Types/Children.swift index 633a7d5..b0030ce 100644 --- a/Sources/CommonMark/Supporting Types/Children.swift +++ b/Sources/CommonMark/Supporting Types/Children.swift @@ -139,7 +139,7 @@ extension Container where ChildNode: Node { extension Container where ChildNode: Node { public init(children: [ChildNode]) { - self.init(cmark_node_new(type(of: self).cmark_node_type)) + self.init(cmark_node_new(Self.cmark_node_type)) guard !children.isEmpty else { return } for child in children { append(child: child) From 4234f8425f94af75be9b8edf299701af35c8b7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20St=C3=BChrk?= Date: Sun, 3 May 2020 12:10:39 +0200 Subject: [PATCH 6/6] Mitigate Swift compiler crash. --- Sources/CommonMark/Nodes/Block/Heading.swift | 17 ++++++++--------- Sources/CommonMark/Nodes/Inline/Link.swift | 11 +++++------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/Sources/CommonMark/Nodes/Block/Heading.swift b/Sources/CommonMark/Nodes/Block/Heading.swift index 4be2915..0ea2b27 100644 --- a/Sources/CommonMark/Nodes/Block/Heading.swift +++ b/Sources/CommonMark/Nodes/Block/Heading.swift @@ -42,15 +42,6 @@ public final class Heading: Node { self.init(level: level, children: [Text(literal: string)]) } - public convenience init(level: Int, children: [Inline & Node] = []) { - self.init() - self.level = level - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } - } - public var level: Int { get { return numericCast(cmark_node_get_heading_level(cmark_node)) @@ -62,3 +53,11 @@ public final class Heading: Node { } } } + +extension Container where Self: Heading { + public init(level: Int, children: [Self.ChildNode] = []) { + self.init(children: children) + self.level = level + } + +} diff --git a/Sources/CommonMark/Nodes/Inline/Link.swift b/Sources/CommonMark/Nodes/Inline/Link.swift index 6317516..ae969b7 100644 --- a/Sources/CommonMark/Nodes/Inline/Link.swift +++ b/Sources/CommonMark/Nodes/Inline/Link.swift @@ -21,14 +21,13 @@ public final class Link: Node { public convenience init(urlString: String, title: String? = nil, text string: String) { self.init(urlString: urlString, title: title, children: [Text(literal: string)]) } +} + - public convenience init(urlString: String?, title: String?, children: [Inline & Node] = []) { - self.init() +extension Container where Self: Link { + public init(urlString: String?, title: String?, children: [Self.ChildNode] = []) { + self.init(children: children) self.urlString = urlString self.title = title - guard !children.isEmpty else { return } - for child in children { - append(child: child) - } } }