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

Automatically move { opening braces down to a new line when pressing enter

Open apotenza92 opened this issue 1 year ago • 3 comments

Hi all, I've come across other older issues raised to have opening braces such as { automatically move down to the new line once enter is pressed and the cursor is inside the brace. As far as I'm aware these were made before the more modern C# extension was created and are issues with the older Omnisharp LSP.

Describe the solution you would like

A setting for the VSCode C# Extension that allows for automatic moving of opening braces { to a newline. This is the way it works in other C# editors like Visual Studio and Rider.

Applicable Scenarios

In what scenarios would the feature apply?

Currently:

class Car {*cursor here press enter*}

Results in the following after pressing enter:

class Car {
    *cursor here*
}

Proposed { setting enabled would result in the following after pressing enter:

class Car
{
    *cursor here*
}

Describe alternatives you've considered

The extension C# Curly Formatter achieves this but it should be a simple included option in the C# extension. This extension also has some issues conflicting with other extension such as Vim.

apotenza92 avatar Jan 27 '24 06:01 apotenza92

I cant believe this is still an issue in 2024.

astralmaster avatar Jun 07 '24 10:06 astralmaster

I can't believe that such a problem exists at all.

scriptBoris avatar Jun 21 '24 08:06 scriptBoris

I can't believe that such a problem exists for 7 years without a fix.

ghost avatar Sep 04 '24 09:09 ghost

Anyone got a solution by any chance it triggers me alot...

Minihacker3k avatar Oct 30 '24 20:10 Minihacker3k

@ryzngard can we please mark this as a feature request as well?

achobanov avatar Nov 16 '24 14:11 achobanov

Thanks for the ping @achobanov , idk why this wasn't marked as untriaged :/ . Updated the labels

ryzngard avatar Nov 18 '24 00:11 ryzngard

Bruh why. This is annoying that this exists. I was wondering why my brackets weren't going on a new line. I'm pretty sure brackets on new lines is supposed to be a csharp code formatting standard.

Consumedgrub2 avatar Dec 21 '24 13:12 Consumedgrub2

There seems to be some similarities between this issue and https://github.com/dotnet/razor/issues/11266, though the other issue is specifically VS and this is for VS Code.

phil-allen-msft avatar Jan 06 '25 22:01 phil-allen-msft

I’m genuinely astonished that this is an issue. How is this even possible? This is a fundamental aspect of the C# standard, and yet even Microsoft, who established this standard, has introduced a bug like this. It’s baffling. With problems like this, I can’t understand how anyone could rely on VS Code for C# development.

david-siegert avatar Jan 21 '25 15:01 david-siegert

We're almost at the 1 year anniversary of this now. Any idea from people in the know if this could become a thing? @achobanov @ryzngard. Appreciate your efforts in responding in the first place.

apotenza92 avatar Jan 22 '25 12:01 apotenza92

A change to format on type has been made to the Roslyn LSP. It will flow into the C# extension later this week and be part of a future pre-release. Do note that format on type will need to be enabled.

JoeRobich avatar Jan 23 '25 04:01 JoeRobich

yeah, how to disable this? It didn't do anything and now it started working and I don't see an option to disable it

LimeShep avatar May 15 '25 08:05 LimeShep

yeah, how to disable this? It didn't do anything and now it started working and I don't see an option to disable it

@LimeShep you can override those settings in your settings.json (ctrl+shift+p -> Open user settings (JSON)):

"[csharp]": {
	"editor.formatOnType": false
}

vegmuffin avatar May 17 '25 08:05 vegmuffin

To help everyone get to the right place because this whole process is almost abusive, you want to use...

  1. FILE => PREFERENCES => SETTINGS.
  2. In the search box, type "@lang:csharp" for just the C# language preferences.
  3. Find "Editor › Language: Brackets"
  4. There's a link there for "Edit in settings.json"
  5. Click to open that file
  6. Look for " "[csharp]": { ... } " or create one.
  7. Add " "editor.formatOnType": false " to that list.

exeSpeak avatar Aug 21 '25 01:08 exeSpeak

For more recent (version 2.93.22); set "dotnet.autoInsert.enableAutoInsert": false in the settings.json. This is also found as Enable Auto Insert under the C# Extension. Why this causes the bracket to go on a separate line baffles me; its definition says "Enable automatic insertion of documentation comments" which is in no relation to curly brackets

Hershbja avatar Nov 04 '25 02:11 Hershbja

For more recent (version 2.93.22); set "dotnet.autoInsert.enableAutoInsert": false in the settings.json. This is also found as Enable Auto Insert under the C# Extension. Why this causes the bracket to go on a separate line baffles me; its definition says "Enable automatic insertion of documentation comments" which is in no relation to curly brackets

this kind of works. but if you have your cursor on the same line as the closing bracket and press enter, the opening bracket shifts down to a new line again :(

edit I think I solved it. I did the following:

  1. Navigate to Preferences -> Settings -> Extensions -> C#
  2. Turn off Enable Auto Insert. This disables placing opening bracket on a new line when pressing enter. But issue still remains where opening bracket goes onto new line again when pressing enter on the closing bracket.
  3. Restart editor.
  4. Navigate to Preferences -> Settings -> Extensions -> C#
  5. Turn off CSharp Format: Enable
  6. Turn off Omnisharp: Use Editor Formatting Settings
  7. Turn off Razor Format: Enable
  8. Restart editor.
  9. Everything should be resolved now. You can re-enable steps 5-7 if needed, and the issue should stay gone. Otherwise, just leave them off. I'm not sure which one actually solved the issue.

yosimba2000 avatar Nov 10 '25 20:11 yosimba2000