Skip to content

Commit 756c205

Browse files
committed
Add new rule to outdentation based on scala/scala3#10331
1 parent 23126cb commit 756c205

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

scalameta/parsers/shared/src/main/scala/scala/meta/internal/parsers/ScalametaParser.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser =>
348348
while ((indentedRegion(sepRegionsProcess)
349349
&& sepRegionsProcess.head.indent > currentIndent && !isLeadingInfixOperator(
350350
curr
351-
)) ||
351+
) && !prev.is[CanContinueOnNextLine]) ||
352352
shouldCloseCaseOnNonCase(sepRegionsProcess)) {
353353
insertOutdent()
354354
// match can start an identation, block but if `match` follows it means it's chaining matches
@@ -880,6 +880,15 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser =>
880880
(token.next.strictNext.is[LineEnd] || token.next.strictNext.is[EOF])
881881
}
882882
}
883+
// then else do catch finally yield match
884+
@classifier
885+
trait CanContinueOnNextLine {
886+
def unapply(token: Token): Boolean = {
887+
token.is[KwThen] || token.is[KwElse] || token.is[KwDo] ||
888+
token.is[KwCatch] || token.is[KwFinally] || token.is[KwYield] ||
889+
token.is[KwMatch]
890+
}
891+
}
883892

884893
@classifier
885894
trait ExprIntro {

tests/shared/src/test/scala/scala/meta/tests/parsers/dotty/SignificantIndentationSuite.scala

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class SignificantIndentationSuite extends BaseDottySuite {
101101
)
102102
}
103103

104-
test("indent-below-not-okay".ignore) {
104+
test("then-no-indent") {
105105
// this test is related to dotty issue: https://github.com/lampepfl/dotty/issues/9790
106106
// It should either assert error during parsing: "illegal start of simple expression"
107107
// Or accept mismatch with parsing rules and parse as 'if (cond) { truep } else {falsep }'
@@ -112,17 +112,41 @@ class SignificantIndentationSuite extends BaseDottySuite {
112112
| else
113113
| falsep
114114
|""".stripMargin
115-
runTestAssert[Stat](code, assertLayout = Some("trait A { def f: Int }"))(
116-
Defn.Trait(
115+
runTestAssert[Stat](
116+
code,
117+
assertLayout = Some(
118+
"""|def fn: Unit = {
119+
| if (cond) truep else falsep
120+
|}
121+
|""".stripMargin
122+
)
123+
)(
124+
Defn.Def(
117125
Nil,
118-
Type.Name("A"),
126+
Term.Name("fn"),
119127
Nil,
120-
Ctor.Primary(Nil, Name(""), Nil),
121-
Template(Nil, Nil, Self(Name(""), None), List(defx))
128+
Nil,
129+
Some(Type.Name("Unit")),
130+
Term.Block(List(Term.If(Term.Name("cond"), Term.Name("truep"), Term.Name("falsep"))))
122131
)
123132
)
124133
}
125134

135+
test("then-no-indent-wrong") {
136+
// this test is related to dotty issue: https://github.com/lampepfl/dotty/issues/9790
137+
// It should either assert error during parsing: "illegal start of simple expression"
138+
// Or accept mismatch with parsing rules and parse as 'if (cond) { truep } else {falsep }'
139+
// Why error is thrown is described in mentioned issue.
140+
val code = """|def fn: Unit =
141+
| if cond then
142+
| truep1
143+
| truep2
144+
| else
145+
| falsep
146+
|""".stripMargin
147+
runTestError[Stat](code, "expected but else found")
148+
}
149+
126150
test("indent-inside-brace-ok") {
127151
val code = """|class X {
128152
| def fx(): Unit =

0 commit comments

Comments
 (0)