Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReplDriver.run and :load take complete input #15811

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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