Allow mixing of tabs and spaces if an explicit tabstop size is set#4303
Allow mixing of tabs and spaces if an explicit tabstop size is set#4303eelco wants to merge 2 commits intojashkenas:masterfrom
Conversation
…abs and spaces for indentation.
|
Mixing tabs and spaces in a single file is a recipe for madness … and then sadness. Let's not add code to support something that is unsupportable, given different people with differently configured editors. |
|
Hi @jashkenas, thanks for replying! :) How would you feel about changing the tab width to something more sensible than 1 (I’d prefer 4), plus making mixed spaces and tabs for indentation an error in the parser? Currently, something like this is valid code: which is definitely a recipe for sadness when seeing it in an editor (without invisibles on) and then trying to align it. Changing the tab width would not fix this (only make it a bit more likely that working code also aligns). I’m happy to do the work and submit a pull request. |
|
We don't want to make it slightly more sensible, because slightly more sensible is still deeply, fundamentally, broken. I would be open to making mixed tabs and spaces a parse error — but I can also imagine that breaking various folks who are doing complicated or sophisticated tricky things. Really, I think that the best behavior is the behavior we currently have:
|
|
I see your point, but that’s not the behavior it currently has, that’s an undocumented expectation of the people that use Coffeescript. I’m often seeing code, especially from beginners (so they might have copy-pasted some code from examples) that mixes tabs and spaces. For them, code not working even though it’s visually aligned is baffling, especially because the errors they’ll get might not make sense. (Try it with the example above, align the 3rd line with spaces and you’ll get “Error on line 3: unexpected :”) While my proposals might have been a bit too radical (or, well, just wrong), I don’t believe this can’t be improved. It would be great to do this in the context of the official version of Coffeescript, to improve the situation for everybody. |
“CoffeeScript just counts tabs and counts spaces -- you can't possibly expect it to guess how wide your text editor is configured to visually show tab characters.”
But, what if you could tell CoffeeScript how wide a tab stop is? Following the Robustness principle I think it would be great if you could instruct CoffeeScript to treat indentation just as it is visually. This of course requires cooperation of the editor / environment or explicit configuration by the user, making this an opt-in feature (we can do this).
I’ve read the discussions about tabs/spaces in a couple of issues and have seen that the sentiment generally is to not allow mixing, although it’s currently not enforced. However, while CoffeeScript is a really nice language to learn to program, the requirement to carefully manage your whitespace is quite unfriendly. The current behavior, where a tab is equivalent to a space in width is imho a poor choice: no editor is configured like that. (Python 2’s behavior of having a tabstop width of 8 makes a bit more sense).
The implementation in this pull request is mostly a ‘proof of concept’, to show what I’m talking about. Having this option would make parsing dependent on a specific option, meaning that some files won’t parse unless the exact right size is passed. A better way to do it (albeit still breaking backwards compatibility) might be to have it set via modeline at the start of the file.
I’m interested to hear from the maintainers and the community if anybody thinks pursuing a feature like this has merit.