Skip to content

Commit ab9caa0

Browse files
committed
Check trailing blank line at EOF for OUTDENT
1 parent a1eb867 commit ab9caa0

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

+15-2
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,20 @@ object Scanners {
601601
lastWidth = r.knownWidth
602602
newlineIsSeparating = r.isInstanceOf[InBraces]
603603

604+
// can emit OUTDENT if line is not non-empty blank line at EOF
605+
inline def isTrailingBlankLine: Boolean =
606+
token == EOF && {
607+
val end = buf.length - 1 // take terminal NL as empty last line
608+
val prev = buf.lastIndexWhere(!isWhitespace(_), end = end)
609+
prev < 0 || end - prev > 0 && isLineBreakChar(buf(prev))
610+
}
611+
612+
inline def canDedent: Boolean =
613+
lastToken != INDENT
614+
&& !isLeadingInfixOperator(nextWidth)
615+
&& !statCtdTokens.contains(lastToken)
616+
&& !isTrailingBlankLine
617+
604618
if newlineIsSeparating
605619
&& canEndStatTokens.contains(lastToken)
606620
&& canStartStatTokens.contains(token)
@@ -613,9 +627,8 @@ object Scanners {
613627
|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH) && token != CASE then
614628
if currentRegion.isOutermost then
615629
if nextWidth < lastWidth then currentRegion = topLevelRegion(nextWidth)
616-
else if !isLeadingInfixOperator(nextWidth) && !statCtdTokens.contains(lastToken) && lastToken != INDENT then
630+
else if canDedent then
617631
currentRegion match
618-
case _ if token == EOF => // no OUTDENT at EOF
619632
case r: Indented =>
620633
insert(OUTDENT, offset)
621634
handleNewIndentWidth(r.enclosing, ir =>

compiler/src/dotty/tools/dotc/util/Chars.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ object Chars:
5050
}
5151

5252
/** Is character a whitespace character (but not a new line)? */
53-
def isWhitespace(c: Char): Boolean =
53+
inline def isWhitespace(c: Char): Boolean =
5454
c == ' ' || c == '\t' || c == CR
5555

5656
/** Can character form part of a doc comment variable $xxx? */

compiler/test-resources/repl/i10886

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
scala> println:
2+
| "hello"
3+
|
4+
hello

0 commit comments

Comments
 (0)