diff --git a/compiler/src/dotty/tools/repl/ParseResult.scala b/compiler/src/dotty/tools/repl/ParseResult.scala index effd7740517f..a67b247066f7 100644 --- a/compiler/src/dotty/tools/repl/ParseResult.scala +++ b/compiler/src/dotty/tools/repl/ParseResult.scala @@ -139,7 +139,7 @@ object ParseResult { Settings.command -> (arg => Settings(arg)), ) - def apply(source: SourceFile)(implicit state: State): ParseResult = { + def apply(source: SourceFile)(using state: State): ParseResult = { val sourceCode = source.content().mkString sourceCode match { case "" => Newline @@ -167,8 +167,14 @@ object ParseResult { } } - def apply(sourceCode: String)(implicit state: State): ParseResult = - apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode, maybeIncomplete = true)) + def apply(sourceCode: String)(using state: State): ParseResult = + maybeIncomplete(sourceCode, maybeIncomplete = true) + + def complete(sourceCode: String)(using state: State): ParseResult = + maybeIncomplete(sourceCode, maybeIncomplete = false) + + private def maybeIncomplete(sourceCode: String, maybeIncomplete: Boolean)(using state: State): ParseResult = + apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode, maybeIncomplete = maybeIncomplete)) /** Check if the input is incomplete. * diff --git a/compiler/src/dotty/tools/repl/ReplCompiler.scala b/compiler/src/dotty/tools/repl/ReplCompiler.scala index aab7ea46e9ed..8db288f50aca 100644 --- a/compiler/src/dotty/tools/repl/ReplCompiler.scala +++ b/compiler/src/dotty/tools/repl/ReplCompiler.scala @@ -166,7 +166,7 @@ class ReplCompiler extends Compiler: PackageDef(Ident(nme.EMPTY_PACKAGE), List(wrapper)) } - ParseResult(sourceFile)(state) match { + ParseResult(sourceFile) match { case Parsed(_, trees, _) => wrap(trees).result case SyntaxErrors(_, reported, trees) => diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index e64ecf02e2e3..4fab4b119a08 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -146,7 +146,7 @@ class ReplDriver(settings: Array[String], |Type in expressions for evaluation. Or try :help.""".stripMargin) /** Blockingly read a line, getting back a parse result */ - def readLine(state: State): ParseResult = { + def readLine()(using state: State): ParseResult = { val completer: Completer = { (_, line, candidates) => val comps = completions(line.cursor, line.line, state) candidates.addAll(comps.asJava) @@ -154,7 +154,7 @@ class ReplDriver(settings: Array[String], given Context = state.context try { val line = terminal.readLine(completer) - ParseResult(line)(state) + ParseResult(line) } catch { case _: EndOfFileException | _: UserInterruptException => // Ctrl+D or Ctrl+C @@ -163,7 +163,7 @@ class ReplDriver(settings: Array[String], } @tailrec def loop(using state: State)(): State = { - val res = readLine(state) + val res = readLine() if (res == Quit) state else loop(using interpret(res))() } @@ -173,8 +173,7 @@ class ReplDriver(settings: Array[String], } final def run(input: String)(using state: State): State = runBody { - val parsed = ParseResult(input)(state) - interpret(parsed) + interpret(ParseResult.complete(input)) } private def runBody(body: => State): State = rendering.classLoader()(using rootCtx).asContext(withRedirectedOutput(body)) diff --git a/compiler/test/dotty/tools/repl/LoadTests.scala b/compiler/test/dotty/tools/repl/LoadTests.scala index 520aeee86197..9b4fb8a44149 100644 --- a/compiler/test/dotty/tools/repl/LoadTests.scala +++ b/compiler/test/dotty/tools/repl/LoadTests.scala @@ -47,6 +47,19 @@ class LoadTests extends ReplTest { |""".stripMargin ) + @Test def truncated = loadTest( + file = """|def f: Unit = + | for i <- 1 to 2 + | do + | println(i)""".stripMargin, // was: unindent expected, but eof found + defs = """|def f: Unit + |""".stripMargin, + runCode = """f""", + output = """|1 + |2 + |""".stripMargin + ) + def loadTest(file: String, defs: String, runCode: String, output: String) = eval(s":load ${writeFile(file)}") andThen { assertMultiLineEquals(defs, storedOutput())