summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-04-08 16:42:38 +0000
committerPaul Phillips <paulp@improving.org>2011-04-08 16:42:38 +0000
commit8e093b517f2c987cb5b4efe8dfb6736d54708e35 (patch)
tree50a963109d8cd6851a05cd9d040fbfd4a676c737
parent6eae720732d493a3a3370a067a3b3b05aef73820 (diff)
downloadscala-8e093b517f2c987cb5b4efe8dfb6736d54708e35.tar.gz
scala-8e093b517f2c987cb5b4efe8dfb6736d54708e35.tar.bz2
scala-8e093b517f2c987cb5b4efe8dfb6736d54708e35.zip
Added :imports command, no review.
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/ILoop.scala26
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/IMain.scala14
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/Imports.scala16
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala4
4 files changed, 56 insertions, 4 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/ILoop.scala b/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
index 830fad5332..d1a23f3bc5 100644
--- a/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/ILoop.scala
@@ -220,6 +220,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: PrintWriter)
nullary("help", "print this help message", helpCommand),
historyCommand,
cmd("h?", "<string>", "search the history", searchHistory),
+ cmd("imports", "[name name ...]", "show import history, identifying sources of names", importsCommand),
cmd("implicits", "[-v]", "show the implicits in scope", implicitsCommand),
cmd("javap", "<path|class>", "disassemble a file or class name", javapCommand),
nullary("keybindings", "show how ctrl-[A-Z] and other keys are bound", keybindingsCommand),
@@ -254,6 +255,31 @@ class ILoop(in0: Option[BufferedReader], protected val out: PrintWriter)
"scala.runtime." -> "runtime."
)
+ private def importsCommand(line: String): Result = {
+ val tokens = words(line)
+ val handlers = intp.languageWildcardHandlers ++ intp.importHandlers
+ val isVerbose = tokens contains "-v"
+
+ handlers.filterNot(_.importedSymbols.isEmpty).zipWithIndex foreach {
+ case (handler, idx) =>
+ val (types, terms) = handler.importedSymbols partition (_.name.isTypeName)
+ val imps = handler.implicitSymbols
+ val found = tokens filter (handler importsSymbolNamed _)
+ val typeMsg = if (types.isEmpty) "" else types.size + " types"
+ val termMsg = if (terms.isEmpty) "" else terms.size + " terms"
+ val implicitMsg = if (imps.isEmpty) "" else imps.size + " are implicit"
+ val foundMsg = if (found.isEmpty) "" else found.mkString(" // imports: ", ", ", "")
+ val statsMsg = List(typeMsg, termMsg, implicitMsg) filterNot (_ == "") mkString ("(", ", ", ")")
+
+ intp.reporter.printMessage("%2d) %-30s %s%s".format(
+ idx + 1,
+ handler.importString,
+ statsMsg,
+ foundMsg
+ ))
+ }
+ }
+
private def implicitsCommand(line: String): Result = {
val intp = ILoop.this.intp
import intp._
diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala
index f407b42689..62e781958f 100644
--- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala
@@ -860,8 +860,10 @@ class IMain(val settings: Settings, protected val out: PrintWriter) extends Impo
case _ => naming.mostRecentVar
})
- private def requestForName(name: Name): Option[Request] =
+ private def requestForName(name: Name): Option[Request] = {
+ assert(definedNameMap != null, "definedNameMap is null")
definedNameMap get name
+ }
private def requestForIdent(line: String): Option[Request] =
requestForName(newTermName(line)) orElse requestForName(newTypeName(line))
@@ -927,17 +929,23 @@ class IMain(val settings: Settings, protected val out: PrintWriter) extends Impo
return None
}
+ def asQualifiedImport = {
+ val name = expr.takeWhile(_ != '.')
+ importedTermNamed(name) flatMap { sym =>
+ typeOfExpression(sym.fullName + expr.drop(name.length))
+ }
+ }
def asModule = safeModule(expr) map (_.tpe)
def asExpr = beSilentDuring {
val lhs = freshInternalVarName()
- interpret("lazy val " + lhs + " = { " + expr + " } ") match {
+ interpret("lazy val " + lhs + " = { " + expr + " } ", true) match {
case IR.Success => typeOfExpression(lhs)
case _ => None
}
}
typeOfExpressionDepth += 1
- try typeOfTerm(expr) orElse asModule orElse asExpr
+ try typeOfTerm(expr) orElse asModule orElse asExpr orElse asQualifiedImport
finally typeOfExpressionDepth -= 1
}
// def compileAndTypeExpr(expr: String): Option[Typer] = {
diff --git a/src/compiler/scala/tools/nsc/interpreter/Imports.scala b/src/compiler/scala/tools/nsc/interpreter/Imports.scala
index 990f504692..b2706330e7 100644
--- a/src/compiler/scala/tools/nsc/interpreter/Imports.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/Imports.scala
@@ -15,9 +15,21 @@ trait Imports {
import definitions.{ ScalaPackage, JavaLangPackage, PredefModule }
import memberHandlers._
+ /** Synthetic import handlers for the language defined imports. */
+ private def makeWildcardImportHandler(sym: Symbol): ImportHandler = {
+ val hd :: tl = sym.fullName.split('.').toList map newTermName
+ val tree = Import(
+ tl.foldLeft(Ident(hd): Tree)((x, y) => Select(x, y)),
+ List(ImportSelector(nme.WILDCARD, -1, null, -1))
+ )
+ tree setSymbol sym
+ new ImportHandler(tree)
+ }
+
/** Symbols whose contents are language-defined to be imported. */
def languageWildcardSyms: List[Symbol] = List(JavaLangPackage, ScalaPackage, PredefModule)
def languageWildcards: List[Type] = languageWildcardSyms map (_.tpe)
+ def languageWildcardHandlers = languageWildcardSyms map makeWildcardImportHandler
def importedTerms = onlyTerms(importHandlers flatMap (_.importedNames))
def importedTypes = onlyTypes(importHandlers flatMap (_.importedNames))
@@ -44,8 +56,12 @@ trait Imports {
def languageSymbols = languageWildcardSyms flatMap membersAtPickler
def sessionImportedSymbols = importHandlers flatMap (_.importedSymbols)
def importedSymbols = languageSymbols ++ sessionImportedSymbols
+ def importedTermSymbols = importedSymbols collect { case x: TermSymbol => x }
+ def importedTypeSymbols = importedSymbols collect { case x: TypeSymbol => x }
def implicitSymbols = importedSymbols filter (_.isImplicit)
+ def importedTermNamed(name: String) = importedTermSymbols find (_.name.toString == name)
+
/** Tuples of (source, imported symbols) in the order they were imported.
*/
def importedSymbolsBySource: List[(Symbol, List[Symbol])] = {
diff --git a/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala b/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala
index f98da3aeac..208b033afb 100644
--- a/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/MemberHandlers.scala
@@ -216,7 +216,9 @@ trait MemberHandlers {
/** The names imported by this statement */
override lazy val importedNames: List[Name] = wildcardNames ++ individualNames
+ lazy val importsSymbolNamed: Set[String] = importedNames map (_.toString) toSet
- override def resultExtractionCode(req: Request) = codegenln(imp.toString) + "\n"
+ def importString = imp.toString
+ override def resultExtractionCode(req: Request) = codegenln(importString) + "\n"
}
}