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

[BUG] Formatting C# code not respecting editorconfig settings

Open arunchndr opened this issue 1 year ago • 2 comments

From vscode-dotnettools created by gmamekudz: microsoft/vscode-dotnettools#886

Describe the Issue

There is no more way to formatting c# code with braces on same line. This is since last vs code update.

Steps To Reproduce

Set up omnisharp.json to

{
	"FormattingOptions": {
		"NewLinesForBracesInLambdaExpressionBody": false,
		"NewLinesForBracesInAnonymousMethods": false,
		"NewLinesForBracesInAnonymousTypes": false,
		"NewLinesForBracesInControlBlocks": false,
		"NewLinesForBracesInTypes": false,
		"NewLinesForBracesInMethods": false,
		"NewLinesForBracesInProperties": false,
		"NewLinesForBracesInObjectCollectionArrayInitializers": false,
		"NewLinesForBracesInAccessors": false,
		"NewLineForElse": false,
		"NewLineForCatch": false,
		"NewLineForFinally": false
	}
}

and/or .editorconfig to

[*.cs]
csharp_new_line_before_open_brace = none
csharp_new_line_before_else = false
csharp_new_line_before_catch = false
csharp_new_line_before_finally = false
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = false

Format c# code.

Expected Behavior

for example:

public static bool BoxIsValidAndActive(RuntimeInfo _ri, long _boxesId) { 
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rdr;

Instead this will be the result:

public static bool BoxIsValidAndActive(RuntimeInfo _ri, long _boxesId) 
{ 
    SqlCommand cmd = new SqlCommand();
    SqlDataReader rdr;

Environment Information

Version: 1.85.2 (system setup) Commit: 8b3775030ed1a69b13e4f4c628c612102e30a681 Datum: 2024-01-18T06:40:10.514Z Electron: 25.9.7 ElectronBuildId: 26354273 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 Betriebssystem: Windows_NT x64 10.0.22631

arunchndr avatar Mar 06 '24 18:03 arunchndr

any update on this? It's been annoying the shit out of me.

ntswamp avatar Jun 20 '24 06:06 ntswamp

I am not able to reproduce this. My .editorconfig contains the following:

root = true

[*.cs]
csharp_new_line_before_open_brace = none

When I run the Format Document command, opening braces are moved from being on their own line to being at the end of the preceding line.

sharwell avatar Jun 26 '24 14:06 sharwell

I am able to reproduce this with "C# Dev Kit" (ms-dotnettools.csdevkit) and "C#" (ms-dotnettools.csharp) both enabled, but the issue does not occur with just "C#" enabled. With the C# Dev Kit extension enabled, the options in the .editorconfig file are ignored.

Penguin-Spy avatar Jul 08 '24 22:07 Penguin-Spy

EditorConfig should be working. If you're still experience this, feel free to open a new issue with details.

mikadumont avatar Aug 27 '24 20:08 mikadumont

I'm experiencing the same issue. Having an .editorconfig file in the root of a Dotnet project works as expected (it formats the curly braces on the same line), but if I have no project setup, it doesn't work.

I mean, having a folder with just an .editorconfig file and a .cs script doesn't seem to work when formatting.

Vasdranna avatar Dec 16 '24 15:12 Vasdranna

@Vasdranna The C# extension requires a project file to be present to startup. Project files are necessary because they tell the extension which .NET framework you are targeting, what NuGet packages you are referencing, etc...

We do have an issue tracking a better loose file experience. See https://github.com/dotnet/vscode-csharp/issues/5729

JoeRobich avatar Dec 16 '24 16:12 JoeRobich

I have the same issue.

My .editorconfig is being ignored when formatting on save but it works when doing dotnet format.

I have a dotnet project setup with dotnet new web.

PierBover avatar Feb 14 '25 17:02 PierBover

@PierBover Please open a new issue and provide the requested logs.

JoeRobich avatar Feb 15 '25 23:02 JoeRobich