From 1235fc8eb84000ec91690fd574ad0c13770c43fb Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 9 Mar 2021 14:21:51 +1300 Subject: [PATCH 1/2] Fix member as starting symbol --- .../scala2plantuml/DiagramModifications.scala | 37 +++++++++++++------ .../scala2plantuml/StartingSymbolTests.scala | 17 +++++++++ .../scala2plantuml/examples/start/Foo.scala | 6 +++ 3 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala create mode 100644 core/src/test/scala/nz/co/bottech/scala2plantuml/examples/start/Foo.scala diff --git a/core/src/main/scala/nz/co/bottech/scala2plantuml/DiagramModifications.scala b/core/src/main/scala/nz/co/bottech/scala2plantuml/DiagramModifications.scala index 2438988..f3209ba 100644 --- a/core/src/main/scala/nz/co/bottech/scala2plantuml/DiagramModifications.scala +++ b/core/src/main/scala/nz/co/bottech/scala2plantuml/DiagramModifications.scala @@ -15,6 +15,7 @@ private[scala2plantuml] object DiagramModifications { import elementsWithNames._ + // TODO: This should be redundant. def removeHidden(options: Options): ElementsWithNames = options.hide match { case Options.HideMatching(patterns) => @@ -104,16 +105,7 @@ private[scala2plantuml] object DiagramModifications { val owner = member.ownerSymbol if (previousType.exists(_.symbol == owner)) loop(tail, previousType, acc :+ member) else { - def createClass = - Class( - scalaTypeName(symbolToScalaIdentifier(owner)), - owner, - isObject = false, - isAbstract = false, - Seq.empty, - Seq.empty - ) - val ownerElement = types.getOrElse(owner, createClass) + val ownerElement = types.getOrElse(owner, fakeOwner(owner)) loop(tail, Some(ownerElement), acc :+ ownerElement :+ member) } case head +: tail => @@ -122,9 +114,32 @@ private[scala2plantuml] object DiagramModifications { } val newElements = loop(elements, None, Vector.empty) elementsWithNames.copy(elements = newElements) - case _ => elementsWithNames + case _ => + // Even when unsorted there can be elements without parents, which typically occurs + // when the starting symbol is a member. + val types = elements.collect { case typ: Type => + typ.symbol + }.toSet + val newElements = elements.flatMap { + case member: Member => + val owner = member.ownerSymbol + if (types.contains(owner)) List(member) + else List(fakeOwner(owner), member) + case element => List(element) + } + elementsWithNames.copy(elements = newElements) } + private def fakeOwner(symbol: String) = + Class( + scalaTypeName(symbolToScalaIdentifier(symbol)), + symbol, + isObject = symbol.endsWith("."), + isAbstract = false, + Seq.empty, + Seq.empty + ) + def calculateNames(options: Options): ElementsWithNames = { assert(names.isEmpty) def typeParameterSymbols(parameters: Seq[TypeParameter]): Seq[String] = diff --git a/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala b/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala new file mode 100644 index 0000000..e7cef66 --- /dev/null +++ b/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala @@ -0,0 +1,17 @@ +package nz.co.bottech.scala2plantuml + +import utest.{TestSuite, Tests, test} + +object StartingSymbolTests extends TestSuite with ClassDiagramTests { + + override protected val exampleDir: String = "start" + + val tests: Tests = Tests { + test("method") { + success("Foo.apply().", + """class Foo { + | + {static} {method} apply + |}""".stripMargin) + } + } +} diff --git a/core/src/test/scala/nz/co/bottech/scala2plantuml/examples/start/Foo.scala b/core/src/test/scala/nz/co/bottech/scala2plantuml/examples/start/Foo.scala new file mode 100644 index 0000000..304a7c2 --- /dev/null +++ b/core/src/test/scala/nz/co/bottech/scala2plantuml/examples/start/Foo.scala @@ -0,0 +1,6 @@ +package nz.co.bottech.scala2plantuml.examples.start + +object Foo { + + def apply(): String = "Hello" +} From 0db15db4be129b16ba67c81b19eb31f58456d123 Mon Sep 17 00:00:00 2001 From: Jason Pickens Date: Tue, 9 Mar 2021 14:56:43 +1300 Subject: [PATCH 2/2] Fix formatting --- .../co/bottech/scala2plantuml/StartingSymbolTests.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala b/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala index e7cef66..d567c91 100644 --- a/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala +++ b/core/src/test/scala/nz/co/bottech/scala2plantuml/StartingSymbolTests.scala @@ -1,6 +1,6 @@ package nz.co.bottech.scala2plantuml -import utest.{TestSuite, Tests, test} +import utest.{test, TestSuite, Tests} object StartingSymbolTests extends TestSuite with ClassDiagramTests { @@ -8,10 +8,12 @@ object StartingSymbolTests extends TestSuite with ClassDiagramTests { val tests: Tests = Tests { test("method") { - success("Foo.apply().", + success( + "Foo.apply().", """class Foo { | + {static} {method} apply - |}""".stripMargin) + |}""".stripMargin + ) } } }