@@ -3,17 +3,25 @@ package tasty
33
44import scala .quoted ._
55import dotty .tools .scaladoc .util .Escape ._
6+ import scala .collection .mutable .{ Map => MMap }
7+ import dotty .tools .io .AbstractFile
68
79class SymOps [Q <: Quotes ](val q : Q ) extends JavadocAnchorCreator with Scaladoc2AnchorCreator :
810 import q .reflect ._
911
1012 given Q = q
13+
14+ private val externalLinkCache : scala.collection.mutable.Map [AbstractFile , Option [ExternalDocLink ]] = MMap ()
15+
1116 extension (sym : Symbol )
1217 def packageName : String = (
1318 if (sym.isPackageDef) sym.fullName
1419 else sym.maybeOwner.packageName
1520 )
1621
22+ def packageNameSplitted : Seq [String ] =
23+ sym.packageName.split('.' ).toList
24+
1725 def className : Option [String ] =
1826 if (sym.isClassDef && ! sym.flags.is(Flags .Package )) Some (
1927 Some (sym.maybeOwner).filter(s => s.exists).flatMap(_.className).fold(" " )(cn => cn + " $" ) + sym.name
@@ -110,11 +118,11 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
110118 else termParamss(1 ).params(0 )
111119 }
112120
113- private def constructPath (location : String , anchor : Option [String ], link : ExternalDocLink ): String =
121+ private def constructPath (location : Seq [ String ] , anchor : Option [String ], link : ExternalDocLink ): String =
114122 val extension = " .html"
115123 val docURL = link.documentationUrl.toString
116124 def constructPathForJavadoc : String =
117- val l = " \\ $+" .r.replaceAllIn(location.replace( " . " , " /" ), _ => " ." )
125+ val l = " \\ $+" .r.replaceAllIn(location.mkString( " /" ), _ => " ." )
118126 val javadocAnchor = if anchor.isDefined then {
119127 val paramSigs = sym.paramSymss.flatten.map(_.tree).collect {
120128 case v : ValDef => v.tpt.tpe
@@ -125,15 +133,15 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
125133
126134 // TODO #263: Add anchor support
127135 def constructPathForScaladoc2 : String =
128- val l = escapeUrl(location).replace( " . " , " / " )
136+ val l = escapeUrl(location.mkString( " / " ) )
129137 val scaladoc2Anchor = if anchor.isDefined then {
130138 " #" + getScaladoc2Type(sym.tree)
131139 } else " "
132140 docURL + l + extension + scaladoc2Anchor
133141
134142 // TODO Add tests for it!
135143 def constructPathForScaladoc3 : String =
136- val base = docURL + escapeUrl(location).replace( " . " , " / " ) + extension
144+ val base = docURL + escapeUrl(location.mkString( " / " ) ) + extension
137145 anchor.fold(base)(a => base + " #" + a)
138146
139147 link.kind match {
@@ -154,7 +162,7 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
154162
155163 val className = sym.className
156164
157- val location = className.fold( sym.packageName)(cn => s " ${sym.packageName} . ${cn} " )
165+ val location = sym.packageNameSplitted ++ className
158166
159167 val anchor = sym.anchor
160168
@@ -163,13 +171,18 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
163171 import dotty .tools .dotc
164172 given ctx : dotc.core.Contexts .Context = q.asInstanceOf [scala.quoted.runtime.impl.QuotesImpl ].ctx
165173 val csym = sym.asInstanceOf [dotc.core.Symbols .Symbol ]
166- Option (csym.associatedFile).map(_.path).flatMap( path =>
167- dctx.externalDocumentationLinks.find(_.originRegexes.exists(r => r.matches(path)))
168- ).map(link => constructPath(location, anchor, link))
174+ val extLink = if externalLinkCache.contains(csym.associatedFile) then externalLinkCache(csym.associatedFile)
175+ else {
176+ val calculatedLink = Option (csym.associatedFile).map(_.path).flatMap( path =>
177+ dctx.externalDocumentationLinks.find(_.originRegexes.exists(r => r.matches(path))))
178+ externalLinkCache += (csym.associatedFile -> calculatedLink)
179+ calculatedLink
180+ }
181+ extLink.map(link => constructPath(location, anchor, link))
169182 }
170183
171184 DRI (
172- location,
185+ location.mkString( " . " ) ,
173186 anchor.getOrElse(" " ),
174187 externalLink = externalLink,
175188 // sym.show returns the same signature for def << = 1 and def >> = 2.
0 commit comments