Skip to content

Commit 9a47eca

Browse files
authored
Merge pull request #8901 from cb372/repl-command-prefix
Support command prefixes in REPL
2 parents 7a17422 + 950be93 commit 9a47eca

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

compiler/src/dotty/tools/repl/ParseResult.scala

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ sealed trait Command extends ParseResult
4141
/** An unknown command that will not be handled by the REPL */
4242
case 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

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ class ReplDriver(settings: Array[String],
329329
out.println(s"""Unknown command: "$cmd", run ":help" for a list of commands""")
330330
state
331331

332+
case AmbiguousCommand(cmd, matching) =>
333+
out.println(s""""$cmd" matches ${matching.mkString(", ")}. Try typing a few more characters. Run ":help" for a list of commands""")
334+
state
335+
332336
case Help =>
333337
out.println(Help.text)
334338
state
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
scala>:t 123
2+
Int
3+
scala>:type 123
4+
Int
5+
scala>:typee 123
6+
Unknown command: ":typee", run ":help" for a list of commands

0 commit comments

Comments
 (0)