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

Correct parsing string interpolation with trailing newline #4726

Conversation

makenowjust
Copy link
Contributor

Fixed #4713

@RX14
Copy link
Contributor

RX14 commented Jul 19, 2017

This fixes parsing

puts "foo #{"bar"
}"

to produce foo bar in stdout, however that example still can't be formatted.

If we can come to a quick consensus on how this should be formatted we should include that change in this PR, if it gets to be a lengthy discussion we can fix it later.

To me the above example should format to

puts "foo #{"bar"}"

but i'm not sure what this (and similar) should format to:

puts "foo #{
  "bar"
}"

@RX14
Copy link
Contributor

RX14 commented Jul 19, 2017

Also "Correctly parse ..." is correct English, instead of "Correct to parse ..."

@makenowjust makenowjust changed the title Correct to parse string interpolation with trailing newline Correct parsing string interpolation with trailing newline Jul 19, 2017
@straight-shoota
Copy link
Member

I am not sure if the formatter should remove newlines even in the first case.

@makenowjust
Copy link
Contributor Author

@straight-shoota Even if the first case's newline is intended, we can use backslash + newline escape in this case (and I think we should use it.)

puts "foo #{"bar"}\
"

@makenowjust
Copy link
Contributor Author

@RX14 I can't understand the difference between "to do" and "doing"...

@makenowjust
Copy link
Contributor Author

I considered some ideas to format string interpolation with newline:

Original source code:

"foo #{
  bar = 1
} baz"

Idea 1: keep

"foo #{
  bar = 1
} baz"

Idea 2: align with delimiter

"foo #{
       bar = 1
     } baz"

Idea 3: remove newline and indent

"foo #{bar = 1} baz"

I don't know which is the best and I can't decide it. I guess, the preference of idea 1 is unstable (and I don't like it). Idea 2 is the best if it follows no string, so:

"foo #{
       bar = 1
     }"

this indentation looks like foo = begin ... end pattern, so it is good enough. However when it follows any string, I think it is ugly. Idea 3, crystal tool format disallow newline inside string interpolation in other words, is good for me, because I think we shouldn't write too complex expression to need newline inside string interpolation, but I don't know this is general opinion.

@RX14
Copy link
Contributor

RX14 commented Jul 21, 2017

I think the rule should be that if there's a newline after #{ we do idea 1 (indent 1 indent then format normally), otherwise keep it as-is. That means that the original example formats to

puts "foo #{"bar"}"

but

puts "foo #{
  "bar"
}"

formats as-is.

@makenowjust
Copy link
Contributor Author

Ok. I implemented formatting string interpolation in this way.

@@ -340,6 +340,11 @@ describe Crystal::Formatter do
assert_format "__DIR__", "__DIR__"
assert_format "__LINE__", "__LINE__"

assert_format "\"\#{foo = 1\n}\"", "\"\#{foo = 1}\""
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use %(...) to make the assertion more readable ?

e.g:

assert_format %("\#{foo = 1\n}"), %("\#{foo = 1}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@makenowjust makenowjust force-pushed the fix/crystal/4713-parse-string-interpolation-newline branch from 5b66dd4 to c8e0bbe Compare July 21, 2017 12:52
@mverzilli mverzilli added this to the Next milestone Jul 21, 2017
@mverzilli mverzilli merged commit f4178bc into crystal-lang:master Jul 21, 2017
@mverzilli
Copy link

Thank you @makenowjust!

@makenowjust makenowjust deleted the fix/crystal/4713-parse-string-interpolation-newline branch July 21, 2017 13:59
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

5 participants