question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

trim_trailing_whitespace moves caret

See original GitHub issue

( ported from Microsoft/vscode-editorconfig#24 )

Repro’d on editorconfig v0.2.7:

  1. Enable autosave in VS Code (e.g. in user settings) - great for users who are used to using Git to back up their code.
  2. Open up a workspace that lacks VS Code workspace settings for files.trimTrailingWhitespace.
  3. 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:closed
  • Created 7 years ago
  • Reactions:24
  • Comments:46 (19 by maintainers)

github_iconTop GitHub Comments

6reactions
NinoFloriscommented, May 11, 2016

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.

3reactions
dgileadicommented, Oct 7, 2017

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 the editor.action.trimTrailingWhitespace command that this extension uses (which is the same as the Ctrl+K Ctrl+X menu command). Internally that TrimTrailingWhitespaceCommand object calls the trimTrailingWhitespace 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 the TrimTrailingWhitespaceCommand. Instead it calls trimTrailingWhitespace directly and passes it an array of cursors.

The best fix I can think of is to implement vscode #24400, passing along a reason of auto-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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found