-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change default namespace associated to a folder when creating new class #5372
Comments
To paraphrase, the request is to be able to mark a folder with metadata to stop it from participating in namespace construction. |
Yep interesting and reasonable request. We don't have the ability to associate metadata with a folder in CPS - other than empty folders, they don't have a item representation, so we'd need to introduce that. |
@davkean You might want to look at this feedback post I created. Admittedly, this was before I realized that ReSharper was doing part of this... So somehow ReSharper is keeping meta data on the folder to determine if the folder name should be part of the namespace. If I add a new class via ReSharper (which I never do) it's honored. If I use VS to add a class, it's not honored (not surprisingly in hindsight). Here is a small screenshot of a folder being selected and ReSharper's added property "Namespace Provider" Now, I am not expecting you to code to ReSharper's standard, but when you start working on this you may let them know so they can take it into account in their code. I would hate to see double folder properties and have to set them both not knowing which one is which. |
ReSharper stores this data in their own resource file, outside of the project file. We would want to store it in the project file directly. For example: <ItemGroup>
<Folder Include="Models\" IsNamespaceProvider="False" />
</ItemGroup> Currently this |
This is useful when we want to add all of our class extensions in an I think the best option would be to allow us to bring up the Properties window and edit a DefaultNameSpace property on the folder. |
@jinujoseph Are you tracking anything similar on your side? |
Agree, this is quite common case when some folder need to be in parent namespace.
|
Any news on this? Is it being considered for a future update? |
This would be really useful! Adding metadata to any C# Project folder to override default folder-structure based namespace with:
And any file added to that folder will have the namespace based on the override when file is created. And this should only apply to new files. If you change the folder's virtual namespace, there should be no sync for existing files with the old namespace. That's the developer's job to rename. This would really help because often, you find yourself wanting to structure (1 or multiple folders deep) files but need them all in the same namespace. It's quite a headache. |
In our case we have a project where we structure functionality under a seperate folder. Like everything for Now each time you add a new class it's being added in the |
In addition to specifying that a folder does not participate in namespace construction, I can see it being useful to also specify the full namespace for a given folder, such that it ignores any project-level namespace and ancestor folder names. |
This is not flexible <ItemGroup>
<Folder Include="Models\" IsNamespaceProvider="False" />
</ItemGroup> Should be <ItemGroup>
<Folder Include="Models\" Namespace="whatever I want or empty" />
</ItemGroup> |
@MhAllan both have merits. Specifying the full namespace on folders is harder to maintain when moving folders or renaming ancestors. |
It should be, If I set a Namespace, that won't change by changing the hierarchy or renaming ancestors. |
@drewnoakes that's the whole point of the request, to break the link between namespace and folder location. |
Indeed. But there are multiple potential implementations, each with strengths and weaknesses. Edit: I can see why my comment could be confusing out of context. I was referring to the idea of specifying the full path on each folder, rather than just opting a particular folder out of namespace construction. |
I like these Examples: 1.Dynamic with folder name: <ItemGroup>
<Folder Include="Models\" Namespace="Example.Sale.Core.[FolderName]" />
</ItemGroup> 2.Static namespace: <ItemGroup>
<Folder Include="Models\" Namespace="Example.Sale.Core.Models" />
</ItemGroup> 3.Default from project namespace: <ItemGroup>
<Folder Include="Models\"/>
</ItemGroup> |
I think * would be better than [FolderName] |
Hi, Any idea on when this will be implemented? |
what do you think about basing the new namespace off whatever the majority of existing files have in that folder? that would avoid the need to store metadata with the folder and is completely automatic. |
IMO, the flexibility of the Handling folder moves, changes, etc would be fairly easy to maintain for such special cases if variables or template placeholders can be provided to refer to the folder name/relative folder path.
Another nice to have would be a way to define/overwrite the namespace pattern for new files at a higher level, like a project wide setting. Something like this maybe?
|
Could this also be done with Pros:
Cons:
|
Editorconfig is a bad idea, because:
|
Yes. Namespace enforcement is a codestyle in csharp. I'll get into the details a bit below, but the only place Long ExplanationI'm assuming we're talking about
A good example is the following code in VB: Imports System
Namespace MyNamespace
Module Program
Sub Main(args As String())
Console.WriteLine(GetType(Program).FullName)
End Sub
End Module
End Namespace with the following vbproj <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>TestVB.Changed</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
</Project> The output of this is A similar C# example looks different: namespace Test
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine(typeof(Program).FullName);
}
}
} with csproj <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>MyNamespace</RootNamespace>
</PropertyGroup>
</Project> The output is Even in the original ask it was pointed out that this would impact new file creation:
Code style is applied on new file creation. That's how we get the default namespace to start with. it also impacts things like file header. Even Enforcement of namespace styling already exists for csharp in editorconfig with |
Example: |
It can't currently, which is why I bring this up. But also it can't be achieved with the current proposal afaik. Current proposal only addresses new files being created. There's no enforcement in csharp as a language that namespaces must be something specific. There's two parts to this:
Root .editorconfig
|
The design here should also factor in how we tool this from within Visual Studio. For folder level constructs, it would feel natural to surface this preference in the Properties pane when the folder in question is focussed in Solution Explorer. The default place to store metadata on folders would be on a |
Maybe it would it would best just to have virtual folders. I also think adding the ability to just nest files would be great. |
Maybe something like this could avoid that maintenance: <Folder Update="Operations/**/" ContributesToNamespace="False" /> We'd need to verify whether MSBuild's globbing can match folders, or only files. |
MSBuild has no representation of folders. VS itself has an item that it uses to represent empty folders (such as |
What about introducing a new project-level property instead? |
I'm not particularly fond of this option. To me, the existing warning is usually good (or in other words, "a good default"), but I want to opt-out of it in a few special situations. A global flag just turns all of it off completely and this can lead into developers messing up a lot of stuff in namespaces. I'd not be completely opposed to a global flag though assuming there exists a way to still configure this in a more granular fashion. |
This feature would be very useful for Unity development. Files in a Unity project have to be inside the |
Continuing from the previous case with I.e. something like: <!-- Everything; all children and grand-children remaining in the current namespace: -->
<Folder Update="Operations/**/" Namespace="." />
<!-- Skipping any contribution to the namespace by the children only: -->
<Folder Update="Operations/*/" Namespace="." />
<!-- Erase the parent namespace for the direct children: -->
<Folder Update="Operations/*/" Namespace="../*" />
<!--
Renames the ConfirmReservation segment to Confirm but keeps all the parents as they were.
I.e. the final namespace is {RootNamespace}.Operations.Confirm
-->
<Folder Update="Operations/ConfirmReservation/" Namespace="./Confirm" />
<!--
Drops everything and just sets a completely new absolute namespace
I.e. AndNow.ForSomething.CompletelyDifferent
-->
<Folder Update="Operations/ConfirmReservation/" Namespace="AndNow.ForSomething.CompletelyDifferent" />
<!--
Or rooting it in front of RootNamespace:
I.e. {RootNamespace}.AndNow.ForSomething.CompletelyDifferent
-->
<Folder Update="Operations/ConfirmReservation/" Namespace="/AndNow.ForSomething.CompletelyDifferent" /> Some of those are probably superfluous, being each other's duals - e.g. skipping the parent itself with |
I want to ask something like this, but google direct me here. |
Such feature would be extremely valuable to us too. Without this feature you usually get a very scattered and highly divided namespace structure. If you manually adjust namespace, you can be sure that the next dev doesn't too the same and you have a fragmented and meaningless namespace structure all over again... :( |
I'm also interested in this feature, as I never thought about this is a ReSharper thing. But sometimes having trouble with created namespaces in new files and infos of wrong namespaces should have made me think about. Anyway, I like the idea of keeping the metadata in project files. There is already a Also to not make it overcomplicated, I would suggest starting with just a feature of similar property As those suggestions are good to enhance it, I would also vote for the Somehow I have two ideas, one looks like rjgotten and would be like <!-- this is default value and adds default namespace-->
<Folder Include="MyFolder\" Namespace="." />
<!-- this does not contribute to namespace and takes parent -->
<Folder Include="MyFolder\" Namespace=".." /> // This behavior is like filesystem, which makes it a bit familiar but confuses maybe with namespaces. My other idea would be: <!-- this is default the value and uses default namespace for this folder -->
<Folder Include="MyFolder\" Namespace=".*" />
<!-- this line will also be removed automatically, if it was empty and a file was added to that folder -->
<!-- this folder does not contribute to namespace and uses parent namespace -->
<Folder Include="MyFolder\" Namespace="." /> So in a further enhancement it could be extended with following: <!-- this is the explicit version of ".*" and will keep the namespace when renaming the folder -->
<Folder Include="MyFolder\" Namespace=".MyFolder" /> //
<!-- in summary, a leading period means a relative namespace -->
<!-- wihtout a leading perion, this will create complete new namespace root for this folder and subfolders -->
<Folder Include="MyFolder\" Namespace="NewFolder" /> For my personal thinking the first step would be already a huge improvement. |
Not being able to specify a default namespace to a folder (therefore impacting child classes) makes some things hard. |
This is a frustrating property of Visual Studio's namespace handling. Your options are basically:
Is there a deal between JetBrains and Microsoft to keep this the status quo to drive sales to resharper? I'm kidding, but c'mon how do we live like this? :P |
@dustinlacewell Because they can afford to (somehow) take years before they add a very basic nothing in the tooling. It's been literally 5 years since this was opened... |
This issue has a corresponding ticket on Developer Community. Please vote and comment there to make sure your voice is heard.
It would be nice if there is a feature to set a default namespace for each folder when creating a new class. For example, if we have the following file structure:
Then if we create
MyClass2
underMyFolder
, the created class will be under theMyProject
namespace instead ofMyProject.MyFolder
.Additional references: https://stackoverflow.com/questions/1317901/change-default-namespace-when-creating-class-in-folder-visual-studio
The text was updated successfully, but these errors were encountered: