-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix heredoc start + comma formatter #6222
Conversation
I think the bug is in |
I would ask @makenowjust if there is another solution that can be thought, but otherwise LGTM. Can we add a spec with more than one hash entry with heredocs? |
@asterite Thanks for the advice. It helped a lot 👍 The complexity is not even that much of a problem. I got it to work (mostly) and |
At least this implementation is not complete. Example: {
:foo => <<-FOO ,
foo
FOO
} is formatted to {
:foo => <<-FOO
foo foo
FOO
,
} I agree with @asterite and such a specialization for heredoc is already used in |
Above comment is about old commit. And a new version looks good to me. Sorry 🙇♂️
-- 木曜日, 21 6月 2018, 06:02午後 +09:00 from Johannes Müller notifications@github.com :
…
@asterite Thanks for the advice. It helped a lot 👍
The complexity is not even that much of a problem. I got it to work (mostly) and format_literal_elements certainly a much better place to fix this.
I'm still left with a rather strange indent on the last line, I can't really figure why that is inserted at in finish_list when @wrote_newline is true. It seems only to be an issue when heredocs are involved, but not with a quoted string literal for example.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub , or mute the thread .
|
@straight-shoota When |
@@ -828,17 +828,23 @@ module Crystal | |||
accept element | |||
end | |||
|
|||
element_is_heredoc = !@lexer.heredocs.empty? |
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 think this variable name is not correct. It should be named has_heredoc_in_line
or has_heredoc
as short.
write "," unless last || found_comment | ||
if !found_comment && (!last || element_is_heredoc) | ||
write "," | ||
wrote_comma = 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.
wrote_comma
is assigned here, but it is not reset. So this affects other iterations.
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.
The variable is limited to block scope and won't be copied over between iterations.
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.
Yes. Sorry.
@makenowjust Yes, writing an indent is correct. But it seems that [
<<-EOF,
foo
EOF
]
# without comma:
[
<<-EOF
foo
EOF
]
# quoted string literal
[
"foo",
] |
@straight-shoota I think this patch fixes indent issue: diff --git a/src/compiler/crystal/tools/formatter.cr b/src/compiler/crystal/tools/formatter.cr
index 29d13c5c0..3fb1f89c3 100644
--- a/src/compiler/crystal/tools/formatter.cr
+++ b/src/compiler/crystal/tools/formatter.cr
@@ -623,7 +623,7 @@ module Crystal
next_token
@string_continuation = old_string_continuation
- @indent = old_indent
+ @indent = old_indent unless is_heredoc
false
end |
Awesome! Thank you very much @makenowjust |
c710905
to
7b17907
Compare
Also, feel free to redactor/improvr/destroy the current formatter for better readability. It started simple but grew complex, mainly because the syntax of the language is so wide and complex too. |
7b17907
to
4c63cd3
Compare
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.
What a nice collaboration we end up having here! 💚
Thanks @makenowjust for jumping in and @straight-shoota for dealing with the issue.
assert_format "[\n <<-EOF,\n foo\n EOF\n <<-BAR,\n bar\n BAR\n]" | ||
assert_format "Hash{\n foo => <<-EOF,\n foo\n EOF\n}" | ||
assert_format "Hash{\n foo => <<-EOF,\n foo\n EOF\n bar => <<-BAR,\n bar\n BAR\n}" | ||
assert_format "Hash{\n foo => <<-EOF\n foo\n EOF\n}" |
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.
Is this really right? I think it should append comma. So correct assertion is:
assert_format "Hash{\n foo => <<-EOF\n foo\n EOF\n}", "Hash{\n foo => <<-EOF,\n foo\n EOF\n}"
Because this code:
{
foo: 42
}
is formatted to:
{
foo: 42,
}
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.
Yeah, that's probably better. But it seems a bit more difficult because of the special treatment of newline in next_token
. And it requires changes in a some other examples, that are - currently - explicitly not adding a comma there:
I'd suggest to leave this PR as a fix for the immediate issue.
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 agree with @straight-shoota here. Not adding a comma although is inconsistent with non heredoc hash/array literals, at least is not producing invalid code.
@sdogruyol @makenowjust WDYT?
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.
Okay.
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 think, this is OK 👍
Would greatly appreciate a fix to not appending a comma, or at least an issue so we can pretend we haven't forgotten about it. |
@RX14 precisely this PR does not append a comma in multiline hashes with heredocs, what is missing is the comma to be consistent with other hashes |
@bcardiff I understood that, I just worded it weirdly, should have said |
Fixes #6197
This is not a great fix, but it seems to work. If anyone has a better solution, go ahead.