Skip to content

Commit

Permalink
Merge pull request #3123 from kitbellew/3113
Browse files Browse the repository at this point in the history
LazyTokenIterator: don't reset `prev` in `undoIndent`
  • Loading branch information
kitbellew committed May 15, 2023
2 parents 4b43bf3 + 8ca1a99 commit 1de6ce7
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ private[parsers] class LazyTokenIterator private (
def undoIndent(): Unit = {
curr.regions match {
case (region: SepRegionIndented) :: others if curr.token.is[Indentation.Indent] =>
next()
resetCurr(curr.withRegions(curr.regions match {
val ref = getNextTokenRef()
resetCurr(ref.withRegions(ref.regions match {
// deal with region added by `case` in enum after self type
case RegionArrow :: _ => others
// if no region was added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ private[parsers] class TokenRef private (
var next: TokenRef = null
) {
def withRegions(regions: List[SepRegion]): TokenRef =
new TokenRef(regions, token, pos, nextPos, pointPos)
if (regions eq this.regions) this
else new TokenRef(regions, token, pos, nextPos, pointPos)
}

private[parsers] object TokenRef {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ trait BaseDottySuite extends ParseSuite {
final def tpl(stats: List[Stat]): Template = Template(Nil, Nil, slf, stats)
final def tparamval(name: String, tpe: String) =
Term.Param(List(Mod.ValParam()), Term.Name(name), Some(pname(tpe)), None)
final def tparam(name: String, tpe: String = null) =
Term.Param(Nil, Term.Name(name), Option(tpe).map(pname), None)
final def tparam(name: String, tpe: Option[Type] = None): Term.Param =
Term.Param(Nil, Term.Name(name), tpe, None)
final def tparam(name: String, tpe: Type): Term.Param =
tparam(name, Option(tpe))
final def tparam(name: String, tpe: String): Term.Param =
tparam(name, Option(tpe).map(pname))
final def tparamInline(name: String, tpe: String) =
Term.Param(List(Mod.Inline()), Term.Name(name), Some(pname(tpe)), None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2486,4 +2486,76 @@ class SignificantIndentationSuite extends BaseDottySuite {
)

}

test("#3113") {
runTestAssert[Source](
"""|object Hello {
| val fun = () =>
| if (true) {
| new Object { obj =>
| println(toString)
| }
| }
|
| def main(args: Array[String]): Unit = fun()
|}""".stripMargin,
assertLayout = Some(
"""|object Hello {
| val fun = () => if (true) {
| new Object { obj => println(toString) }
| }
| def main(args: Array[String]): Unit = fun()
|}
|""".stripMargin
)
)(
Source(
Defn.Object(
Nil,
tname("Hello"),
Template(
Nil,
Nil,
Self(Name(""), None),
List(
Defn.Val(
Nil,
List(Pat.Var(tname("fun"))),
None,
Term.Function(
Nil,
Term.If(
bool(true),
Term.Block(
Term.NewAnonymous(
Template(
Nil,
List(Init(pname("Object"), Name(""), Nil)),
Self(tname("obj"), None),
List(Term.Apply(tname("println"), List(tname("toString")))),
Nil
)
) :: Nil
),
Lit.Unit(),
Nil
)
)
),
Defn.Def(
Nil,
tname("main"),
Nil,
List(List(tparam("args", Type.Apply(pname("Array"), List(pname("String")))))),
Some(pname("Unit")),
Term.Apply(tname("fun"), Nil)
)
),
Nil
)
) :: Nil
)
)
}

}

0 comments on commit 1de6ce7

Please sign in to comment.