trim_trailing_whitespace moves caret
See original GitHub issue( ported from Microsoft/vscode-editorconfig#24 )
Repro’d on editorconfig v0.2.7:
- Enable autosave in VS Code (e.g. in user settings) - great for users who are used to using Git to back up their code.
- Open up a workspace that lacks VS Code workspace settings for
files.trimTrailingWhitespace
. - In the same workspace, create a .editorconfig, setting
trim_trailing_whitespace = true
Type some code with spaces, then rest (wait for autosave delay).
Expected: Caret position remains, trailing whitespace preceding the caret is not trimmed Actual: All trailing whitespace is trimmed, causing the caret to move and the code (whitespace) that was under composition to be deleted.
Note, this caused me to stop using trim_trailing_whitespace
for a while, before I realised that enabling the matching VS Code option overrode the behaviour to be correct. However, figured it was worth reporting so that this could be improved for new users.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:24
- Comments:46 (19 by maintainers)
Top Results From Across the Web
trim_trailing_whitespace in .editorconfig violates the ...
It just removes trailing whitespaces in the current line when you press Enter (and fails to remove whitespaces on an edited line when...
Read more >Remove trailing whitespace on save in IntelliJ IDEA 12
It is super inconvenient to have a caret after the line end, but I want to remove trailing whitespaces from current line too...
Read more >ITextCaret.MoveTo Method (Microsoft.VisualStudio.Text.Editor)
A CaretPosition that contains the valid values of the caret position after the move has occurred. Remarks. This method handles UTF-16 surrogate pairs...
Read more >Thread: [ jEdit-users ] Remove trailing whitespace on save | jEdit
... whitespace" is bad not usable, as it move the caret to the end of the buffer. ... common to want to trim...
Read more >How to trim trailing whitespace in Visual Studio Code ( macOS ...
How to remove trailing spaces in visual studio code, using macOS Big Sur as my operating system. This can be changed on any...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Nice to find this issue
I would love it if you could at least not trim it if there is no text in front of it?
As currently if I want to start by indenting a line and then type my text I have to be faster than my autosave or I will be yanked back to the start. Very unpleasant and currently my trim_trailing_whitespace is commented out because of this.
I’ve done some looking into this and I know why it’s happening. The vscode function
trimTrailingWhitespace
correctly ignores trailing whitespace at all of the cursors that are passed into it. The problem is theeditor.action.trimTrailingWhitespace
command that this extension uses (which is the same as the Ctrl+K Ctrl+X menu command). Internally thatTrimTrailingWhitespaceCommand
object calls thetrimTrailingWhitespace
function and always passes it an empty array of cursors. This behavior makes sense in the context of a menu command, but of course doesn’t make sense when the command is generated by an auto-save.Incidentally The reason the
files.trimTrailingWhitespace
setting doesn’t trim whitespace at the cursor(s) is that it doesn’t use theTrimTrailingWhitespaceCommand
. Instead it callstrimTrailingWhitespace
directly and passes it an array of cursors.The best fix I can think of is to implement vscode #24400, passing along a
reason
ofauto-save
. I’ve implemented this locally and it solves the problem for me. I plan on making a PR for vscode and a PR for this extension.