Skip to content

Commit 315f58f

Browse files
committed
SI-6626 make @throws tags create links to exceptions
In scaladoc, this turns exceptions in @throws tags into links (when it can find the target exception), instead of just showing the name.
1 parent b556b2f commit 315f58f

File tree

5 files changed

+72
-5
lines changed

5 files changed

+72
-5
lines changed

src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,28 @@ trait CommentFactoryBase { this: MemberLookupBase =>
345345
Map.empty[String, Body] ++ pairs
346346
}
347347

348+
def linkedExceptions: Map[String, Body] = {
349+
val m = allSymsOneTag(SimpleTagKey("throws"))
350+
351+
m.map { case (name,body) =>
352+
val link = memberLookup(pos, name, site)
353+
val newBody = body match {
354+
case Body(List(Paragraph(Chain(content)))) =>
355+
val descr = Text(" ") +: content
356+
val entityLink = EntityLink(Monospace(Text(name)), link)
357+
Body(List(Paragraph(Chain(entityLink +: descr))))
358+
case _ => body
359+
}
360+
(name, newBody)
361+
}
362+
}
363+
348364
val com = createComment (
349365
body0 = Some(parseUncycloAtSymbol(docBody.toString, pos, site)),
350366
authors0 = allTags(SimpleTagKey("author")),
351367
see0 = allTags(SimpleTagKey("see")),
352368
result0 = oneTag(SimpleTagKey("return")),
353-
throws0 = allSymsOneTag(SimpleTagKey("throws")),
369+
throws0 = linkedExceptions,
354370
valueParams0 = allSymsOneTag(SimpleTagKey("param")),
355371
typeParams0 = allSymsOneTag(SimpleTagKey("tparam")),
356372
version0 = oneTag(SimpleTagKey("version")),

src/scaladoc/scala/tools/nsc/doc/html/page/Template.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class Template(universe: doc.Universe, generator: DiagramGenerator, tpl: DocTemp
605605
<dd>{
606606
val exceptionsXml: List[NodeSeq] =
607607
for((name, body) <- comment.throws.toList.sortBy(_._1) ) yield
608-
<span class="cmt">{Text(name) ++ bodyToHtml(body)}</span>
608+
<span class="cmt">{bodyToHtml(body)}</span>
609609
exceptionsXml.reduceLeft(_ ++ Text("") ++ _)
610610
}</dd>
611611
}

src/scaladoc/scala/tools/partest/ScaladocModelTest.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,16 @@ abstract class ScaladocModelTest extends DirectTest {
182182
}
183183
}
184184

185-
def countLinks(c: Comment, p: EntityLink => Boolean) = {
186-
def countLinks(body: Any): Int = body match {
185+
def countLinks(c: Comment, p: EntityLink => Boolean): Int = countLinksInBody(c.body, p)
186+
187+
def countLinksInBody(body: Body, p: EntityLink => Boolean): Int = {
188+
def countLinks(b: Any): Int = b match {
187189
case el: EntityLink if p(el) => 1
188190
case s: Seq[_] => s.toList.map(countLinks(_)).sum
189191
case p: Product => p.productIterator.toList.map(countLinks(_)).sum
190192
case _ => 0
191193
}
192-
countLinks(c.body)
194+
countLinks(body)
193195
}
194196

195197
def testDiagram(doc: DocTemplateEntity, diag: Option[Diagram], nodes: Int, edges: Int) = {

test/scaladoc/run/t6626.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
newSource:10: warning: Could not find any member to link for "SomeUnknownException".
2+
/**
3+
^
4+
newSource:10: warning: Could not find any member to link for "IOException".
5+
/**
6+
^
7+
Done.

test/scaladoc/run/t6626.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import scala.tools.nsc.doc.base._
2+
import scala.tools.nsc.doc.base.comment._
3+
import scala.tools.nsc.doc.model._
4+
import scala.tools.partest.ScaladocModelTest
5+
6+
object Test extends ScaladocModelTest {
7+
8+
override def code = """
9+
10+
package org.foo
11+
12+
class MyException extends Exception
13+
14+
class MyOtherException extends Exception
15+
16+
object Foo {
17+
/**
18+
* Test exception linking
19+
*
20+
* @throws org.foo.MyException linked with a fully-qualified name
21+
* @throws MyOtherException linked with a relative name
22+
* @throws SomeUnknownException not linked at all (but with some text)
23+
* @throws IOException
24+
*/
25+
def test(): Unit = ???
26+
}
27+
"""
28+
29+
def scaladocSettings = ""
30+
31+
def testModel(rootPackage: Package) = {
32+
// get the quick access implicit defs in scope (_package(s), _class(es), _trait(s), object(s) _method(s), _value(s))
33+
import access._
34+
35+
val a = rootPackage._package("org")._package("foo")._object("Foo")._method("test")
36+
val throws = a.comment.get.throws
37+
val allbodies = Body(throws.values.flatMap(_.blocks).toSeq)
38+
39+
val links = countLinksInBody(allbodies, _.link.isInstanceOf[LinkToTpl[_]])
40+
assert(links == 2, links + " == 2 (links to MyException and MyOtherException)")
41+
}
42+
}

0 commit comments

Comments
 (0)