@@ -322,20 +322,23 @@ object Interactive {
322
322
323
323
def traverser (source : SourceFile ) = {
324
324
new untpd.TreeTraverser {
325
- private def handleImport (imported : List [Symbol ],
326
- uexpr : untpd.Tree ,
327
- id : untpd.Ident ,
328
- rename : Option [untpd.Ident ]): Unit = {
329
- val expr = uexpr.asInstanceOf [tpd.Tree ]
325
+ private def handleImport (imp : tpd.Import ): Unit = {
326
+ val imported =
327
+ imp.selectors.flatMap {
328
+ case id : untpd.Ident =>
329
+ importedSymbols(imp.expr, id.name).map((_, id, None ))
330
+ case Thicket ((id : untpd.Ident ) :: (newName : untpd.Ident ) :: Nil ) =>
331
+ importedSymbols(imp.expr, id.name).map((_, id, Some (newName)))
332
+ }
330
333
imported match {
331
334
case Nil =>
332
- traverse(expr)
335
+ traverse(imp. expr)
333
336
case syms =>
334
- syms.foreach { sym =>
335
- val tree = tpd.Select (expr, sym.name).withPos(id .pos)
337
+ syms.foreach { case ( sym, name, rename) =>
338
+ val tree = tpd.Select (imp. expr, sym.name).withPos(name .pos)
336
339
val renameTree = rename.map { r =>
337
340
val name = if (sym.name.isTypeName) r.name.toTypeName else r.name
338
- RenameTree (name, tpd.Select (expr, sym.name)).withPos(r.pos)
341
+ RenameTree (name, tpd.Select (imp. expr, sym.name)).withPos(r.pos)
339
342
}
340
343
renameTree.foreach(traverse)
341
344
traverse(tree)
@@ -344,12 +347,8 @@ object Interactive {
344
347
}
345
348
override def traverse (tree : untpd.Tree )(implicit ctx : Context ) = {
346
349
tree match {
347
- case imp @ Import (uexpr, (id : untpd.Ident ) :: Nil ) if includeImports =>
348
- val imported = importedSymbols(imp.asInstanceOf [tpd.Import ])
349
- handleImport(imported, uexpr, id, None )
350
- case imp @ Import (uexpr, Thicket ((id : untpd.Ident ) :: (rename : untpd.Ident ) :: Nil ) :: Nil ) if includeImports =>
351
- val imported = importedSymbols(imp.asInstanceOf [tpd.Import ])
352
- handleImport(imported, uexpr, id, Some (rename))
350
+ case imp : untpd.Import if includeImports =>
351
+ handleImport(imp.asInstanceOf [tpd.Import ])
353
352
case utree : untpd.NameTree if tree.hasType =>
354
353
val tree = utree.asInstanceOf [tpd.NameTree ]
355
354
if (tree.symbol.exists
@@ -522,25 +521,33 @@ object Interactive {
522
521
private def importedSymbols (imp : tpd.Import ,
523
522
selectorPredicate : untpd.Tree => Boolean = util.common.alwaysTrue)
524
523
(implicit ctx : Context ): List [Symbol ] = {
525
- def lookup0 (name : Name ): Symbol = imp.expr.tpe.member(name).symbol
526
- def lookup (name : Name ): List [Symbol ] = {
527
- lookup0(name.toTermName) ::
528
- lookup0(name.toTypeName) ::
529
- lookup0(name.moduleClassName) ::
530
- lookup0(name.sourceModuleName) :: Nil
531
- }
532
-
533
524
val symbols = imp.selectors.find(selectorPredicate) match {
534
525
case Some (id : untpd.Ident ) =>
535
- lookup( id.name)
526
+ importedSymbols(imp.expr, id.name)
536
527
case Some (Thicket ((id : untpd.Ident ) :: (_ : untpd.Ident ) :: Nil )) =>
537
- lookup(id.name)
538
- case _ => Nil
528
+ importedSymbols(imp.expr, id.name)
529
+ case _ =>
530
+ Nil
539
531
}
540
532
541
533
symbols.map(sourceSymbol).filter(_.exists).distinct
542
534
}
543
535
536
+ /**
537
+ * The symbols that are imported with `expr.name`
538
+ *
539
+ * @param expr The base of the import statement
540
+ * @param name The name that is being imported.
541
+ * @return All the symbols that would be imported with `expr.name`.
542
+ */
543
+ private def importedSymbols (expr : tpd.Tree , name : Name )(implicit ctx : Context ): List [Symbol ] = {
544
+ def lookup (name : Name ): Symbol = expr.tpe.member(name).symbol
545
+ lookup(name.toTermName) ::
546
+ lookup(name.toTypeName) ::
547
+ lookup(name.moduleClassName) ::
548
+ lookup(name.sourceModuleName) :: Nil
549
+ }
550
+
544
551
/**
545
552
* Used to represent a renaming import `{foo => bar}`.
546
553
* We need this because the name of the tree must be the new name, but the
0 commit comments