Omnisharp-vscode: Indentation removed on empty lines when Format Document is used

Created on 25 Jan 2018  路  17Comments  路  Source: OmniSharp/omnisharp-vscode

Imgur

VS Code version: Code 1.19.2 (490ef761b76b3f3b3832eff7a588aac891e5fe80, 2018-01-10T15:55:03.538Z)
C# Extension version: 1.13.1

Steps to reproduce

Make multiple new lines in an indented code block, then

  • right click the workspace and choose "Format Document"
  • use FormatOnType, FormatOnPaste, etc., to automatically format the document
  • press Ctrl-Shift-I to auto indent document

Expected behavior

All lines retain their indentation

Actual behavior

Any lines in the document without code have their indentation removed

I have narrowed this down to this plugin, as this behavior isn't seen in VSCode when extension is disabled. I have tried every combination of white space trimming, etc., with no solution.

See Reddit thread.

Most helpful comment

I would like to have the option to _not_ have whitespace removed on empty lines, regardless of where the caret is located. I frequently leave empty lines between code blocks so as to make my code more readable, oftentimes adding additional code in between these blocks at a later time. Having to re-tab a bunch of times to start that new code is definitely not preferred. This was a feature in VS, and I miss it greatly in VS Code.

All 17 comments

See text highlighting at end of attached gif, doesn't appear to be caret positioning but actual deletion of the spaces.

@poodle-chowder : The goal of the whitespace formatter is to remove superfluous whitespace. So, the behavior that you're seeing where unnecessary indentation at the starts of lines is removed is working as designed. I suspect that the real issue here is really the same as #1680, where the indentation is removed on the line where the caret appears, shifting it to the left.

@DustinCampbell Understood. I think the heart of the issue is that whether omnisharp.useEditorFormattingSettings is turned on or off, editor whitespace trimming settings are ignored. With the omnisharp extension disabled, these settings are honored and I don't have the indentation issue.

VS does not seem to consider the indents leading up to a block's indentation level to be superfluous, but VSCode formats to 0 indent even in the middle of a code block. This seems like odd behavior to me. Is the agenda to keep workflow consistent between the two?

Not exactly. The intent is to keep the formatting similar _for C#_ between Visual Studio and Visual Studio Code. C#'s formatter has always removed extra indentation, but there's special behavior on the line that the caret is on.

Is there any alternative to format documents without removing whitespace on empty lines now? Or even just a 'format without removing whitespace anywhere at all'? I have tried a lot of solutions and disabling/enabling whitespace trimming settings, but whitespace on blank lines is always deleted.

I've also tried everything but nothing works. Any ideas?

@Magenda Right now i have just accepted writing lines ugly, and then using ctrl-s to immediately format them and put them back at the right position again

I would like to have the option to _not_ have whitespace removed on empty lines, regardless of where the caret is located. I frequently leave empty lines between code blocks so as to make my code more readable, oftentimes adding additional code in between these blocks at a later time. Having to re-tab a bunch of times to start that new code is definitely not preferred. This was a feature in VS, and I miss it greatly in VS Code.

I've been looking for a solution for weeks. It's driving me literally insane. Please fix this.

I, too, would like a solution to this. It's very, very annoying, especially when combined with Vim.

This bug is infuriating, and nothing has been done about it for years?! The people who maintain this project must not actually use it for real-world, day-to-day work. I don鈥檛 see how anyone can stand this behavior. It seems like this issue has just been blamed on some other issue and so nothing is being done about it. Unacceptable people!

I just checked Visual Studio, indeed it deletes whitespaces, it's just it keeps caret on correct indentation level. So it's indeed not an extension fault but VSCode itself.

I like empty lines to be removed (with format document) while editing JSON files. Now for some reason this has behavior has been removed.

This wouldn't be an issue if VSCode had a behavior similar to IntelliJ/Android Studio.
When you go on a blank line (not indented) and press END, it "pretends" to auto-add the whitespace for the correct indentation, and after you type something, it actually adds it for you. So, even though the whitespace is trimmed on blank lines, it's not an annoyance.

It's 2020 and there is still no fix to this bug as i am currently experiencing the same issue. I can't believe it. This is literally driving me nuts. One moment it was working the next I know it wasn't.

Any news on this? I cannot switch to VSCode with this. It would drive me insane.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slevengood picture slevengood  路  3Comments

visten picture visten  路  4Comments

ZeldaZocker picture ZeldaZocker  路  3Comments

hamhub7 picture hamhub7  路  3Comments

ghost picture ghost  路  3Comments