@@ -3,17 +3,25 @@ package tasty
3
3
4
4
import scala .quoted ._
5
5
import dotty .tools .scaladoc .util .Escape ._
6
+ import scala .collection .mutable .{ Map => MMap }
7
+ import dotty .tools .io .AbstractFile
6
8
7
9
class SymOps [Q <: Quotes ](val q : Q ) extends JavadocAnchorCreator with Scaladoc2AnchorCreator :
8
10
import q .reflect ._
9
11
10
12
given Q = q
13
+
14
+ private val externalLinkCache : scala.collection.mutable.Map [AbstractFile , Option [ExternalDocLink ]] = MMap ()
15
+
11
16
extension (sym : Symbol )
12
17
def packageName : String = (
13
18
if (sym.isPackageDef) sym.fullName
14
19
else sym.maybeOwner.packageName
15
20
)
16
21
22
+ def packageNameSplitted : Seq [String ] =
23
+ sym.packageName.split('.' ).toList
24
+
17
25
def className : Option [String ] =
18
26
if (sym.isClassDef && ! sym.flags.is(Flags .Package )) Some (
19
27
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
110
118
else termParamss(1 ).params(0 )
111
119
}
112
120
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 =
114
122
val extension = " .html"
115
123
val docURL = link.documentationUrl.toString
116
124
def constructPathForJavadoc : String =
117
- val l = " \\ $+" .r.replaceAllIn(location.replace( " . " , " /" ), _ => " ." )
125
+ val l = " \\ $+" .r.replaceAllIn(location.mkString( " /" ), _ => " ." )
118
126
val javadocAnchor = if anchor.isDefined then {
119
127
val paramSigs = sym.paramSymss.flatten.map(_.tree).collect {
120
128
case v : ValDef => v.tpt.tpe
@@ -125,15 +133,15 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
125
133
126
134
// TODO #263: Add anchor support
127
135
def constructPathForScaladoc2 : String =
128
- val l = escapeUrl(location).replace( " . " , " / " )
136
+ val l = escapeUrl(location.mkString( " / " ) )
129
137
val scaladoc2Anchor = if anchor.isDefined then {
130
138
" #" + getScaladoc2Type(sym.tree)
131
139
} else " "
132
140
docURL + l + extension + scaladoc2Anchor
133
141
134
142
// TODO Add tests for it!
135
143
def constructPathForScaladoc3 : String =
136
- val base = docURL + escapeUrl(location).replace( " . " , " / " ) + extension
144
+ val base = docURL + escapeUrl(location.mkString( " / " ) ) + extension
137
145
anchor.fold(base)(a => base + " #" + a)
138
146
139
147
link.kind match {
@@ -154,7 +162,7 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
154
162
155
163
val className = sym.className
156
164
157
- val location = className.fold( sym.packageName)(cn => s " ${sym.packageName} . ${cn} " )
165
+ val location = sym.packageNameSplitted ++ className
158
166
159
167
val anchor = sym.anchor
160
168
@@ -163,13 +171,18 @@ class SymOps[Q <: Quotes](val q: Q) extends JavadocAnchorCreator with Scaladoc2A
163
171
import dotty .tools .dotc
164
172
given ctx : dotc.core.Contexts .Context = q.asInstanceOf [scala.quoted.runtime.impl.QuotesImpl ].ctx
165
173
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))
169
182
}
170
183
171
184
DRI (
172
- location,
185
+ location.mkString( " . " ) ,
173
186
anchor.getOrElse(" " ),
174
187
externalLink = externalLink,
175
188
// sym.show returns the same signature for def << = 1 and def >> = 2.
0 commit comments