@@ -41,6 +41,9 @@ sealed trait Command extends ParseResult
41
41
/** An unknown command that will not be handled by the REPL */
42
42
case class UnknownCommand (cmd : String ) extends Command
43
43
44
+ /** An ambiguous prefix that matches multiple commands */
45
+ case class AmbiguousCommand (cmd : String , matchingCommands : List [String ]) extends Command
46
+
44
47
/** `:load <path>` interprets a scala file as if entered line-by-line into
45
48
* the REPL
46
49
*/
@@ -116,19 +119,27 @@ object ParseResult {
116
119
stats
117
120
}
118
121
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
+
119
132
def apply (source : SourceFile )(implicit state : State ): ParseResult = {
120
133
val sourceCode = source.content().mkString
121
134
sourceCode match {
122
135
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
+ }
132
143
}
133
144
case _ =>
134
145
implicit val ctx : Context = state.context
0 commit comments