Skip to content

Commit

Permalink
Allow linking to Java class with Scala object (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ennru authored and raboof committed Jan 2, 2020
1 parent c60d8f6 commit 675d1f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/main/scala/com/lightbend/paradox/apidoc/ApidocDirective.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,19 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], ctx: Writer.Cont
private case class Query(label: Option[String], pattern: String, generics: String, linkToObject: Boolean) {
def scalaLabel(matched: String): String =
label match {
case None => matched.split('.').last.replace("$", ".") + generics
case None =>
matched
.split('.')
.last
// replace inner class dots
.replaceAll("\\$(.)", ".$1")
// remove ending $
.replaceAll("\\$$", "") + generics
case Some(la) => la + generics
}

def scalaFqcn(matched: String): String =
matched.replace("$", ".")
matched.replaceAll("\\$(.)", ".$1")

def javaLabel(matched: String): String =
scalaLabel(matched)
Expand Down Expand Up @@ -102,7 +109,15 @@ class ApidocDirective(allClassesAndObjects: IndexedSeq[String], ctx: Writer.Cont
}
}
} else {
renderMatches(query, allClasses.filter(_.endsWith('.' + query.pattern)), node, visitor, printer)
val className = '.' + query.pattern
val classMatches = allClasses.filter(_.endsWith(className))
if (classMatches.size == 1 && classMatches(0).contains(".javadsl.")) {
val objectName = className + '$'
val allMatches = allClassesAndObjects.filter { name =>
name.endsWith(className) || name.endsWith(objectName)
}
renderMatches(query, allMatches, node, visitor, printer)
} else renderMatches(query, classMatches, node, visitor, printer)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ApidocDirectiveSpec extends MarkdownBaseSpec {
"akka.stream.javadsl.Flow$",
"akka.stream.scaladsl.Flow",
"akka.stream.scaladsl.Flow$",
"akka.stream.scaladsl.JavaFlowSupport$",
"akka.stream.javadsl.JavaFlowSupport",
"akka.kafka.scaladsl.Consumer$Control",
"akka.kafka.javadsl.Consumer$Control",
"akka.kafka.scaladsl.Consumer$Control$$anonfun$drainAndShutdown$2",
Expand Down Expand Up @@ -155,6 +157,16 @@ class ApidocDirectiveSpec extends MarkdownBaseSpec {
)
}

it should "generate markdown correctly for scaladsl `object` and javadsl `class`" in {
markdown("@apidoc[JavaFlowSupport]") shouldEqual
html(
"""<p><span class="group-scala">
|<a href="https://doc.akka.io/api/akka/2.5/akka/stream/scaladsl/JavaFlowSupport$.html" title="akka.stream.scaladsl.JavaFlowSupport"><code>JavaFlowSupport</code></a></span><span class="group-java">
|<a href="https://doc.akka.io/japi/akka/2.5/?akka/stream/javadsl/JavaFlowSupport.html" title="akka.stream.javadsl.JavaFlowSupport"><code>JavaFlowSupport</code></a></span>
|</p>""".stripMargin
)
}

it should "generate markdown correctly for type parameter and wildcard" in {
markdown("@apidoc[Source[ServerSentEvent, \\_]]") shouldEqual
html(
Expand Down

0 comments on commit 675d1f4

Please sign in to comment.