Skip to content

Commit 891d9d1

Browse files
committed
Enable significant indentation inside braces.
Also: - Make indentation opt in, controlled by -indent. indent is set as a default option when testing.
1 parent a7845d4 commit 891d9d1

File tree

8 files changed

+205
-114
lines changed

8 files changed

+205
-114
lines changed

compiler/src/dotty/tools/dotc/config/Config.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ object Config {
158158
final val simplifyApplications = true
159159

160160
/** Always assume -indent */
161-
final val allowIndent = true
161+
final val alwaysIndent = false
162162

163163
/** If set, prints a trace of all symbol completions */
164164
final val showCompletions = false

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,11 @@ object Parsers {
606606

607607
/** Parse indentation region `body` and rewrite it to be in braces instead */
608608
def indentedToBraces[T](body: => T): T = {
609-
val indentWidth = in.indent.enclosing.width
609+
val enclRegion = in.currentRegion.enclosing
610+
def indentWidth = enclRegion match {
611+
case Indented(w, _, _, _) => w
612+
case _ => IndentWidth.Zero
613+
}
610614
val followsColon = testChar(in.lastOffset - 1, ':')
611615
val startOpening =
612616
if (followsColon)
@@ -701,10 +705,13 @@ object Parsers {
701705
def bracesToIndented[T](body: => T): T = {
702706
val colonRequired = possibleColonOffset == in.lastOffset
703707
val (startOpening, endOpening) = startingElimRegion(colonRequired)
704-
val isOuterMost = in.sepRegions.isEmpty
708+
val isOutermost = in.currentRegion.isOutermost
705709
val savedPending = pendingPatches
706-
var canRewrite =
707-
in.sepRegions.forall(token => token == RBRACE || token == OUTDENT) && // test (1)
710+
def allBraces(r: Region): Boolean = r match {
711+
case r: InBraces => allBraces(r.enclosing)
712+
case _ => r.isOutermost
713+
}
714+
var canRewrite = allBraces(in.currentRegion) && // test (1)
708715
!testChars(in.lastOffset - 3, " =>") // test(6)
709716
val t = enclosed(LBRACE, {
710717
canRewrite &= in.isAfterLineEnd // test (2)
@@ -726,7 +733,7 @@ object Parsers {
726733
patch(source, Span(startClosing, endClosing), "")
727734
}
728735
pendingPatches = applyPatch :: pendingPatches
729-
if (isOuterMost) {
736+
if (isOutermost) {
730737
pendingPatches.reverse.foreach(_())
731738
pendingPatches = Nil
732739
}
@@ -1168,7 +1175,7 @@ object Parsers {
11681175
}
11691176

11701177
def indentRegion[T](tag: EndMarkerTag)(op: => T): T = {
1171-
val iw = in.indent.width
1178+
val iw = in.currentIndentWidth
11721179
val t = op
11731180
in.consumeEndMarker(tag, iw)
11741181
t
@@ -1282,6 +1289,7 @@ object Parsers {
12821289
}
12831290
else { accept(TLARROW); typ() }
12841291
}
1292+
else if (in.token == INDENT) enclosed(INDENT, typ())
12851293
else infixType()
12861294

12871295
in.token match {
@@ -2152,8 +2160,15 @@ object Parsers {
21522160
def block(): Tree = {
21532161
val stats = blockStatSeq()
21542162
def isExpr(stat: Tree) = !(stat.isDef || stat.isInstanceOf[Import])
2155-
if (stats.nonEmpty && isExpr(stats.last)) Block(stats.init, stats.last)
2156-
else Block(stats, EmptyTree)
2163+
stats match {
2164+
case (stat : Block) :: Nil =>
2165+
stat // A typical case where this happens is creating a block around a region
2166+
// hat is already indented, e.g. something following a =>.
2167+
case _ :: stats1 if isExpr(stats.last) =>
2168+
Block(stats.init, stats.last)
2169+
case _ =>
2170+
Block(stats, EmptyTree)
2171+
}
21572172
}
21582173

21592174
/** Guard ::= if PostfixExpr
@@ -2192,7 +2207,7 @@ object Parsers {
21922207
/** Generator ::= [‘case’] Pattern `<-' Expr
21932208
*/
21942209
def generator(): Tree = {
2195-
val casePat = if (in.token == CASE) { in.skipCASE(); true } else false
2210+
val casePat = if (in.token == CASE) { in.nextToken(); true } else false
21962211
generatorRest(pattern1(), casePat)
21972212
}
21982213

@@ -2302,15 +2317,21 @@ object Parsers {
23022317
* ImplicitCaseClause ::= ‘case’ PatVar [Ascription] [Guard] `=>' Block
23032318
*/
23042319
val caseClause: () => CaseDef = () => atSpan(in.offset) {
2305-
accept(CASE)
2306-
CaseDef(pattern(), guard(), atSpan(accept(ARROW)) { block() })
2320+
val (pat, grd) = inSepRegion(LPAREN, RPAREN) {
2321+
accept(CASE)
2322+
(pattern(), guard())
2323+
}
2324+
CaseDef(pat, grd, atSpan(accept(ARROW)) { block() })
23072325
}
23082326

23092327
/** TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
23102328
*/
23112329
val typeCaseClause: () => CaseDef = () => atSpan(in.offset) {
2312-
accept(CASE)
2313-
CaseDef(infixType(), EmptyTree, atSpan(accept(ARROW)) {
2330+
val pat = inSepRegion(LPAREN, RPAREN) {
2331+
accept(CASE)
2332+
infixType()
2333+
}
2334+
CaseDef(pat, EmptyTree, atSpan(accept(ARROW)) {
23142335
val t = typ()
23152336
if (isStatSep) in.nextToken()
23162337
t
@@ -3246,7 +3267,7 @@ object Parsers {
32463267
*/
32473268
def enumCase(start: Offset, mods: Modifiers): DefTree = {
32483269
val mods1 = mods | EnumCase
3249-
in.skipCASE()
3270+
accept(CASE)
32503271

32513272
atSpan(start, nameStart) {
32523273
val id = termIdent()

0 commit comments

Comments
 (0)