Skip to content

Fix member as starting symbol #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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 =>
Expand All @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nz.co.bottech.scala2plantuml

import utest.{test, TestSuite, Tests}

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
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package nz.co.bottech.scala2plantuml.examples.start

object Foo {

def apply(): String = "Hello"
}