Skip to content

Commit ebcc91d

Browse files
committed
Cleanup observeIndented logic
1 parent 9cab509 commit ebcc91d

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ object Parsers {
12301230
// note: next is defined here because current == NEWLINE
12311231
if (in.token == NEWLINE && in.next.token == token) in.nextToken()
12321232

1233+
def newLinesOptWhenFollowedBy(name: Name): Unit =
1234+
if in.isNewLine && in.next.token == IDENTIFIER && in.next.name == name then
1235+
in.nextToken()
1236+
12331237
def newLineOptWhenFollowing(p: Int => Boolean): Unit =
12341238
// note: next is defined here because current == NEWLINE
12351239
if (in.token == NEWLINE && p(in.next.token)) newLineOpt()
@@ -1245,7 +1249,7 @@ object Parsers {
12451249
}
12461250

12471251
def possibleTemplateStart(): Unit = {
1248-
in.observeIndented(unless = noIndentTemplateTokens, unlessSoftKW = nme.derives)
1252+
in.observeIndented()
12491253
newLineOptWhenFollowedBy(LBRACE)
12501254
}
12511255

@@ -2373,7 +2377,7 @@ object Parsers {
23732377
dropParensOrBraces(start, if (in.token == YIELD || in.token == DO) "" else "do")
23742378
}
23752379
}
2376-
in.observeIndented(unless = noIndentAfterEnumeratorTokens)
2380+
in.observeIndented()
23772381
res
23782382
}
23792383
else {
@@ -3505,6 +3509,7 @@ object Parsers {
35053509
/** Template ::= InheritClauses [TemplateBody]
35063510
*/
35073511
def template(constr: DefDef, isEnum: Boolean = false): Template = {
3512+
newLinesOptWhenFollowedBy(nme.derives)
35083513
val (parents, derived) = inheritClauses()
35093514
possibleTemplateStart()
35103515
if (isEnum) {
@@ -3517,10 +3522,11 @@ object Parsers {
35173522
/** TemplateOpt = [Template]
35183523
*/
35193524
def templateOpt(constr: DefDef): Template =
3520-
possibleTemplateStart()
3525+
newLinesOptWhenFollowedBy(nme.derives)
35213526
if in.token == EXTENDS || isIdent(nme.derives) then
35223527
template(constr)
35233528
else
3529+
possibleTemplateStart()
35243530
if in.isNestedStart then
35253531
template(constr)
35263532
else

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

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ object Scanners {
385385
lookahead.allowLeadingInfixOperators = false
386386
// force a NEWLINE a after current token if it is on its own line
387387
lookahead.nextToken()
388-
canStartExpressionTokens.contains(lookahead.token)
388+
canStartExprTokens.contains(lookahead.token)
389389
}
390390
&& {
391391
if isScala2Mode || oldSyntax && !rewrite then
@@ -541,29 +541,19 @@ object Scanners {
541541
|Previous indent : $lastWidth
542542
|Latest indent : $nextWidth"""
543543

544-
def observeIndented(
545-
unless: BitSet = BitSet.empty,
546-
unlessSoftKW: TermName = EmptyTermName): Unit
547-
=
548-
if (indentSyntax && isAfterLineEnd && token != INDENT) {
549-
val nextOffset = if (isNewLine) next.offset else offset
550-
val nextToken = if (isNewLine) next.token else token
551-
val nextWidth = indentWidth(nextOffset)
552-
val lastWidth = currentRegion match {
544+
def observeIndented(): Unit =
545+
if indentSyntax && isNewLine then
546+
val nextWidth = indentWidth(next.offset)
547+
val lastWidth = currentRegion match
553548
case r: Indented => r.width
554549
case r: InBraces => r.width
555550
case _ => nextWidth
556-
}
557551

558-
if (lastWidth < nextWidth
559-
&& !unless.contains(nextToken)
560-
&& (unlessSoftKW.isEmpty || token != IDENTIFIER || name != unlessSoftKW)) {
552+
if lastWidth < nextWidth then
561553
currentRegion = Indented(nextWidth, Set(), COLONEOL, currentRegion)
562-
if (!isNewLine) next.copyFrom(this)
563-
offset = nextOffset
554+
offset = next.offset
564555
token = INDENT
565-
}
566-
}
556+
end observeIndented
567557

568558
/** - Join CASE + CLASS => CASECLASS, CASE + OBJECT => CASEOBJECT, SEMI + ELSE => ELSE, COLON + <EOL> => COLONEOL
569559
* - Insert missing OUTDENTs at EOF

0 commit comments

Comments
 (0)