Skip to content

Commit c0479d9

Browse files
committed
Prefer quiet syntax
1 parent 46d0b05 commit c0479d9

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

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

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -537,15 +537,13 @@ object Parsers {
537537
def inBrackets[T](body: => T): T = enclosed(LBRACKET, body)
538538

539539
def inBracesOrIndented[T](body: => T, rewriteWithColon: Boolean = false): T =
540-
if (in.token == INDENT) {
541-
val rewriteToBraces =
542-
in.rewriteNoIndent &&
543-
!testChars(in.lastOffset - 3, " =>") // braces are always optional after `=>` so none should be inserted
544-
if (rewriteToBraces) indentedToBraces(body)
540+
if in.token == INDENT then
541+
val rewriteToBraces = in.rewriteNoIndent
542+
&& !testChars(in.lastOffset - 3, " =>") // braces are always optional after `=>` so none should be inserted
543+
if rewriteToBraces then indentedToBraces(body)
545544
else enclosed(INDENT, body)
546-
}
547545
else
548-
if (in.rewriteToIndent) bracesToIndented(body, rewriteWithColon)
546+
if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)
549547
else inBraces(body)
550548

551549
def inDefScopeBraces[T](body: => T, rewriteWithColon: Boolean = false): T =
@@ -635,68 +633,63 @@ object Parsers {
635633
else idx
636634

637635
/** Parse indentation region `body` and rewrite it to be in braces instead */
638-
def indentedToBraces[T](body: => T): T = {
639-
val enclRegion = in.currentRegion.enclosing
640-
def indentWidth = enclRegion.indentWidth
636+
def indentedToBraces[T](body: => T): T =
637+
val enclRegion = in.currentRegion.enclosing // capture on entry
638+
def indentWidth = enclRegion.indentWidth
641639
val followsColon = testChar(in.lastOffset - 1, ':')
642-
val startOpening =
643-
if (followsColon)
644-
if (testChar(in.lastOffset - 2, ' ')) in.lastOffset - 2
645-
else in.lastOffset - 1
646-
else in.lastOffset
647-
val endOpening = in.lastOffset
648-
649-
val t = enclosed(INDENT, body)
650640

651641
/** Is `expr` a tree that lacks a final `else`? Put such trees in `{...}` to make
652642
* sure we don't accidentally merge them with a following `else`.
653643
*/
654644
def isPartialIf(expr: Tree): Boolean = expr match {
655645
case If(_, _, EmptyTree) => true
656-
case If(_, _, e) => isPartialIf(e)
657-
case _ => false
646+
case If(_, _, e) => isPartialIf(e)
647+
case _ => false
658648
}
659649

660650
/** Is `expr` a (possibly curried) function that has a multi-statement block
661651
* as body? Put such trees in `{...}` since we don't enclose statements following
662652
* a `=>` in braces.
663653
*/
664654
def isBlockFunction[T](expr: T): Boolean = expr match {
665-
case Function(_, body) => isBlockFunction(body)
655+
case Function(_, body) => isBlockFunction(body)
666656
case Block(stats, expr) => stats.nonEmpty || isBlockFunction(expr)
667-
case _ => false
657+
case _ => false
668658
}
669659

670660
/** Start of first line after in.lastOffset that does not have a comment
671661
* at indent width greater than the indent width of the closing brace.
672662
*/
673663
def closingOffset(lineStart: Offset): Offset =
674-
if (in.lineOffset >= 0 && lineStart >= in.lineOffset) in.lineOffset
675-
else {
676-
val candidate = source.nextLine(lineStart)
664+
if in.lineOffset >= 0 && lineStart >= in.lineOffset then in.lineOffset
665+
else
666+
val candidate = source.nextLine(lineStart) // unused
677667
val commentStart = skipBlanks(lineStart)
678-
if (testChar(commentStart, '/') && indentWidth < in.indentWidth(commentStart))
679-
closingOffset(source.nextLine(lineStart))
680-
else
681-
lineStart
682-
}
668+
if testChar(commentStart, '/') && indentWidth < in.indentWidth(commentStart)
669+
then closingOffset(source.nextLine(lineStart))
670+
else lineStart
683671

684672
def needsBraces(t: Any): Boolean = t match {
685673
case Match(EmptyTree, _) => true
686-
case Block(stats, expr) =>
687-
stats.nonEmpty || needsBraces(expr)
688-
case expr: Tree =>
689-
followsColon ||
690-
isPartialIf(expr) && in.token == ELSE ||
691-
isBlockFunction(expr)
692-
case _ => true
693-
}
694-
if (needsBraces(t)) {
674+
case Block(stats, expr) => stats.nonEmpty || needsBraces(expr)
675+
case expr: Tree => followsColon
676+
|| isPartialIf(expr) && in.token == ELSE
677+
|| isBlockFunction(expr)
678+
case _ => true
679+
}
680+
// begin indentedToBraces
681+
val startOpening =
682+
if followsColon then
683+
if testChar(in.lastOffset - 2, ' ') then in.lastOffset - 2
684+
else in.lastOffset - 1
685+
else in.lastOffset
686+
val endOpening = in.lastOffset
687+
val t = enclosed(INDENT, body)
688+
if needsBraces(t) then
695689
patch(source, Span(startOpening, endOpening), " {")
696690
patch(source, Span(closingOffset(source.nextLine(in.lastOffset))), indentWidth.toPrefix ++ "}\n")
697-
}
698691
t
699-
}
692+
end indentedToBraces
700693

701694
/** The region to eliminate when replacing an opening `(` or `{` that ends a line.
702695
* The `(` or `{` is at in.offset.
@@ -1304,12 +1297,11 @@ object Parsers {
13041297
case _: (ForYield | ForDo) => in.token == FOR
13051298
case _ => false
13061299

1307-
def matchesAndSetEnd(last: T): Boolean = {
1300+
def matchesAndSetEnd(last: T): Boolean =
13081301
val didMatch = matches(last)
13091302
if didMatch then
13101303
updateSpanOfLast(last)
13111304
didMatch
1312-
}
13131305

13141306
if in.token == END then
13151307
val start = in.skipToken()

0 commit comments

Comments
 (0)