Skip to content

Commit

Permalink
Merge pull request #15811 from som-snytt/issue/15808-repl-load-truncated
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand authored Aug 4, 2022
2 parents d032d1f + 39786b8 commit 6a84737
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/repl/ParseResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/ReplCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
9 changes: 4 additions & 5 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ 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)
}
given Context = state.context
try {
val line = terminal.readLine(completer)
ParseResult(line)(state)
ParseResult(line)
} catch {
case _: EndOfFileException |
_: UserInterruptException => // Ctrl+D or Ctrl+C
Expand All @@ -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))()
}
Expand All @@ -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))
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/repl/LoadTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 6a84737

Please sign in to comment.