Skip to content

Commit df5b967

Browse files
committed
Simplify symbol extraction for doc in REPL
1 parent 6fb9c64 commit df5b967

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dotty.tools.repl
33
import dotty.tools.backend.jvm.GenBCode
44
import dotty.tools.dotc.ast.Trees._
55
import dotty.tools.dotc.ast.{tpd, untpd}
6+
import dotty.tools.dotc.ast.tpd.TreeOps
67
import dotty.tools.dotc.core.Comments.CommentsContext
78
import dotty.tools.dotc.core.Contexts._
89
import dotty.tools.dotc.core.Decorators._
@@ -169,26 +170,27 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
169170
def docOf(expr: String)(implicit state: State): Result[String] = {
170171
implicit val ctx: Context = state.context
171172

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-
180173
/**
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.
183186
*/
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
189192
}
190-
191-
Iterator(selectedSymbol) ++ selectedSymbol.allOverriddenSymbols
193+
Iterator(sym) ++ sym.allOverriddenSymbols
192194
}
193195

194196
typeCheck(expr).map {
@@ -198,7 +200,7 @@ class ReplCompiler(val directory: AbstractFile) extends Compiler {
198200
else {
199201
val doc =
200202
ctx.docCtx.flatMap { docCtx =>
201-
val symbols = pickSymbols(extractSymbol(stat))
203+
val symbols = extractSymbols(stat)
202204
symbols.collectFirst {
203205
case sym if docCtx.docstrings.contains(sym) =>
204206
docCtx.docstrings(sym).raw

0 commit comments

Comments
 (0)