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

Format multi-line braces blocks using do/end #4517

Merged
merged 2 commits into from Jun 6, 2017

Conversation

RX14
Copy link
Contributor

@RX14 RX14 commented Jun 5, 2017

Fixes #4504.

@@ -2480,11 +2480,13 @@ module Crystal
next_token_skip_space_or_newline
end

if @token.keyword?(:do)
needs_do_block = node.location.try(&.line_number) != node.end_location.try(&.line_number)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not entirely sure this is the correct way to detect this but it seems to work.

Copy link
Member

Choose a reason for hiding this comment

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

I was going to do it using the same logic, so I think it's OK :-)

@asterite
Copy link
Member

asterite commented Jun 6, 2017

I'm merging this because this will effectively end discussions about when to use { ... } and do ... end. Thank you!!

@asterite asterite merged commit abd2dfd into crystal-lang:master Jun 6, 2017
@asterite asterite added this to the Next milestone Jun 6, 2017
@asterite
Copy link
Member

While working on rufo I realized this is wrong.

For example:

foo bar {
  1 + 2
}

Here the block is associated with bar, but converting it to do/end will associate it to foo, which changes the semantic of the program. I'm reverting this.

@asterite asterite removed this from the Next milestone Jun 24, 2017
@RX14
Copy link
Contributor Author

RX14 commented Jun 24, 2017

@asterite I don't think that do/end should have different parsing rules to {}, I should be able to switch between the two freely. I'd even suggest going further and ensuring that you need braces around calls with do/end/braces so that you'd need to do

foo(bar) do
  1 + 2
end

but either of those suggestions can be implemented without the other.

I think it's the fact this changes semantics which should be fixed, not this PR (but reverting it is good temporarily).

@RX14
Copy link
Contributor Author

RX14 commented Jul 6, 2017

I've looked into making a PR for my above suggestion but I'm finding it hard. The crystal parser is very complex and has a lot of accumulated, undocumented state and undocumented abstractions. It's very hard to refactor the parser to take advantage of the added simplicity of having 1 unambiguous parsing rule for blocks.

@RX14
Copy link
Contributor Author

RX14 commented Sep 7, 2017

Perhaps @asterite would have fun unifying the two block syntaxes :). I still think it's a bad idea that do/end and {/} have slightly different semantics, regardless of formatter changes. Ensuring braces around the arguments of functions with blocks seems like a good way to handle this breaking change such that it's always an error when it's an issue.

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

Successfully merging this pull request may close these issues.

None yet

2 participants