Skip to content

Commit 5414a87

Browse files
committed
Fixes for setting-controlled colon-at-eol
1 parent b01cd54 commit 5414a87

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,6 @@ object Parsers {
710710
*/
711711
def bracesToIndented[T](body: => T): T = {
712712
val colonRequired = possibleColonOffset == in.lastOffset
713-
if colonRequired && !in.colonSyntax then return body
714713
val (startOpening, endOpening) = startingElimRegion(colonRequired)
715714
val isOutermost = in.currentRegion.isOutermost
716715
def allBraces(r: Region): Boolean = r match {
@@ -729,7 +728,7 @@ object Parsers {
729728
}
730729
})
731730
canRewrite &= (in.isAfterLineEnd || statCtdTokens.contains(in.token)) // test (5)
732-
if (canRewrite) {
731+
if (canRewrite && (!colonRequired || in.colonSyntax)) {
733732
val openingPatchStr =
734733
if (!colonRequired) ""
735734
else if (testChar(startOpening - 1, Chars.isOperatorPart(_))) " :"
@@ -1180,7 +1179,7 @@ object Parsers {
11801179
}
11811180

11821181
def possibleTemplateStart(): Unit = {
1183-
in.observeIndented()
1182+
in.observeIndented(noIndentTemplateTokens, nme.derives)
11841183
newLineOptWhenFollowedBy(LBRACE)
11851184
}
11861185

@@ -1196,9 +1195,6 @@ object Parsers {
11961195
case _ => op
11971196
}
11981197

1199-
def observeIndentedUnlessFollowedBy(followers: BitSet): Unit =
1200-
if !followers.contains(in.token) then in.observeIndented()
1201-
12021198
/* ------------- TYPES ------------------------------------------------------ */
12031199

12041200
/** Same as [[typ]], but if this results in a wildcard it emits a syntax error and
@@ -1601,7 +1597,7 @@ object Parsers {
16011597
in.nextToken()
16021598
}
16031599
else
1604-
observeIndentedUnlessFollowedBy(BitSet(THEN, DO))
1600+
in.observeIndented(noIndentAfterConditionTokens)
16051601
if (rewriteToNewSyntax(t.span))
16061602
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
16071603
t
@@ -2266,7 +2262,7 @@ object Parsers {
22662262
dropParensOrBraces(start, if (in.token == YIELD || in.token == DO) "" else "do")
22672263
}
22682264
}
2269-
observeIndentedUnlessFollowedBy(BitSet(YIELD, DO))
2265+
in.observeIndented(noIndentAfterEnumeratorTokens)
22702266
res
22712267
}
22722268
else {
@@ -3471,11 +3467,10 @@ object Parsers {
34713467
/** TemplateOpt = [Template]
34723468
*/
34733469
def templateOpt(constr: DefDef): Template =
3474-
possibleBracesStart()
3470+
possibleTemplateStart()
34753471
if (in.token == EXTENDS || isIdent(nme.derives))
34763472
template(constr)
34773473
else {
3478-
possibleTemplateStart()
34793474
if (in.isNestedStart) template(constr)
34803475
else Template(constr, Nil, Nil, EmptyValDef, Nil)
34813476
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,23 +535,25 @@ object Scanners {
535535
}
536536
}
537537

538-
def observeIndented(): Unit =
538+
def observeIndented(unless: BitSet, unlessSoftKW: TermName = EmptyTermName): Unit =
539539
if (indentSyntax && isAfterLineEnd && token != INDENT) {
540540
val newLineInserted = token == NEWLINE || token == NEWLINES
541541
val nextOffset = if (newLineInserted) next.offset else offset
542+
val nextToken = if (newLineInserted) next.token else token
542543
val nextWidth = indentWidth(nextOffset)
543544
val lastWidth = currentRegion match {
544545
case r: Indented => r.width
545546
case r: InBraces => r.width
546547
case _ => nextWidth
547548
}
548549

549-
if (lastWidth < nextWidth) {
550+
if lastWidth < nextWidth
551+
&& !unless.contains(nextToken)
552+
&& (unlessSoftKW.isEmpty || token != IDENTIFIER || name != unlessSoftKW) then
550553
currentRegion = Indented(nextWidth, Set(), COLONEOL, currentRegion)
551554
if (!newLineInserted) next.copyFrom(this)
552555
offset = nextOffset
553556
token = INDENT
554-
}
555557
}
556558

557559
/** - Join CASE + CLASS => CASECLASS, CASE + OBJECT => CASEOBJECT, SEMI + ELSE => ELSE, COLON + <EOL> => COLONEOL

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ object Tokens extends TokensCommon {
270270
statCtdTokens | BitSet(COLONEOL, EQUALS, ARROW, LARROW, WHILE, TRY, FOR)
271271
// `if` is excluded because it often comes after `else` which makes for awkward indentation rules
272272

273+
final val noIndentTemplateTokens = BitSet(EXTENDS)
274+
final val noIndentAfterConditionTokens = BitSet(THEN, DO)
275+
final val noIndentAfterEnumeratorTokens = BitSet(YIELD, DO)
276+
273277
final val scala3keywords = BitSet(ENUM, ERASED, GIVEN, IMPLIED)
274278

275279
final val softModifierNames = Set(nme.inline, nme.opaque)

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ trait QuotesAndSplices {
216216
super.transform(tree)
217217
case tdef: TypeDef if tdef.symbol.hasAnnotation(defn.InternalQuoted_patternBindHoleAnnot) =>
218218
transformTypeBindingTypeDef(tdef, typePatBuf)
219-
case tree @ AppliedTypeTree(tpt, args) =>
219+
case tree @ AppliedTypeTree(tpt, args) =>
220220
val args1: List[Tree] = args.zipWithConserve(tpt.tpe.typeParams.map(_.paramVariance)) { (arg, v) =>
221221
arg.tpe match {
222222
case _: TypeBounds => transform(arg)

0 commit comments

Comments
 (0)