Skip to content

Commit 96365b9

Browse files
committed
šŸ› fix: incorrect relationship label when protocol is not scanned first (fix #49)
fix #49
1 parent 61f57df commit 96365b9

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import Foundation
2+
3+
extension Array where Element == SyntaxStructure {
4+
/// order: protocols first, then non-extensions (i.e protocols, structs, classes, enums). Extensions last/
5+
/// This ensures that linking is correct
6+
/// see https://github.com/MarcoEidinger/SwiftPlantUML/issues/47
7+
/// as well as relationship label
8+
/// see https://github.com/MarcoEidinger/SwiftPlantUML/issues/49
9+
func orderedByProtocolsFirstExtensionsLast() -> [SyntaxStructure] {
10+
return sorted(by: { $0.kind ?? .struct < $1.kind ?? .struct })
11+
}
12+
}
13+
14+
extension ElementKind: Comparable {
15+
private var sortOrder: Int {
16+
switch self {
17+
case .protocol:
18+
return 0
19+
case .extension:
20+
return 2
21+
default:
22+
return 1
23+
}
24+
}
25+
26+
static func < (lhs: ElementKind, rhs: ElementKind) -> Bool {
27+
lhs.sortOrder < rhs.sortOrder
28+
}
29+
}
30+

ā€ŽSources/SwiftPlantUMLFramework/PlantUMLScript.swift

+1-20
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public struct PlantUMLScript {
4545

4646
var replacingText = "\n"
4747

48-
let orderedItems = items.orderedByExtensionsLast()
48+
let orderedItems = items.orderedByProtocolsFirstExtensionsLast()
4949
for (index, element) in orderedItems.enumerated() {
5050
if let text = processStructureItem(item: element, index: index) {
5151
replacingText.appendAsNewLine(text)
@@ -96,22 +96,3 @@ public struct PlantUMLScript {
9696
return item.plantuml(context: context) ?? nil
9797
}
9898
}
99-
100-
extension Array where Element == SyntaxStructure {
101-
/// order: non-extensions (i.e protocols, structs, classes, enums) first. Extensions last
102-
/// this ensures that linking is correct
103-
/// see https://github.com/MarcoEidinger/SwiftPlantUML/issues/47
104-
func orderedByExtensionsLast() -> [SyntaxStructure] {
105-
var ordered: [SyntaxStructure] = []
106-
var nonExtInsertPoint = 0
107-
forEach {
108-
if $0.kind == .extension {
109-
ordered.append($0)
110-
} else {
111-
ordered.insert($0, at: nonExtInsertPoint)
112-
nonExtInsertPoint += 1
113-
}
114-
}
115-
return ordered
116-
}
117-
}

ā€ŽTests/SwiftPlantUMLFrameworkTests/SyntaxStructureTests.swift

+5-3
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,14 @@ final class SyntaxStructureTests: XCTestCase {
128128
let ext1 = SyntaxStructure(kind: .extension, name: "Ext1")
129129
let p1 = SyntaxStructure(kind: .protocol, name: "Protocol1")
130130
let c1 = SyntaxStructure(kind: .class, name: "Class1")
131+
let s1 = SyntaxStructure(kind: .struct, name: "Struct1")
131132
let ext2 = SyntaxStructure(kind: .extension, name: "Ext2")
132133
let c2 = SyntaxStructure(kind: .class, name: "Class2")
134+
let s2 = SyntaxStructure(kind: .struct, name: "Struct2")
133135

134-
let unordered = [ext1, p1, c1, ext2, c2]
135-
let ordered = unordered.orderedByExtensionsLast()
136-
XCTAssertEqual(ordered, [p1, c1, c2, ext1, ext2])
136+
let unordered = [ext1, c1, p1, s1, ext2, c2, s2]
137+
let ordered = unordered.orderedByProtocolsFirstExtensionsLast()
138+
XCTAssertEqual(ordered, [p1, c1, s1, c2, s2, ext1, ext2])
137139
}
138140

139141
func getTestFile() throws -> URL {

0 commit comments

Comments
Ā (0)