Skip to content

Fix #8762: Make REPL work with indented code #8765

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

Merged
merged 2 commits into from
Apr 22, 2020
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ node_modules

# VS Code
.vscode/
.metals/

# Scala-IDE specific
.scala_dependencies
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ object Parsers {
if in.token == COLONEOL then
in.nextToken()
if in.token != INDENT then
syntaxError(i"indented definitions expected")
syntaxErrorOrIncomplete(i"indented definitions expected")
else
newLineOptWhenFollowedBy(LBRACE)

Expand Down
19 changes: 11 additions & 8 deletions compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ object Scanners {
def observeColonEOL(): Unit =
if token == COLON then
lookahead()
val atEOL = isAfterLineEnd
val atEOL = isAfterLineEnd || token == EOF
reset()
if atEOL then token = COLONEOL

Expand Down Expand Up @@ -618,6 +618,12 @@ object Scanners {
this.copyFrom(prev)
}

def closeIndented() = currentRegion match
case r: Indented if !r.isOutermost =>
insert(OUTDENT, offset)
currentRegion = r.outer
case _ =>

/** - Join CASE + CLASS => CASECLASS, CASE + OBJECT => CASEOBJECT, SEMI + ELSE => ELSE, COLON + <EOL> => COLONEOL
* - Insert missing OUTDENTs at EOF
*/
Expand Down Expand Up @@ -658,13 +664,10 @@ object Scanners {
reset()
case COLON =>
if colonSyntax then observeColonEOL()
case EOF | RBRACE | RPAREN | RBRACKET =>
currentRegion match {
case r: Indented if !r.isOutermost =>
insert(OUTDENT, offset)
currentRegion = r.outer
case _ =>
}
case RBRACE | RPAREN | RBRACKET =>
closeIndented()
case EOF if !source.maybeIncomplete =>
closeIndented()
case _ =>
}
}
Expand Down
9 changes: 8 additions & 1 deletion compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class SourceFile(val file: AbstractFile, computeContent: => Array[Char]) extends
myContent
}

private var _maybeInComplete: Boolean = false

def maybeIncomplete: Boolean = _maybeInComplete

def this(file: AbstractFile, codec: Codec) = this(file, new String(file.toByteArray, codec.charSet).toCharArray)

/** Tab increment; can be overridden */
Expand Down Expand Up @@ -200,7 +204,10 @@ object SourceFile {

def fromId(id: Int): SourceFile = sourceOfChunk(id >> ChunkSizeLog)

def virtual(name: String, content: String) = new SourceFile(new VirtualFile(name, content.getBytes), scala.io.Codec.UTF8)
def virtual(name: String, content: String, maybeIncomplete: Boolean = false) =
val src = new SourceFile(new VirtualFile(name, content.getBytes), scala.io.Codec.UTF8)
src._maybeInComplete = maybeIncomplete
src

private final val ChunkSizeLog = 10
private final val ChunkSize = 1 << ChunkSizeLog
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/repl/ParseResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ object ParseResult {
}

def apply(sourceCode: String)(implicit state: State): ParseResult =
apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode))
apply(SourceFile.virtual(str.REPL_SESSION_LINE + (state.objectIndex + 1), sourceCode, maybeIncomplete = true))

/** Check if the input is incomplete.
*
Expand All @@ -159,7 +159,7 @@ object ParseResult {
case CommandExtract(_) | "" => false
case _ => {
val reporter = newStoreReporter
val source = SourceFile.virtual("<incomplete-handler>", sourceCode)
val source = SourceFile.virtual("<incomplete-handler>", sourceCode, maybeIncomplete = true)
val unit = CompilationUnit(source, mustExist = false)
val localCtx = ctx.fresh
.setCompilationUnit(unit)
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ReplDriver(settings: Array[String],
compiler
.typeCheck(expr, errorsAllowed = true)
.map { tree =>
val file = SourceFile.virtual("<completions>", expr)
val file = SourceFile.virtual("<completions>", expr, maybeIncomplete = true)
val unit = CompilationUnit(file)(state.context)
unit.tpdTree = tree
implicit val ctx = state.context.fresh.setCompilationUnit(unit)
Expand Down