Skip to content

Allow observing an indent after conditional #22611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ object Parsers {
private val InCase: Region => Region = Scanners.InCase(_)
private val InCond: Region => Region = Scanners.InParens(LPAREN, _)
private val InFor : Region => Region = Scanners.InBraces(_)
private val InOldCond: Region => Region = // old-style Cond to allow indent when InParens, see #22608
case p: Scanners.InParens => Scanners.Indented(p.indentWidth, p.prefix, p)
case r => r

def unimplementedExpr(using Context): Select =
Select(scalaDot(nme.Predef), nme.???)
Expand Down Expand Up @@ -2325,25 +2328,25 @@ object Parsers {
def condExpr(altToken: Token): Tree =
val t: Tree =
if in.token == LPAREN then
var t: Tree = atSpan(in.offset):
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
if in.token != altToken then
if toBeContinued(altToken) then
t = inSepRegion(InCond) {
inSepRegion(InOldCond): // allow inferred NEWLINE for observeIndented below
atSpan(in.offset):
makeTupleOrParens(inParensWithCommas(commaSeparated(exprInParens)))
.pipe: t =>
if in.token == altToken then t
else if toBeContinued(altToken) then
inSepRegion(InCond):
expr1Rest(
postfixExprRest(
simpleExprRest(t, Location.ElseWhere),
Location.ElseWhere),
Location.ElseWhere)
}
else
if rewriteToNewSyntax(t.span) then
dropParensOrBraces(t.span.start, s"${tokenString(altToken)}")
dropParensOrBraces(t.span.start, tokenString(altToken))
in.observeIndented()
return t
t
else if in.isNestedStart then
try expr() finally newLinesOpt()
expr().tap(_ => newLinesOpt())
else
inSepRegion(InCond)(expr())
if rewriteToOldSyntax(t.span.startPos) then revertToParens(t)
Expand Down
48 changes: 48 additions & 0 deletions tests/pos/i22608.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

def f(i: Int) = i
def g(i: Int, j: Int) = i+j

def t =
val y = f(
if (true)// then
val x = 1
5
else 7
)
y

def u(j: Int) =
val y = g(
if (true)// then
val x = 1
5
else 7,
j
)
y

def b(k: Boolean): Int =
f(
if (
k
&& b(!k) > 0
) then 27
else 42
)

def p(b: Boolean) =
import collection.mutable.ListBuffer
val xs, ys = ListBuffer.empty[String]
(if (b)
xs
else
ys) += "hello, world"
(xs.toString, ys.toString)

def q(b: Boolean) =
import collection.mutable.ListBuffer
val xs, ys = ListBuffer.empty[String]
(if (b)
then xs
else ys) += "hello, world"
(xs.toString, ys.toString)
Loading