@@ -41,6 +41,9 @@ sealed trait Command extends ParseResult
4141/** An unknown command that will not be handled by the REPL */
4242case class UnknownCommand (cmd : String ) extends Command
4343
44+ /** An ambiguous prefix that matches multiple commands */
45+ case class AmbiguousCommand (cmd : String , matchingCommands : List [String ]) extends Command
46+
4447/** `:load <path>` interprets a scala file as if entered line-by-line into
4548 * the REPL
4649 */
@@ -116,19 +119,27 @@ object ParseResult {
116119 stats
117120 }
118121
122+ private val commands : List [(String , String => ParseResult )] = List (
123+ Quit .command -> (_ => Quit ),
124+ Help .command -> (_ => Help ),
125+ Reset .command -> (_ => Reset ),
126+ Imports .command -> (_ => Imports ),
127+ Load .command -> (arg => Load (arg)),
128+ TypeOf .command -> (arg => TypeOf (arg)),
129+ DocOf .command -> (arg => DocOf (arg))
130+ )
131+
119132 def apply (source : SourceFile )(implicit state : State ): ParseResult = {
120133 val sourceCode = source.content().mkString
121134 sourceCode match {
122135 case " " => Newline
123- case CommandExtract (cmd, arg) => cmd match {
124- case Quit .command => Quit
125- case Help .command => Help
126- case Reset .command => Reset
127- case Imports .command => Imports
128- case Load .command => Load (arg)
129- case TypeOf .command => TypeOf (arg)
130- case DocOf .command => DocOf (arg)
131- case _ => UnknownCommand (cmd)
136+ case CommandExtract (cmd, arg) => {
137+ val matchingCommands = commands.filter((command, _) => command.startsWith(cmd))
138+ matchingCommands match {
139+ case Nil => UnknownCommand (cmd)
140+ case (_, f) :: Nil => f(arg)
141+ case multiple => AmbiguousCommand (cmd, multiple.map(_._1))
142+ }
132143 }
133144 case _ =>
134145 implicit val ctx : Context = state.context
0 commit comments