Skip to content

Commit d16721c

Browse files
authored
Merge pull request #10 from BotTech/fix/member-symbol
Fix member as starting symbol
2 parents 75fa832 + 0db15db commit d16721c

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

core/src/main/scala/nz/co/bottech/scala2plantuml/DiagramModifications.scala

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ private[scala2plantuml] object DiagramModifications {
1515

1616
import elementsWithNames._
1717

18+
// TODO: This should be redundant.
1819
def removeHidden(options: Options): ElementsWithNames =
1920
options.hide match {
2021
case Options.HideMatching(patterns) =>
@@ -104,16 +105,7 @@ private[scala2plantuml] object DiagramModifications {
104105
val owner = member.ownerSymbol
105106
if (previousType.exists(_.symbol == owner)) loop(tail, previousType, acc :+ member)
106107
else {
107-
def createClass =
108-
Class(
109-
scalaTypeName(symbolToScalaIdentifier(owner)),
110-
owner,
111-
isObject = false,
112-
isAbstract = false,
113-
Seq.empty,
114-
Seq.empty
115-
)
116-
val ownerElement = types.getOrElse(owner, createClass)
108+
val ownerElement = types.getOrElse(owner, fakeOwner(owner))
117109
loop(tail, Some(ownerElement), acc :+ ownerElement :+ member)
118110
}
119111
case head +: tail =>
@@ -122,9 +114,32 @@ private[scala2plantuml] object DiagramModifications {
122114
}
123115
val newElements = loop(elements, None, Vector.empty)
124116
elementsWithNames.copy(elements = newElements)
125-
case _ => elementsWithNames
117+
case _ =>
118+
// Even when unsorted there can be elements without parents, which typically occurs
119+
// when the starting symbol is a member.
120+
val types = elements.collect { case typ: Type =>
121+
typ.symbol
122+
}.toSet
123+
val newElements = elements.flatMap {
124+
case member: Member =>
125+
val owner = member.ownerSymbol
126+
if (types.contains(owner)) List(member)
127+
else List(fakeOwner(owner), member)
128+
case element => List(element)
129+
}
130+
elementsWithNames.copy(elements = newElements)
126131
}
127132

133+
private def fakeOwner(symbol: String) =
134+
Class(
135+
scalaTypeName(symbolToScalaIdentifier(symbol)),
136+
symbol,
137+
isObject = symbol.endsWith("."),
138+
isAbstract = false,
139+
Seq.empty,
140+
Seq.empty
141+
)
142+
128143
def calculateNames(options: Options): ElementsWithNames = {
129144
assert(names.isEmpty)
130145
def typeParameterSymbols(parameters: Seq[TypeParameter]): Seq[String] =
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package nz.co.bottech.scala2plantuml
2+
3+
import utest.{test, TestSuite, Tests}
4+
5+
object StartingSymbolTests extends TestSuite with ClassDiagramTests {
6+
7+
override protected val exampleDir: String = "start"
8+
9+
val tests: Tests = Tests {
10+
test("method") {
11+
success(
12+
"Foo.apply().",
13+
"""class Foo {
14+
| + {static} {method} apply
15+
|}""".stripMargin
16+
)
17+
}
18+
}
19+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package nz.co.bottech.scala2plantuml.examples.start
2+
3+
object Foo {
4+
5+
def apply(): String = "Hello"
6+
}

0 commit comments

Comments
 (0)