@@ -1157,10 +1157,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
1157
1157
*/
1158
1158
def importedSymbols (expr : Tree , name : Name )(implicit ctx : Context ): List [Symbol ] = {
1159
1159
def lookup (name : Name ): Symbol = expr.tpe.member(name).symbol
1160
- List (lookup(name.toTermName),
1161
- lookup(name.toTypeName),
1162
- lookup(name.moduleClassName),
1163
- lookup(name.sourceModuleName))
1160
+ val symbols =
1161
+ List (lookup(name.toTermName),
1162
+ lookup(name.toTypeName),
1163
+ lookup(name.moduleClassName),
1164
+ lookup(name.sourceModuleName))
1165
+
1166
+ symbols.map(_.sourceSymbol).filter(_.exists).distinct
1164
1167
}
1165
1168
1166
1169
/**
@@ -1174,38 +1177,59 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
1174
1177
def importedSymbols (imp : Import ,
1175
1178
selectorPredicate : untpd.Tree => Boolean = util.common.alwaysTrue)
1176
1179
(implicit ctx : Context ): List [Symbol ] = {
1177
- val symbols = imp.selectors.find(selectorPredicate) match {
1180
+ imp.selectors.find(selectorPredicate) match {
1178
1181
case Some (id : untpd.Ident ) =>
1179
1182
importedSymbols(imp.expr, id.name)
1180
1183
case Some (Thicket ((id : untpd.Ident ) :: (_ : untpd.Ident ) :: Nil )) =>
1181
1184
importedSymbols(imp.expr, id.name)
1182
1185
case _ =>
1183
1186
Nil
1184
1187
}
1185
-
1186
- symbols.map(_.sourceSymbol).filter(_.exists).distinct
1187
1188
}
1188
1189
1190
+ /**
1191
+ * The list of select trees that resolve to the same symbols as the ones that are imported
1192
+ * by `imp`.
1193
+ */
1189
1194
def importSelections (imp : Import )(implicit ctx : Context ): List [Select ] = {
1190
- val imported =
1191
- imp.selectors.flatMap {
1192
- case id : untpd.Ident =>
1193
- importedSymbols(imp.expr, id.name).map((_, id, None ))
1194
- case Thicket ((id : untpd.Ident ) :: (newName : untpd.Ident ) :: Nil ) =>
1195
- val renaming = Some (newName)
1196
- importedSymbols(imp.expr, id.name).map((_, id, renaming))
1195
+ def imported (sym : Symbol , id : untpd.Ident , rename : Option [untpd.Ident ]): List [Select ] = {
1196
+ val noPosExpr = focusPositions(imp.expr)
1197
+ val selectTree = Select (noPosExpr, sym.name).withPos(id.pos)
1198
+ rename match {
1199
+ case None =>
1200
+ selectTree :: Nil
1201
+ case Some (rename) =>
1202
+ // Get the type of the symbol that is actually selected, and construct a select
1203
+ // node with the new name and the type of the real symbol.
1204
+ val name = if (sym.name.isTypeName) rename.name.toTypeName else rename.name
1205
+ val actual = Select (noPosExpr, sym.name)
1206
+ val renameTree = Select (noPosExpr, name).withPos(rename.pos).withType(actual.tpe)
1207
+ selectTree :: renameTree :: Nil
1197
1208
}
1198
- imported.flatMap { case (symbol, name, rename) =>
1199
- val tree = Select (imp.expr, symbol.name).withPos(name.pos)
1200
- val renameTree = rename.map { r =>
1201
- // Get the type of the symbol that is actually selected, and construct a select
1202
- // node with the new name and the type of the real symbol.
1203
- val name = if (symbol.name.isTypeName) r.name.toTypeName else r.name
1204
- val actual = Select (imp.expr, symbol.name)
1205
- Select (imp.expr, name).withPos(r.pos).withType(actual.tpe)
1209
+ }
1210
+
1211
+ imp.selectors.flatMap {
1212
+ case Ident (nme.WILDCARD ) =>
1213
+ Nil
1214
+ case id : untpd.Ident =>
1215
+ importedSymbols(imp.expr, id.name).flatMap { sym =>
1216
+ imported(sym, id, None )
1217
+ }
1218
+ case Thicket ((id : untpd.Ident ) :: (newName : untpd.Ident ) :: Nil ) =>
1219
+ importedSymbols(imp.expr, id.name).flatMap { sym =>
1220
+ imported(sym, id, Some (newName))
1221
+ }
1222
+ }
1223
+ }
1224
+
1225
+ /** Replaces all positions in `tree` with zero-extent positions */
1226
+ private def focusPositions (tree : Tree )(implicit ctx : Context ): Tree = {
1227
+ val transformer = new tpd.TreeMap {
1228
+ override def transform (tree : Tree )(implicit ctx : Context ): Tree = {
1229
+ super .transform(tree).withPos(tree.pos.focus)
1206
1230
}
1207
- tree :: renameTree.toList
1208
1231
}
1232
+ transformer.transform(tree)
1209
1233
}
1210
1234
}
1211
1235
0 commit comments