-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Support refactoring to Sync Namespace and Folder Name #14341
Comments
see comments here: #18147 |
Proposed design for V1 (C#) This will be triggered when cursor is moved to the namespace declaration, or first member declaration if everything in the document is declared in global namespace. It tries to match current declared namespace with the document's path, based on Two kind of actions might be provided: Move file to a new directory to match namespace and rename namespace to match directory hierarchy.
Examples
|
Can you clarify what you mean by "rename namespace"? Is that a true rename (where references to that namespace are updated? Or do you just mean "the namespace name declaration in that file is update to have the new name? |
It's the latter - the namespace name declaration in that file is updated to have the new name |
How is the root namespace determined? I got lost in some of the comments - is this a new feature or a change to move type to file that already exists? |
@KathleenDollard Sorry, the description above might be confusing, you can find a better explaination of root namespace vs. default namespace here. A quick recap, what it is actually called is "default namespace" in C#, which is defined in csproj by This is a new feature, since the file move is decided by namespace declaration instead of type name, and it also involves moving to a different folder. But logically it would make sense to have a single |
I asked the VB community in VBLang. My first reaction is that the relation with namespaces in VB tends to be different because of the difference in root namespace - I think there are more VB programs with no explicit namespace in the file, and more cases where the namespaces do not align with the file structure. But those cases might not be offered the code fix, so are there enough VB programmers that do use a parallel file and namespace structure to justify. |
I am currently on the VS 2019 Preview 2.2 and it does not seem to be available to me. I changed my classes namespace to something else: from I expected to have the refactoring available when hitting ctrl+. on the now wrong namespace. But I don't. Do I miss anything? Projects default namespace is If you need anything else, let me know. |
@btastic Thanks for trying out this feature and providing feedbacks! This refactoring isn't supported for .NET core projects in preview 2. The required change to support this was implemented in project-system very recently (dotnet/project-system#4432, which I believe will ship with preview 3), and there was a small fix needed on Roslyn side to make it work (#32920), which will ship with preview 4. To try this today with VS 2019 preview 2 bits, you will need to build and deploy both project-system and roslyn from source. |
@genlu Thanks for the response! Can't wait to have this in .NET core. Quick question though: will it be possible to get warnings for inconsistent namespaces? |
Right now this is just a code refactoring, but we are contemplating the idea of converting it into an analyzer/codefix combo (although, just want to point it out, because of perf, it's highly unlikely a "fix-all" fixer will be provided). FYI @jinujoseph @mavasani |
I love it. Thank you! |
Just eagerly installed the latest Preview 4.0 (RC) and it works! Thank you again |
I would love to see this become an analyzer (as you suggest above) and show warnings, even if it would only work in visual studio |
I have created #33877 for converting the refactoring to diagnostic/codefix. Please leave a comment if you have any feedback/suggestion. Thanks! |
@genlu Is it possible to make this refactoring globally available? I mean, in ReSharper and Rider you can right click on your project and you can apply this refactoring to all files in your project with a single click. |
@BackDoorManUC We are considering to make this refactoring a analyzer/codefix pair, it will provide warnings for out-of-sync namespaces, which might help to identify those cases. That said, we are not sure a fix-all will be provided for this, because the perf on a large solution might be an issue. |
FYI for anyone else who wants this and might want to 👍 and/or follow it, I filed #37453 to specifically request it because I frequently use this ReSharper feature. |
From comment in P5 Blog Post:
"Does the “Sync File and Type Name” refactoring also change the namespace of a type to match its folder name? And can it be performed in batch? This would make mass renames/moves of namespaces much easier!"
https://blogs.msdn.microsoft.com/visualstudio/2016/10/05/announcing-visual-studio-15-preview-5/#comments
The text was updated successfully, but these errors were encountered: