vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

Indentation removed on empty lines when Format Document is used

Open NorthboundGames opened this issue 6 years ago • 31 comments

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.

NorthboundGames avatar Jan 25 '18 21:01 NorthboundGames

Possible duplicate of https://github.com/OmniSharp/omnisharp-vscode/issues/1680

akshita31 avatar Jan 25 '18 23:01 akshita31

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

NorthboundGames avatar Jan 26 '18 00:01 NorthboundGames

@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 avatar Jan 26 '18 16:01 DustinCampbell

@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?

NorthboundGames avatar Jan 27 '18 16:01 NorthboundGames

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.

DustinCampbell avatar Jan 29 '18 15:01 DustinCampbell

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.

fizzd avatar Feb 15 '19 14:02 fizzd

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

Magenda avatar Apr 09 '19 13:04 Magenda

@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

fizzd avatar Apr 09 '19 15:04 fizzd

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.

SonOfHans avatar May 10 '19 20:05 SonOfHans

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

eric-vgastel avatar May 14 '19 08:05 eric-vgastel

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

radutomy avatar May 19 '19 17:05 radutomy

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’t 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!

davidmilligan avatar Jun 23 '19 11:06 davidmilligan

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.

tr0q avatar Aug 14 '19 07:08 tr0q

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

wbplomp avatar Dec 01 '19 15:12 wbplomp

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.

geekley avatar Jul 17 '20 06:07 geekley

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.

creativogee avatar Jul 19 '20 23:07 creativogee

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

nzeman avatar Nov 27 '20 02:11 nzeman

I also would like this to be fixed. I really think having "fake" indentation is the way to go.

Phyyl avatar Mar 02 '21 21:03 Phyyl

I encourage everyone interested in this fuctionality to upvote the following vscode issue - https://github.com/microsoft/vscode/issues/13960

JoeRobich avatar Mar 02 '21 21:03 JoeRobich

I encourage everyone interested in this fuctionality to upvote the following vscode issue - microsoft/vscode#13960

But virtual space is not the same thing - this issue highlights that VS Code is formatting out indents on blank lines...

paweston70 avatar Mar 19 '21 11:03 paweston70

This is a very annoying problem, and makes interoperability with Visual Studio almost impossible without changing whitespace back and forth in each commit.

cornytrace avatar Dec 28 '21 19:12 cornytrace

Please fix it

BachiMjavanadze avatar Feb 26 '22 13:02 BachiMjavanadze

Please fix!

RandomGgames avatar Aug 09 '22 05:08 RandomGgames

Still doesnt work

IXLLEGACYIXL avatar Sep 18 '22 20:09 IXLLEGACYIXL

The goal of the whitespace formatter is to remove superfluous whitespace

Is it not arguable that there are reasons to consider indents on empty lines as non superfluous? If it is arguable at all, then there should be a configuration option for it. Forcing one view to be the solution for everyone in all contexts is extremely frustrating.

acdupont avatar Apr 02 '23 14:04 acdupont

Any chance of prioritizing this? It's a very annoying issue, indeed.

victoraldecoa avatar Dec 01 '23 14:12 victoraldecoa

When I click on a blank line in Visual Studio (Mac) the caret automatically gets a virtual indent from the previous line. In vscode the caret doesn't get any indentation and I have to press tab several times. It infuriates me. Make it behave like in Visual Studio

mana-break avatar Feb 22 '24 10:02 mana-break

Is it not arguable that there are reasons to consider indents on empty lines as non superfluous?

Yes, it's awful. When I leave blank space between methods in a class, I want to keep it at that class' indentation, so that it's already right when inserting new code. If you think this space is unnecessary, I disagree, and in fact I think removing it is what's not only unnecessary but also counter-intuitive - unless the user explicitly wants to do it this way.

geekley avatar Mar 04 '24 04:03 geekley

It's been 6 years... I guess I'm just gonna switch editor.

larrysammii avatar Mar 07 '24 16:03 larrysammii

I would also like to see this feature, the auto formatting is basically unusable because of this

geewasvoboda avatar Mar 19 '24 10:03 geewasvoboda