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
Don't create block on indentation with a single statement #2183
Conversation
@@ -2087,42 +2087,55 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser => | |||
atPos(t, auto)(Term.Match(t, inBracesOrNil(caseClauses()))) | |||
} | |||
} | |||
|
|||
def ifClause(isInline: Boolean = false) = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is moved out from expr
// FIXME: when parsing `(2 + 3)`, do we want the ApplyInfix's position to include parentheses? | ||
// if yes, then nothing has to change here | ||
// if no, we need eschew autoPos here, because it forces those parentheses on the result of calling prefixExpr | ||
// see https://github.com/scalameta/scalameta/issues/1083 and https://github.com/scalameta/scalameta/issues/1223 | ||
def expr(location: Location, allowRepeated: Boolean): Term = | ||
autoPos(token match { | ||
case SkInline() if ahead(token.is[KwIf]) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out there was one more issue with ifs, so a much safer place to check it is here.
The problem was with
def a = inline if cond then a
} | ||
case KwIf() if mods.size > 0 => | ||
syntaxError("If can only contain inline modifier", at = token.pos) | ||
ifClause(isInline = true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the additional case with non-inline mods - it will be reported anyway as error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 👍
After working a bit on scalafmt it seems that most problematic is the case with a single statement, which now is sometimes be treated as an indented block. So reformatting could sometimes cause a new block to be created even if there was none before.