Skip to content
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

[Scala 3]Keywords can also end indentation region if on the same line #2384

Merged
merged 1 commit into from
Jun 16, 2021

Conversation

tgodzik
Copy link
Collaborator

@tgodzik tgodzik commented Jun 16, 2021

This follows the additional rules in https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html

The following two additional rules support parsing of legacy code with ad-hoc layout. They might be withdrawn in future language versions:

An <outdent> is also inserted if the next token following a statement sequence starting with an <indent> closes an indentation region, i.e. is one of then, else, do, catch, finally, yield, }, ), ] or case.

Fixes #2379

@@ -358,7 +358,8 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
token.is[KwDo] || token.is[KwCatch] || token.is[KwFinally] || token.is[KwYield] ||
token.is[KwMatch]

if (existingIndent == expected && canEndIndentation(curr.token)) {
// we also insert outdent if correct keyword is on the same line
if ((existingIndent == expected || expected == -1) && canEndIndentation(curr.token)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this additional rule, it seems that calling countIndentAndNewline isn't necessary at all.
It would be enough just to check that we are inside the indented region and keyword can close it.
Smth like:

sepRegions match {
  case region :: tail if region.isIndented && canEndIndetation(curr.token) =>
     // procude outdent
     true
   case _ => false
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ach, you are right. This actually works in more situations that it didn't previously work like:

    val a = if false then
      1
      2 
       else
      3

This follows the additional rules in https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html

```
The following two additional rules support parsing of legacy code with ad-hoc layout. They might be withdrawn in future language versions:

An <outdent> is also inserted if the next token following a statement sequence starting with an <indent> closes an indentation region, i.e. is one of then, else, do, catch, finally, yield, }, ), ] or case.
```

Fixes scalameta#2379
@tgodzik tgodzik force-pushed the fix-then-same-line branch from 968a3e3 to b518235 Compare June 16, 2021 14:07
Copy link
Member

@dos65 dos65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@tgodzik tgodzik merged commit 5a8c527 into scalameta:main Jun 16, 2021
@tgodzik tgodzik deleted the fix-then-same-line branch June 16, 2021 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants