Skip to content
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

Bug in Move Type to Namespace refactoring - usings not added for namespaces in same file #50672

Open
bernd5 opened this issue Jan 21, 2021 · 11 comments
Assignees
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@bernd5
Copy link
Contributor

bernd5 commented Jan 21, 2021

Version Used: Visual Studio 16.9.0 Preview 3.0

Sample Code:

using System;

namespace Tests
{
    internal class Foo
    {
        public static int Do(Inner.Data d) => 42;
    }
}

namespace Tests {
    namespace Inner
    {
        public class App
        {
            static void Main()
            {
                Console.WriteLine(Foo.Do(new Data()));
            }
        }
        public class Data { }
    }
}

Steps to Reproduce:

  1. Move type Foo to Namespace Something.Else

Expected Behavior:
Working Code

Actual Behavior:
Compile Error

Code after refatoring:

using Something.Else;
using System;

namespace Something.Else
{
    internal class Foo
    {
        public static int Do(Inner.Data d) => 42;
    }
}

namespace Tests
{
    namespace Inner
    {
        public class App
        {
            static void Main()
            {
                Console.WriteLine(Foo.Do(new Data()));
            }
        }
        public class Data { }
    }
}

Expected Code:

using Something.Else;
using System;
using Tests.Inner;

namespace Something.Else
{
    internal class Foo
    {
        public static int Do(Data d) => 42;
    }
}

namespace Tests
{
    namespace Inner
    {
        public class App
        {
            static void Main()
            {
                Console.WriteLine(Foo.Do(new Data()));
            }
        }
        public class Data { }
    }
}
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 21, 2021
@bernd5
Copy link
Contributor Author

bernd5 commented Jan 21, 2021

PR-Merge: #30896 (comment)
(The error is not there but it's the IDEs entry point...)

It works if all types are fully qualified

@jinujoseph jinujoseph added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jan 29, 2021
@jinujoseph jinujoseph added this to the 16.10 milestone Jan 29, 2021
@ryzngard ryzngard changed the title Bug in Move Type to Namespace refactoring Bug in Move Type to Namespace refactoring - usings not added for namespaces in same file Jan 29, 2021
@ryzngard ryzngard added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Jan 29, 2021
@ryzngard
Copy link
Contributor

Should be fairly easy to fix, we can take this in 16.10. Marking help wanted in case anyone wants to take it before I get to it.

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 29, 2021

Thanks for your response. I'm not sure how easy or difficult it is to fix. But the issue seems to be that partially qualified names are not handled correctly.
With just adding a using it is possible to get a name-conflict which would require full-qualification or a using declaration inside a namespace declaration.
It sounds easier as it is...

@ryzngard
Copy link
Contributor

@bernd5 agreed in some cases it could be complex, but not too bad. We can find references in current doc, fully qualify, add using to the new namespace, and use simplifier to remove unnecessary qualifications and namespace. References outside current doc should already be fixed

@bernd5
Copy link
Contributor Author

bernd5 commented Jan 31, 2021

Btw.: is it possible to access those refactorings from the public roslyn API surface?

@CyrusNajmabadi
Copy link
Member

Btw.: is it possible to access those refactorings from the public roslyn API surface?

Yes. You can use VS's ISuggestedActionsSourceProvider to query for Roslyn's provider that exposes our code fix and code refactoring provideers (ISuggestedActionsSource). You can call GetSuggestedActions on that source to get all our suggestions.

@bernd5
Copy link
Contributor Author

bernd5 commented Feb 2, 2021

Do you know where to find the implementation of the "move namespace" SuggestionAction? I want to move some classes (ClassDeclarationSyntax / ITypeSymbol) to a different namespace.
Do you have a link to a sample on how to get suggestions by code?

@ryzngard
Copy link
Contributor

ryzngard commented Feb 3, 2021

@bernd5 what are you trying to do? That code doesn't have a public API.

@bernd5
Copy link
Contributor Author

bernd5 commented Feb 3, 2021

I have a huge library with a massive amount of types. If certain criteria are met I would like to move some of them to another namespace.
I have started with my own CSharp-Syntax-Rewriter - but I would generally avoid to "reinvent the wheel" all over again and use a standard solution for this task.
The really complex part here is the adjustment of non-qualified names and the handling of using clauses.

@bernd5
Copy link
Contributor Author

bernd5 commented Feb 3, 2021

Even the API / rewriter is not public - I could imagine to use the assembly :-).

@CyrusNajmabadi
Copy link
Member

This is not a supported scenario. You could certainly look at teh code we've written and copy/paste for your own needs as appropriate. But we don't expose out individual APIs to accomplish this stuff (primarily because these APIs change all the time and we don't want to be locked into supporting something forever that limits our ability to respond to changes/issues in an efficient fashion).

@ryzngard ryzngard modified the milestones: 16.10, 16.10.P3 Apr 15, 2021
@jinujoseph jinujoseph modified the milestones: 16.10.P3, 16.10 Apr 23, 2021
@jinujoseph jinujoseph modified the milestones: 16.10, Backlog Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
None yet
Development

No branches or pull requests

4 participants