dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 672 forks source link

Indentation removed on empty lines when Format Document is used #1980

Open NorthboundGames opened 6 years ago

NorthboundGames commented 6 years ago

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

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.

akshita31 commented 6 years ago

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

NorthboundGames commented 6 years ago

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

DustinCampbell commented 6 years ago

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

NorthboundGames commented 6 years ago

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

DustinCampbell commented 6 years ago

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.

fizzd commented 5 years ago

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.

Magenda commented 5 years ago

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

fizzd commented 5 years ago

@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

SonOfHans commented 5 years ago

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.

eric-vgastel commented 5 years ago

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

radutomy commented 5 years ago

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

davidmilligan commented 5 years ago

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!

tr0q commented 5 years ago

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.

wbplomp commented 4 years ago

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

geekley commented 4 years ago

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.

creativogee commented 4 years ago

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.

nzeman commented 3 years ago

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

Phyyl commented 3 years ago

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

JoeRobich commented 3 years ago

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

paweston70 commented 3 years ago

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

cornytrace commented 2 years ago

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

BachiMjavanadze commented 2 years ago

Please fix it

RandomGgames commented 2 years ago

Please fix!

IXLLEGACYIXL commented 2 years ago

Still doesnt work

acdupont commented 1 year ago

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.

victoraldecoa commented 10 months ago

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

mana-break commented 8 months ago

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

geekley commented 7 months ago

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.

larrysammii commented 7 months ago

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

geewasvoboda commented 7 months ago

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

jr-uk commented 3 months ago

Finally found a decent simple way with this great extension: https://marketplace.visualstudio.com/items?itemName=jemc.vscode-implicit-indent

StoneWolf commented 3 months ago

Finally found a decent simple way with this great extension: https://marketplace.visualstudio.com/items?itemName=jemc.vscode-implicit-indent

Be warned that this extension breaks your undo/redo history. If you undo a bunch of edits then move your caret to an empty line, you'll lose all your redo history.

claybford commented 2 months ago

Any chance this can be prioritized? Bueller?

JoeRobich commented 2 months ago

This is the VSCode issue tracking Smart Indent, https://github.com/microsoft/vscode/issues/16998. This is the VSCode issue tracking Virtual Space, https://github.com/microsoft/vscode/issues/13960.

If the feature is meaningful to you, then be sure to let them know.