-
-
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
ECR: suppress newlines #2985
ECR: suppress newlines #2985
Conversation
add support for <% -%> syntax
Do we actually need this to be an explicit token? If so I think I would prefer it at the opening tag, not the closing one. But is there any sane reason to include the newlines of lines containing just whitespace and a non-printing tag? |
No there isn’t. Currently it preserves the whitespace, but not the newline. I guess it probably should nuke that too.
|
Thinking about it some more, retroactively deleting whitespace seems like a chore. Maybe it’s better to forego the whole thing a simply do a final sweep deleting all lines containing zero or more whitespace chars? Michel
|
With explicit token I mean
and so on, no explicit tag to skip it. I just can't come up with a usecase for when you would want lines with just |
I guess you mean why intercept the ‘-‘ char? If so, since Char::Reader doesn’t have a “look_back” method, you’d have to keep track of a previous_char and intercept that inside the ‘%>’ block. I don’t think that is a good idea. Also, the reason for the trailing -%> syntax in eRB is that it can also be used on output nodes to explicitly remove leading whitespace. Which brings back the discussion on how to go about that. Michel |
I'm not talking about your implementation, I'm trying to discuss the ideal behavior of ECR. |
Sorry, I somehow missed your initial reply about the explicit nodes. If we do it like you suggest, we lose some of the fine grained control an explicit character allows. Also, I'm not sure how you would deal with leading whitespace? I like to think eRB has thought this through well enough. |
I agree with @jhass here. Basically, lines that only have a "control" expression should not appear in the output. This is a bit tricky to implement, because you need to consider a few ECR tokens at the same time. I'll try to implement this and send a PR. I also don't see a compelling reason to keep the "old" behaviour of keeping these newlines, but we could add a way to have them back with a different syntax. |
The compelling reason is fine grained control over the output, as well as compatibility with eRB. You are proposing a brute force method that ignores all whitespace significance. Anyway, I am sorry my PR gets treated this way, but best of luck to you guys. |
@beno Relax, we are just discussing things, nothing is set in stone yet :-) Can you provide an example where you'd want to keep newlines of control-only lines? For example: Items:
<% items.each do |item| %>
- <%= item %>
<% end %> Output when items = [1, 2, 3]:
When do you want this behaviour? It's when you suppress control-only lines that you get fine-grained behaviour, not the other way around. I also don't see a real point to preserve compatibility with eRB, because Crystal is not a Ruby implementation. There's also erubies that behaves differently, which ones should we implement? |
This is how eRB provides fine grained control. It is not only about newlines, it is also indentation and modifying output behaviour. What if I want <%= somelongexpressionresultinginfoo -%> -> foobarbaz I'd be forced to jam it all in one line without an explicit token. |
@beno Those are very good reasons. If we implement this, I think we should support trimming both preceding and following indentation in more tokens. The easy way to do it is to store these flags in the tokens themselves, and then when generating the output you'd check these flags and remove the spaces and newlines. I think it's much easier to do it this way then at the lexer level. What do you think? |
I'd also try to avoid invoking |
But don't you have to rethink the way ECR currently works then? How can you reason about the end tag when all you do is process characters one by one? You’d have to be looking at a more object oriented approach when you evaluate the tokens in pairs I feel. If so, I don’t think that is easier. But maybe I am missing something. Michel
|
But you can’t peek twice in a row can you? How does that work? Michel
|
Yes, you need to look at a few tokens at once. But I don't think it's possible to do it just in the lexer, for example if you want to remove preceding space. You just need to peek once and check for |
collect tokens in lines, add token behaviour
Fixed in #2991 |
add support for <% -%> syntax