Skip to content

Commit e58013f

Browse files
committed
Fix indent after self type
1 parent fbf6ed4 commit e58013f

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4278,7 +4278,7 @@ object Parsers {
42784278
nextToken()
42794279
else
42804280
syntaxError(em"`=>` expected after self type")
4281-
indentedRegionAfterArrow(makeSelfDef(selfName, selfTpt))
4281+
makeSelfDef(selfName, selfTpt)
42824282
}
42834283
else EmptyValDef
42844284

@@ -4295,23 +4295,28 @@ object Parsers {
42954295
*/
42964296
def templateStatSeq(): (ValDef, List[Tree]) = checkNoEscapingPlaceholders {
42974297
val stats = new ListBuffer[Tree]
4298+
val startsAfterLineEnd = in.isAfterLineEnd
42984299
val self = selfType()
4299-
while
4300-
var empty = false
4301-
if (in.token == IMPORT)
4302-
stats ++= importClause()
4303-
else if (in.token == EXPORT)
4304-
stats ++= exportClause()
4305-
else if isIdent(nme.extension) && followingIsExtension() then
4306-
stats += extension()
4307-
else if (isDefIntro(modifierTokensOrCase))
4308-
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
4309-
else if (isExprIntro)
4310-
stats += expr1(inStatSeq = true)
4311-
else
4312-
empty = true
4313-
statSepOrEnd(stats, noPrevStat = empty)
4314-
do ()
4300+
def loop =
4301+
while
4302+
var empty = false
4303+
if (in.token == IMPORT)
4304+
stats ++= importClause()
4305+
else if (in.token == EXPORT)
4306+
stats ++= exportClause()
4307+
else if isIdent(nme.extension) && followingIsExtension() then
4308+
stats += extension()
4309+
else if (isDefIntro(modifierTokensOrCase))
4310+
stats +++= defOrDcl(in.offset, defAnnotsMods(modifierTokens))
4311+
else if (isExprIntro)
4312+
stats += expr1(inStatSeq = true)
4313+
else
4314+
empty = true
4315+
statSepOrEnd(stats, noPrevStat = empty)
4316+
do ()
4317+
if self != null && !startsAfterLineEnd then
4318+
indentedRegionAfterArrow(loop)
4319+
else loop
43154320
(self, if stats.isEmpty then List(EmptyTree) else stats.toList)
43164321
}
43174322

tests/rewrites/indent-rewrite.check

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// A collection of patterns/bugs found when rewriting the community build to indent
22

3-
trait A:
3+
trait C1:
44

5-
class B
5+
class CC1
66
// do not remove braces if empty region
7-
class C {
7+
class CC2 {
88

99
}
1010
// do not remove braces if open brace is not followed by new line
@@ -213,3 +213,14 @@ do not indent in a multiline string"""
213213
given String = "bar"
214214
m18
215215
}
216+
217+
// indent template after self type
218+
class C2 { self =>
219+
val x = ""
220+
}
221+
trait C3:
222+
self =>
223+
val x = ""
224+
case class C4():
225+
self =>
226+
val y = ""

tests/rewrites/indent-rewrite.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// A collection of patterns/bugs found when rewriting the community build to indent
22

3-
trait A {
3+
trait C1 {
44

5-
class B
5+
class CC1
66
// do not remove braces if empty region
7-
class C {
7+
class CC2 {
88

99
}
1010
// do not remove braces if open brace is not followed by new line
@@ -241,3 +241,16 @@ m10 { 5 } {
241241
}
242242
}
243243
}
244+
245+
// indent template after self type
246+
class C2 { self =>
247+
val x = ""
248+
}
249+
trait C3 {
250+
self =>
251+
val x = ""
252+
}
253+
case class C4() {
254+
self =>
255+
val y = ""
256+
}

0 commit comments

Comments
 (0)