dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.85k stars 669 forks source link

Disable Namespace Sync with Folder Name #5603

Open mryan-hpg opened 1 year ago

mryan-hpg commented 1 year ago

Is your feature request related to a problem? Please describe.

I would like a configuration option (omnisharp.json) that disables the syncing/rename of namespace to match the folders.

Describe the solution you would like

Don't rename the namespace in a file on save.

Applicable Scenarios

I have a project where I've got integration and unit tests in the project. I'm using xunit + dependency injection. I am using a different namespace for the test files in order to isolate the DI, as well as filter.

Describe alternatives you've considered

Other than never using format tooling, not sure what alternatives there are.

Additional context

This is annoying to say the least.

filipw commented 1 year ago

can you provide the steps that you are doing? OmniSharp should not rename your namespaces automatically on save out of the box.

victormf2 commented 1 year ago

I am creating a NuGet package using vscode and I want to provide dependency injection features. It would be nice to use namespace Microsoft.Extensions.DependencyInjection, because my users would be able to get autocomplete suggestions when they type services.AddMyService() as it would not be required for them to add using MyPackage;.

What I want is to keep the namespace fix beahvior for most of my files, but disable it for specific ones.

Example

I turn on source fix on save on vscode: settings.json

"editor.codeActionsOnSave": {
  "source.fixAll": true
},
"omnisharp.enableRoslynAnalyzers": true,
"omnisharp.enableEditorConfigSupport": true

I create this file: Extensions/DependencyInjection.cs

namespace Microsoft.Extensions.DependencyInjection
{
  public static class DependencyInjection
  {
    public static void AddMyService(this IServiceCollection services)
    {
      // adding services
    }
  }
}

I save the file, and this is the source fix result:

using Microsoft.Extensions.DependencyInjection;

namespace MyPackage.Extensions
{
  public static class DependencyInjection
  {
    public static void AddMyService(this IServiceCollection services)
    {
      // adding services
    }
  }
}

It would be nice to have this behavior being configurable on a per file basis (like with a comment of disable warning).

mryan-hpg commented 1 year ago

@filipw I simple set editor.formatOnSave to true, and it seems to rename the namespace for the current file...