Skip to content

Commit a612dbe

Browse files
oderskysmarter
authored andcommitted
Fixes for setting-controlled colon-at-eol
1 parent c2fc7da commit a612dbe

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
@@ -717,7 +717,6 @@ object Parsers {
717717
*/
718718
def bracesToIndented[T](body: => T): T = {
719719
val colonRequired = possibleColonOffset == in.lastOffset
720-
if colonRequired && !in.colonSyntax then return body
721720
val (startOpening, endOpening) = startingElimRegion(colonRequired)
722721
val isOutermost = in.currentRegion.isOutermost
723722
def allBraces(r: Region): Boolean = r match {
@@ -736,7 +735,7 @@ object Parsers {
736735
}
737736
})
738737
canRewrite &= (in.isAfterLineEnd || statCtdTokens.contains(in.token)) // test (5)
739-
if (canRewrite) {
738+
if (canRewrite && (!colonRequired || in.colonSyntax)) {
740739
val openingPatchStr =
741740
if (!colonRequired) ""
742741
else if (testChar(startOpening - 1, Chars.isOperatorPart(_))) " :"
@@ -1219,7 +1218,7 @@ object Parsers {
12191218
}
12201219

12211220
def possibleTemplateStart(): Unit = {
1222-
in.observeIndented()
1221+
in.observeIndented(noIndentTemplateTokens, nme.derives)
12231222
newLineOptWhenFollowedBy(LBRACE)
12241223
}
12251224

@@ -1235,9 +1234,6 @@ object Parsers {
12351234
case _ => op
12361235
}
12371236

1238-
def observeIndentedUnlessFollowedBy(followers: BitSet): Unit =
1239-
if !followers.contains(in.token) then in.observeIndented()
1240-
12411237
/* ------------- TYPES ------------------------------------------------------ */
12421238

12431239
/** Same as [[typ]], but if this results in a wildcard it emits a syntax error and
@@ -1641,7 +1637,7 @@ object Parsers {
16411637
in.nextToken()
16421638
}
16431639
else
1644-
observeIndentedUnlessFollowedBy(BitSet(THEN, DO))
1640+
in.observeIndented(noIndentAfterConditionTokens)
16451641
if (rewriteToNewSyntax(t.span))
16461642
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
16471643
t
@@ -2302,7 +2298,7 @@ object Parsers {
23022298
dropParensOrBraces(start, if (in.token == YIELD || in.token == DO) "" else "do")
23032299
}
23042300
}
2305-
observeIndentedUnlessFollowedBy(BitSet(YIELD, DO))
2301+
in.observeIndented(noIndentAfterEnumeratorTokens)
23062302
res
23072303
}
23082304
else {
@@ -3547,11 +3543,10 @@ object Parsers {
35473543
/** TemplateOpt = [Template]
35483544
*/
35493545
def templateOpt(constr: DefDef): Template =
3550-
possibleBracesStart()
3546+
possibleTemplateStart()
35513547
if (in.token == EXTENDS || isIdent(nme.derives))
35523548
template(constr)
35533549
else {
3554-
possibleTemplateStart()
35553550
if (in.isNestedStart) template(constr)
35563551
else Template(constr, Nil, Nil, EmptyValDef, Nil)
35573552
}

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
@@ -281,6 +281,10 @@ object Tokens extends TokensCommon {
281281
*/
282282
final val startParamOrGivenTypeTokens: BitSet = startParamTokens | BitSet(GIVEN, ERASED)
283283

284+
final val noIndentTemplateTokens = BitSet(EXTENDS)
285+
final val noIndentAfterConditionTokens = BitSet(THEN, DO)
286+
final val noIndentAfterEnumeratorTokens = BitSet(YIELD, DO)
287+
284288
final val scala3keywords = BitSet(ENUM, ERASED, GIVEN, IMPLIED)
285289

286290
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)