@@ -603,6 +603,20 @@ object Scanners {
603
603
lastWidth = r.knownWidth
604
604
newlineIsSeparating = r.isInstanceOf [InBraces ]
605
605
606
+ // can emit OUTDENT if line is not non-empty blank line at EOF
607
+ inline def isTrailingBlankLine : Boolean =
608
+ token == EOF && {
609
+ val end = buf.length - 1 // take terminal NL as empty last line
610
+ val prev = buf.lastIndexWhere(! isWhitespace(_), end = end)
611
+ prev < 0 || end - prev > 0 && isLineBreakChar(buf(prev))
612
+ }
613
+
614
+ inline def canDedent : Boolean =
615
+ lastToken != INDENT
616
+ && ! isLeadingInfixOperator(nextWidth)
617
+ && ! statCtdTokens.contains(lastToken)
618
+ && ! isTrailingBlankLine
619
+
606
620
if newlineIsSeparating
607
621
&& canEndStatTokens.contains(lastToken)
608
622
&& canStartStatTokens.contains(token)
@@ -615,9 +629,8 @@ object Scanners {
615
629
|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH ) && token != CASE then
616
630
if currentRegion.isOutermost then
617
631
if nextWidth < lastWidth then currentRegion = topLevelRegion(nextWidth)
618
- else if ! isLeadingInfixOperator(nextWidth) && ! statCtdTokens.contains(lastToken) && lastToken != INDENT then
632
+ else if canDedent then
619
633
currentRegion match
620
- case _ if token == EOF => // no OUTDENT at EOF
621
634
case r : Indented =>
622
635
insert(OUTDENT , offset)
623
636
handleNewIndentWidth(r.enclosing, ir =>
@@ -671,13 +684,16 @@ object Scanners {
671
684
reset()
672
685
if atEOL then token = COLONeol
673
686
674
- // consume => and insert <indent> if applicable
687
+ // consume => and insert <indent> if applicable. Used to detect colon arrow: x =>
675
688
def observeArrowIndented (): Unit =
676
689
if isArrow && indentSyntax then
677
690
peekAhead()
678
- val atEOL = isAfterLineEnd || token == EOF
691
+ val atEOL = isAfterLineEnd
692
+ val atEOF = token == EOF
679
693
reset()
680
- if atEOL then
694
+ if atEOF then
695
+ token = EOF
696
+ else if atEOL then
681
697
val nextWidth = indentWidth(next.offset)
682
698
val lastWidth = currentRegion.indentWidth
683
699
if lastWidth < nextWidth then
0 commit comments