-
Notifications
You must be signed in to change notification settings - Fork 16
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
What happens with empty or whitespace only lines? #23
Comments
Looking at other languages:
So, there's uniform agreement that whitespace-only lines are not considered when determining common prefixes (i.e. option 2). I think we should do that. And of the languages I checked, 3/4 empty out whitespace-only lines entirely. My preference would be to go with the majority. |
Thanks for the research! If we clear whitespace-only lines, Option 2 seems acceptable. The mentioned failure case only happens if we don't clear the lines, and they have a different common prefix whitespace, what happens? I find that to be a terrible design, but it's side-stepped entirely be clearing the whole line. |
Kotlin treats spaces and tabs interchangeably, which I really dislike:
And they also do not entirely remove whitespace-only lines. They simply remove up to X number of leading whitespace chars, with X being the least leading whitespace of every line that contains non-whitespace. I think Kotlin has a terrible behavior. Ruby treats a tab as 8 spaces, and removes the least number of spaces from every line. I also hate Ruby's behavior. Java's haven't been implemented yet, so I can't test exactly what's happening. They explicitly mention ignoring and removing all whitespace from whitespace-only lines. They don't specifically mention spaces or tabs, but do say:
Python is the most sensible of all here. They remove all whitespace on whitespace-only lines, and treat spaces and tabs as separate. |
Huh, don't know how I missed that, sorry.
Java's Anyway, from testing locally, it looks like Java treats spaces and tabs as interchangeable, both being a single character. And they do indeed empty out whitespace-only lines. Here I've translated space characters to
produces
|
I think about a fourth: all trailing whitespaces are removed. For instance, the following template ( dedent`
function f(p) {
if (p) {
${1}...
...
}
}
` produces the following string:
Otherwise, I found the first behavior (ignoring empty newlines and taking whitesapce-only lines into account) more consistent than the second behavior (ignoring both). |
When determining the common indentation of all lines, I can think of 3 possible behaviors:
\n\n
newlines) are ignored\n \n
) lines are ignoredMy preference is to ignore only empty lines, it makes the rest of the algorithm easier to reason about. If a lines contains any chars, then it is used when determining the common indentation. The whitespace-only option has a weird failure case where the whitespace line contains a different indentation than the non-whitespace lines. And considering all lines makes it impossible to break content into sections (eg, markdown).
The text was updated successfully, but these errors were encountered: