kakoune: Handle trailing whitespaces consistently in all file types (either don't touch them, or always cleanup)
I noticed that every language in rc
has to deal with trailing white spaces on its own, and ag '(remove|clean).*trailing.*space' --stats
shows that there are currently 38 files that clean white spaces. This is a lot of duplicated logic!
With the same motivation “kak is a code editor, and not arbitrary data editor” as mentioned in #2147, I propose to consider making kak always remove trailing white spaces. A simple snippet below should simplify those 38 files and also remove the handling of trailing white spaces from editorconfig parsing:
hook global BufWritePre .* %{ try %{ execute-keys -draft \%s\h+$<ret>d } }
About this issue
- Original URL
- State: open
- Created 6 years ago
- Reactions: 1
- Comments: 18 (16 by maintainers)
I agree with @lenormf that these hooks don’t really belong with the language support scripts, and I’d be in favor of removing them. Right now doing it unconditionally when leaving insert mode is just too greedy, doing something as simple as pasting an identifier after inserting a newline is frustrating, and doesn’t have an obvious workaround.
I also think they’re sufficiently trivial to let the users set them up as they want (on a binding, before saving or even automatically when leaving insert mode).
Just fyi if you don’t like the automatic removal of whitespaces when exiting insert mode you can now use
set global disabled_hooks .*-trim-indent
Actually the language support scripts I’ve written do not handle trailing whitespace, as I think it’s not the indentation hooks’ purpose to handle that.
Secondly, removing all trailing whitespace by default without the user’s consent is also bad, the editor shouldn’t assume how the user wants a buffer’s contents to be formatted.
Example: I regularly have to fix bugs in code that wasn’t written by me, that has trailing whitespace everywhere (and even LF and CRLF line endings mixed together), if the editor were to arbitrarily fix those issues, I would end up with diffs that are 99% cosmetic changes every time I close the editor. If the user wants proper formatting, they should use
:format
or declare hooks in their configuration.Finally it’s not an issue that some code is duplicated across a few files, as the user is free to load whatever script they want, and factorizing redundant code would make things harder to cherry-pick scripts in
autoload
(c.f. @danr’s answer regarding dependencies).Perhaps adding a common whitespace hook to #4266 might help with this. That way, filetypes could have default trimming behaviour but it could be overridden by editorconfig, or manually.
There is probably no source code that requires trailing whitespaces to be preserved,
git diff -w
and modern code review tools are able to hide whitespace differences, so I don’t think it’s too bad if kakoune would slightly clean code files that you touch. It’s a good practice to not have trailing whitespaces in the code, and cleaning them up is not the same as reformatting the file.Having said that, I’m all for consistency. If kakoune unconditionally adds a missing terminating newline to the file and cleans up trailing whitespaces sometimes, it can just as well cleanup the trailing whitespaces all the time — less people in the world will commit code with such kind of issues (yay). Alternatively don’t mess with trailing whitespaces in all languages and mention on the wiki how to add the hook.
This is just a proposal, and thank you for commenting and providing great arguments. Good point about mixed LF and CRLF line endings, this is another case to consider. In the meantime, I want to investigate now how to write a hook to fix the mixed line endings 😉