madskristensen / AddAnyFile

A Visual Studio extension
Apache License 2.0
263 stars 122 forks source link

[Request] When creating a C# file, use file-scoped namespace declaration if possible #90

Closed ryanbuening closed 2 years ago

ryanbuening commented 3 years ago

In C# 10 we can now use a new form of the namespace declaration to declare that all subsequent declarations are members of the declared namespace:

namespace MyNamespace;

Is this extension able to detect the version of C# being used and use this new syntax?

nathan130200 commented 3 years ago

up

Update

After i read this extension source code, i've noticed that we can modify templates just:

Example: image

Content of .cs.txt that extension will read:

namespace {namespace};

public class {itemname}
{
        $
}

Same also for .cs-interface.txt

fl3pp commented 3 years ago

I feel like this feature should have been documented somewhere on the readme. I've been using this extension for quite some time, and this would have come in handy more than once.

kelps commented 2 years ago

I understand we can change the templates, but that is not a great solution because file scoped namespaces are only supported starting with .net 6. If not all your the projects in your organization are .net 6, changing the template will cause problems when working on pre-net6 projects.

On the other hand, to be able to "properly" do this, a simple template isn't enough, because if you want to detect and respect this setting, you should also detect and respect other style settings, both in .editorconfig and in VS.

Adding a file with the stock "Add > New item..." menu in VS2022 does apply and respect your settings. I was thinking that one "simple" solution in this case would be to run "dotnet format style" for that file, but I just tried and it only works for solutions and projects, not individual files. I REALLY like this extension and I use it a LOT. It would be excellent if this feature was implemented...

nathan130200 commented 2 years ago

I agree also, specially with WinForms templates.

timheuer commented 2 years ago

FYI for this item, having a rule/editorconfig in the project should run when adding new items. That way the project owner can determine the style. Right now AddAnyFile does not trigger running the rules upon add (e.g., if you add a new class using the built-in template it works), so talking w/Mads and Roslyn team about doing this correctly.

timheuer commented 2 years ago

I understand we can change the templates, but that is not a great solution because file scoped namespaces are only supported starting with .net 6. If not all your the projects in your organization are .net 6, changing the template will cause problems when working on pre-net6 projects.

One clarification, this is not a .NET6 thing, but a C#9 thing, but still agree with @kelps that changing the base item templates is not ideal.

On the other hand, to be able to "properly" do this, a simple template isn't enough, because if you want to detect and respect this setting, you should also detect and respect other style settings, both in .editorconfig and in VS.

Adding a file with the stock "Add > New item..." menu in VS2022 does apply and respect your settings. I was thinking that one "simple" solution in this case would be to run "dotnet format style" for that file, but I just tried and it only works for solutions and projects, not individual files. I REALLY like this extension and I use it a LOT. It would be excellent if this feature was implemented...

Just added a draft PR #101 to accommodate/try to fix this.

kelps commented 2 years ago

Ok, I am impressed! Looks like a very simple fix. I really hope there are no side effects. There are 3 extensions I use almost every day and this is one of them.

The other ones are Open Command Line [ Alt + Space ] and Extension Manager. In my view, all 3 should already by built-in features in VS.

timheuer commented 2 years ago

If interested in this capability see the PR #101 to follow the scenarios kind of breaking with this change. Might be an issue with Roslyn or VS -- not sure yet, but tracking the issue there. Indeed subfolders and caret position are affected with this behavior.

timheuer commented 2 years ago

If anyone wants to kick the change around -- fixed the project issue and the PR is updated if you want to build/run it with the change.

timheuer commented 2 years ago

Realized a build from this is available: https://ci.appveyor.com/project/madskristensen/addanyfile/builds/41895811/artifacts

ryanbuening commented 2 years ago

The build from appveyor seems to be working for me.

The only thing that I would change is not including the System usings (when creating a class, interface, etc.) as implicit usings are turned on by default now in .NET 6, but that is probably a different topic.

Thanks!

madskristensen commented 2 years ago

This should now work and you can help test it using the latest CI build.

timheuer commented 2 years ago

The only thing that I would change is not including the System usings (when creating a class, interface, etc.) as implicit usings are turned on by default now in .NET 6, but that is probably a different topic.

Yeah I wouldn't change that. Item templates aren't versioned specific so it's better to rely on editorconfig/refactoring/code-cleanpu post-rules IMO.

kelps commented 2 years ago

You guys are amazing. I'll try it tomorrow.

tiesmaster commented 2 years ago

@madskristensen I've been using the CI build for the last couple of days, and it's working like a charm. Really amazing man, thank you!