diff --git a/Sources/SwiftPlantUMLFramework/Internal/String+Extensions.swift b/Sources/SwiftPlantUMLFramework/Internal/String+Extensions.swift index e4b11a7..c26c4a2 100644 --- a/Sources/SwiftPlantUMLFramework/Internal/String+Extensions.swift +++ b/Sources/SwiftPlantUMLFramework/Internal/String+Extensions.swift @@ -7,8 +7,26 @@ internal extension String { } internal extension String { + /// example: "Hello".getAngleBracketsWithContent() returns "Hello" func removeAngleBracketsWithContent() -> String { - replacingOccurrences(of: "\\<[^\\]]+\\>", with: "", options: .regularExpression) + replacingOccurrences(of: "\\<.*\\>", with: "", options: .regularExpression) + } + + /// example: "Hello".getAngleBracketsWithContent() returns "" + func getAngleBracketsWithContent() -> String? { + do { + let regex = try NSRegularExpression(pattern: "\\<.*\\>") + let results = regex.matches(in: self, + range: NSRange(startIndex..., in: self)) + let result = results.map { + String(self[Range($0.range, in: self)!]) + } + + return result.first + } catch { + print("invalid regex: \(error.localizedDescription)") + return nil + } } } diff --git a/Sources/SwiftPlantUMLFramework/Internal/SyntaxStructure+PlantUML.swift b/Sources/SwiftPlantUMLFramework/Internal/SyntaxStructure+PlantUML.swift index dcdc4de..e272b08 100644 --- a/Sources/SwiftPlantUMLFramework/Internal/SyntaxStructure+PlantUML.swift +++ b/Sources/SwiftPlantUMLFramework/Internal/SyntaxStructure+PlantUML.swift @@ -141,7 +141,10 @@ extension SyntaxStructure { } private func genericsStatement() -> String? { - guard let substructure = substructure else { return nil } + guard let substructure = substructure else { + guard let parent = inheritedTypes?.first else { return nil } + return parent.name?.getAngleBracketsWithContent() + } let params = substructure.filter { $0.kind == SwiftPlantUMLFramework.ElementKind.genericTypeParam } var genParts: [String] = [] for param in params { diff --git a/Tests/SwiftPlantUMLFrameworkTests/StringExtensionsTests.swift b/Tests/SwiftPlantUMLFrameworkTests/StringExtensionsTests.swift index a7499ed..b969f68 100644 --- a/Tests/SwiftPlantUMLFrameworkTests/StringExtensionsTests.swift +++ b/Tests/SwiftPlantUMLFrameworkTests/StringExtensionsTests.swift @@ -5,6 +5,15 @@ final class StringExtensionsTests: XCTestCase { func testRemoveAngleBracketsWithContent() { XCTAssertEqual("A".removeAngleBracketsWithContent(), "A") XCTAssertEqual("A".removeAngleBracketsWithContent(), "A") + XCTAssertEqual("Handler".removeAngleBracketsWithContent(), "Handler") + } + + func testExtractsContentInAngleBrackets() { + XCTAssertEqual("Hello, <[World]>".getAngleBracketsWithContent(), "<[World]>") + XCTAssertEqual("Hello, <<[World]>>".getAngleBracketsWithContent(), "<<[World]>>") + XCTAssertNil("Hello, World!".getAngleBracketsWithContent()) + XCTAssertNil("Hello, ".getAngleBracketsWithContent()) } func testIsMatching() { diff --git a/Tests/SwiftPlantUMLFrameworkTests/SyntaxStructureTests.swift b/Tests/SwiftPlantUMLFrameworkTests/SyntaxStructureTests.swift index 624a721..8360cd3 100644 --- a/Tests/SwiftPlantUMLFrameworkTests/SyntaxStructureTests.swift +++ b/Tests/SwiftPlantUMLFrameworkTests/SyntaxStructureTests.swift @@ -106,6 +106,16 @@ final class SyntaxStructureTests: XCTestCase { XCTAssertTrue(plantUMLElement!.contains("")) } + func testStructureGenericsParent() { + let code = """ + class Handler {} + class MyHandler: Handler {} + """ + let cut = SyntaxStructure.create(from: code) + let plantUMLElement = cut?.find(.class, named: "MyHandler")?.plantuml(context: PlantUMLContext(configuration: Configuration(elements: ElementOptions(showGenerics: true)))) + XCTAssertTrue(plantUMLElement!.contains(#"class "MyHandler" as MyHandler"#)) + } + func testStructureHideGenerics() { let cut = SyntaxStructure.create(from: "struct aStruct {}") let plantUMLElement = cut?.find(.struct, named: "aStruct")?.plantuml(context: PlantUMLContext(configuration: Configuration(elements: ElementOptions(showGenerics: false))))