@@ -3,6 +3,7 @@ package dotty.tools.repl
3
3
import dotty .tools .backend .jvm .GenBCode
4
4
import dotty .tools .dotc .ast .Trees ._
5
5
import dotty .tools .dotc .ast .{tpd , untpd }
6
+ import dotty .tools .dotc .ast .tpd .TreeOps
6
7
import dotty .tools .dotc .core .Comments .CommentsContext
7
8
import dotty .tools .dotc .core .Contexts ._
8
9
import dotty .tools .dotc .core .Decorators ._
@@ -169,26 +170,27 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
169
170
def docOf (expr : String )(implicit state : State ): Result [String ] = {
170
171
implicit val ctx : Context = state.context
171
172
172
- /** Extract the (possibly) documented symbol from `tree` */
173
- def extractSymbol (tree : tpd.Tree ): Symbol = {
174
- tree match {
175
- case tpd.closureDef(defdef) => defdef.rhs.symbol
176
- case _ => tree.symbol
177
- }
178
- }
179
-
180
173
/**
181
- * Adapt `symbol` so that we get the one that holds the documentation.
182
- * Return also the symbols that `symbol` overrides`.
174
+ * Extract the "selected" symbol from `tree`.
175
+ *
176
+ * Because the REPL typechecks an expression, special syntax is needed to get the documentation
177
+ * of certain symbols:
178
+ *
179
+ * - To select the documentation of classes, the user needs to pass a call to the class' constructor
180
+ * (e.g. `new Foo` to select `class Foo`)
181
+ * - When methods are overloaded, the user needs to enter a lambda to specify which functions he wants
182
+ * (e.g. `foo(_: Int)` to select `def foo(x: Int)` instead of `def foo(x: String)`
183
+ *
184
+ * This function returns the right symbol for the received expression, and all the symbols that are
185
+ * overridden.
183
186
*/
184
- def pickSymbols ( symbol : Symbol ): Iterator [Symbol ] = {
185
- val selectedSymbol = {
186
- if (symbol.is( Module , butNot = ModuleClass )) symbol.moduleClass
187
- if (symbol.isConstructor) symbol.owner
188
- else symbol
187
+ def extractSymbols ( tree : tpd. Tree ): Iterator [Symbol ] = {
188
+ val sym = tree match {
189
+ case tree if tree.isInstantiation => tree. symbol.owner
190
+ case tpd.closureDef(defdef) => defdef.rhs.symbol
191
+ case _ => tree. symbol
189
192
}
190
-
191
- Iterator (selectedSymbol) ++ selectedSymbol.allOverriddenSymbols
193
+ Iterator (sym) ++ sym.allOverriddenSymbols
192
194
}
193
195
194
196
typeCheck(expr).map {
@@ -198,7 +200,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
198
200
else {
199
201
val doc =
200
202
ctx.docCtx.flatMap { docCtx =>
201
- val symbols = pickSymbols(extractSymbol( stat) )
203
+ val symbols = extractSymbols( stat)
202
204
symbols.collectFirst {
203
205
case sym if docCtx.docstrings.contains(sym) =>
204
206
docCtx.docstrings(sym).raw
0 commit comments