14

I am using Visual Stuido 2022 to code my C# project.

Is there a way to configure VS using (.editorconfig file) where a new line is added before and after the namespace?

So my class will look like this

using System;

namespace ProjectName.Tests;

public class Test
{
    
}

instead of

using System;
namespace ProjectName.Tests;
public class Test
{
    
}

Kit
  • 20,354
  • 4
  • 60
  • 103
Jay
  • 1,168
  • 13
  • 41

3 Answers3

2

I'm not sure there is a Visual Studio native way of doing this.

There is definitely not a way to do this in .editorconfig with Visual Studio alone (meaning no plugins). About halfway down Namespace declaration preferences, it talks about csharp_style_namespace_declarations, and the code formatting sample when that value is file_scoped looks like

// csharp_style_namespace_declarations = file_scoped
using System;

namespace Convention;
class C
{
}

which appears to get you part of the way there (blank line after using). When you look at the supported formatting rules, the list is pretty brief.

If you have ReSharper there is a way. These settings in .editorconfig will do what you want:

resharper_blank_lines_after_file_scoped_namespace_directive = 1
resharper_blank_lines_after_imports = 1

If ReSharper is not an option, here are 3 possible paths to take, none all that great. They certainly aren't simple solutions.

Kit
  • 20,354
  • 4
  • 60
  • 103
  • 1
    Regarding the last point, [a github issue #67400 at Roslyn repo](https://github.com/dotnet/roslyn/issues/67400) already exists based on Developer Community feedback. – Zdeněk Jelínek Aug 01 '23 at 14:50
1

StyleCop.Analyzers has a bunch of formatting related rules that you can enforce and, in some cases, automatically fix across your codebase.

Unfortunately it looks as though there is not yet support for file-scoped namespace formatting in the way you want. There's a PR to add it here, so if you really wanted to have this you could get that PR across the finish line, or make your own build:

https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3512

This issue might also be worth a look:

https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3578

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
0

Like the other guy said,

No, not with editor config (out of the box).

Resharper can do this; you can build a custom format for your code and tell it, for example, where you want certain sections like imports below the class for some weird reason ;) ...of course, with that spacing around those sections.

But now you have to get ReSharper licenses for all your devs. The Resharper devs are very cool, and Jetbrains has been building quality software for a few decades. Maybe I'm sentimental.

Well, there is another option if you don't want to pay... In DevOps roles of the past, I've used Roslyn to build extensions for custom formatting.

Build a custom Rosyln analyzer. https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview?view=vs-2022#severity-levels-of-analyzers

You can build a custom code suggestion with this of configurable severity.

It's pretty easy, too, thanks to Rosyln, but you must think a little backward. Let me know if I can help more.

Paul Wade
  • 591
  • 5
  • 17