Skip to content

Handle properly stringifying single line blocks#15568

Merged
straight-shoota merged 4 commits intocrystal-lang:masterfrom
Blacksmoke16:block-string-representation
Apr 18, 2025
Merged

Handle properly stringifying single line blocks#15568
straight-shoota merged 4 commits intocrystal-lang:masterfrom
Blacksmoke16:block-string-representation

Conversation

@Blacksmoke16
Copy link
Member

Depends on, and is similar to, #15305 but for Block nodes.

Previously code like:

{%
  [1, 2, 3].each { |v| pp v }
  [1, 2, 3].each do |v|
    pp v
  end
%}

Would always stringify single-line blocks as a do/end block:

{% [1, 2, 3].each do |v|
  pp(v)
end
[1, 2, 3].each do |v|
  pp(v)
end
 %}

But now, in-conjunction with the other PR, results in:

{%
  [1, 2, 3].each { |v| pp(v) }
  [1, 2, 3].each do |v|
    pp(v)
  end
%}

I threw a bunch of test cases at it with the assumption the code may not have had the formatter ran on it and all seems 👍.

Marking as draft and skipping CI. Will rebase/trigger CI once the other dependent PR is merged. But wanted to get this opened for feedback.

@Blacksmoke16 Blacksmoke16 changed the title Block string representation Handle properly stringifying single line blocks Mar 18, 2025
@Blacksmoke16 Blacksmoke16 force-pushed the block-string-representation branch from 61b983e to d7c1550 Compare March 19, 2025 12:51
@Blacksmoke16 Blacksmoke16 marked this pull request as ready for review March 19, 2025 12:51
@straight-shoota straight-shoota added this to the 1.17.0 milestone Apr 17, 2025
@straight-shoota straight-shoota merged commit 75b0cd3 into crystal-lang:master Apr 18, 2025
32 checks passed
@Blacksmoke16 Blacksmoke16 deleted the block-string-representation branch April 18, 2025 13:52
straight-shoota pushed a commit that referenced this pull request Apr 25, 2025
Follow up to #15568 to handle the case of significant whitespace before a block's body.

Previously code like:
```cr
{%
  ({"a" => "b"} of Nil => Nil).each do |k, v|
    # stuff and things
    k + v

    # foo bar

    k + v
  end
%}
```
Would always be stringified as:
```cr
{%
  ({"a" => "b"} of Nil => Nil).each do |k, v|
    k + v



    k + v
  end
%}
```
But now results in:
```cr
{%
  ({"a" => "b"} of Nil => Nil).each do |k, v|
  
    k + v
  
  
  
    k + v
  end
%}
```
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.

2 participants