Skip to content

Commit 1b33a36

Browse files
committed
Fix #5183: (REPL) Only insert line break when cursor is not on the last line
... or input is incomplete.
1 parent 7b9ffbb commit 1b33a36

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

compiler/src/dotty/tools/repl/JLineTerminal.scala

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ final class JLineTerminal extends java.io.Closeable {
9595
def words = java.util.Collections.emptyList[String]
9696
}
9797

98-
def parse(line: String, cursor: Int, context: ParseContext): reader.ParsedLine = {
98+
def parse(input: String, cursor: Int, context: ParseContext): reader.ParsedLine = {
9999
def parsedLine(word: String, wordCursor: Int) =
100-
new ParsedLine(cursor, line, word, wordCursor)
100+
new ParsedLine(cursor, input, word, wordCursor)
101101
// Used when no word is being completed
102102
def defaultParsedLine = parsedLine("", 0)
103103

@@ -110,7 +110,7 @@ final class JLineTerminal extends java.io.Closeable {
110110

111111
case class TokenData(token: Token, start: Int, end: Int)
112112
def currentToken: TokenData /* | Null */ = {
113-
val source = new SourceFile("<completions>", line)
113+
val source = new SourceFile("<completions>", input)
114114
val scanner = new Scanner(source)(ctx.fresh.setReporter(Reporter.NoReporter))
115115
while (scanner.token != EOF) {
116116
val start = scanner.offset
@@ -125,23 +125,22 @@ final class JLineTerminal extends java.io.Closeable {
125125
null
126126
}
127127

128+
def acceptLine = {
129+
val onLastLine = !input.substring(cursor).contains(System.lineSeparator)
130+
onLastLine && !ParseResult.isIncomplete(input)
131+
}
132+
128133
context match {
129-
case ParseContext.ACCEPT_LINE =>
130-
// ENTER means SUBMIT when
131-
// - cursor is at end (discarding whitespaces)
132-
// - and, input line is complete
133-
val cursorIsAtEnd = line.indexWhere(!_.isWhitespace, from = cursor) < 0
134-
if (cursorIsAtEnd && !ParseResult.isIncomplete(line))
135-
defaultParsedLine // using dummy values, resulting parsed line is probably unused
136-
else
137-
incomplete()
134+
case ParseContext.ACCEPT_LINE if acceptLine =>
135+
// using dummy values, resulting parsed input is probably unused
136+
defaultParsedLine
138137

139138
case ParseContext.COMPLETE =>
140139
// Parse to find completions (typically after a Tab).
141140
def isCompletable(token: Token) = isIdentifier(token) || isKeyword(token)
142141
currentToken match {
143142
case TokenData(token, start, end) if isCompletable(token) =>
144-
val word = line.substring(start, end)
143+
val word = input.substring(start, end)
145144
val wordCursor = cursor - start
146145
parsedLine(word, wordCursor)
147146
case _ =>

0 commit comments

Comments
 (0)