Skip to content

Commit

Permalink
fix: don't split :<word> into two tokens.
Browse files Browse the repository at this point in the history
In the situation where we have `:im<tab>` normally `:` and `im` would be split into
two tokens and it would result in `::imports`. Instead, we detect early if we are
about to complete a command and instead of parsing it normally like Scala we just
grab the entire thing as the word.
  • Loading branch information
ckipp01 committed Mar 8, 2022
1 parent 4577255 commit 3323d5d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/repl/JLineTerminal.scala
Expand Up @@ -152,6 +152,14 @@ final class JLineTerminal extends java.io.Closeable {
// using dummy values, resulting parsed input is probably unused
defaultParsedLine

// In the situation where we have a partial command that we want to
// complete we need to ensure that the :<partial-word> isn't split into
// 2 tokens, but rather the entire thing is treated as the "word", in
// order to insure the : is replaced in the completion.
case ParseContext.COMPLETE if
ParseResult.commands.exists(command => command._1.startsWith(input)) =>
parsedLine(input, cursor)

case ParseContext.COMPLETE =>
// Parse to find completions (typically after a Tab).
def isCompletable(token: Token) = isIdentifier(token) || isKeyword(token)
Expand Down
8 changes: 1 addition & 7 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Expand Up @@ -221,13 +221,7 @@ class ReplDriver(settings: Array[String],

if expr.startsWith(":") then
ParseResult.commands.collect {
// If expr is only : then we return the commands with : since jline
// correctly handles them
case command if expr == ":" => makeCandidate(command._1)
// However if there is more than just the : we filter by it and then
// drop the : since jline will correctly complete it but you'll end up
// with ::import for example instead of :import
case command if command._1.startsWith(expr) => makeCandidate(command._1.drop(1))
case command if command._1.startsWith(expr) => makeCandidate(command._1)
}
else
given state: State = newRun(state0)
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/TabcompleteTests.scala
Expand Up @@ -211,7 +211,7 @@ class TabcompleteTests extends ReplTest {
@Test def commandPreface = initially {
// This looks odd, but if we return :doc here it will result in ::doc in the REPL
assertEquals(
List("doc"),
List(":doc"),
tabComplete(":d")
)
}
Expand Down

0 comments on commit 3323d5d

Please sign in to comment.