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

Correctly detect colon lambda eol indent for optional brace of argument #22477

Merged

Conversation

som-snytt
Copy link
Contributor

@som-snytt som-snytt commented Jan 29, 2025

Fixes #22193

The lookahead scanner used by followingIsLambdaAfterColon would recalculate the indentation width of its top region from the current offset, which is the colon token. That would cause detection of indentation to fail on the next line. This commit preserves the indentation width for that purpose. Subsequent parsing of the function literal proceeds normally.

@som-snytt som-snytt force-pushed the issue/22193-indent-after-colon-arrow branch 2 times, most recently from d8ff0e2 to b8316d8 Compare January 29, 2025 21:44
@som-snytt som-snytt force-pushed the issue/22193-indent-after-colon-arrow branch from b8316d8 to 5ad196e Compare January 29, 2025 21:53
@som-snytt som-snytt changed the title Permit indent at width after colon arrow eol Correctly detect colon lambda eol indent for optional brace of argument Jan 29, 2025
@som-snytt som-snytt marked this pull request as ready for review January 29, 2025 23:32
Copy link

@kitbellew kitbellew left a comment

Choose a reason for hiding this comment

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

fantastic! i would select approve, mainly based on the tests :)

@som-snytt
Copy link
Contributor Author

@mbovel This was tricky to figure out without knowing anything, but turns out to be a one-liner (plus a method) that may be a quick review.

Copy link
Member

@mbovel mbovel left a comment

Choose a reason for hiding this comment

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

I dream of simpler indentation rules. Otherwise looks good to me! 🙂

val nextWidth = indentWidth(next.offset)
val lastWidth = currentRegion.indentWidth
if lastWidth < nextWidth then
currentRegion = Indented(nextWidth, COLONeol, currentRegion)
Copy link
Member

Choose a reason for hiding this comment

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

Is it expected to use COLONeol here? Or could use ARROW instead?

At first sight, it only seems to be used here where it's compared to MATCH.

Copy link
Contributor Author

@som-snytt som-snytt Feb 7, 2025

Choose a reason for hiding this comment

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

My first attempt used ARROW and used it during newline handling, but that was not needed. I changed to colon for uniformity only; the result here is only produced and discarded in lookahead scanner. Actually on my WIP branch, I used a new token ARROWeol for when it is not a proper indent, as after MATCH. (Region prefix is used

|| nextWidth == lastWidth && (indentPrefix == MATCH || indentPrefix == CATCH) && token != CASE then
)

There is experimental (dubious) use of the prefix at https://github.com/scala/scala3/pull/22530/files just to show that the prefix, if observed, could have semantics.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, let's get that merged then!

@@ -680,7 +693,6 @@ object Scanners {
currentRegion = Indented(nextWidth, COLONeol, currentRegion)
offset = next.offset
token = INDENT
end observeIndented
Copy link
Member

Choose a reason for hiding this comment

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

+1, I also don't like these end markers 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the method used to be longer and had a blank line. I must have deleted it during other edits while experimenting, but here it's no longer needed.

@som-snytt som-snytt merged commit f5ba8d6 into scala:main Feb 7, 2025
29 checks passed
@som-snytt som-snytt deleted the issue/22193-indent-after-colon-arrow branch February 7, 2025 17:21
@som-snytt
Copy link
Contributor Author

I ought to have squashed.

@kitbellew
Copy link

thank you both!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fewer braces and lambda: incorrect indentation region detection
3 participants