2

I'm trying to create code refactoring extension using Roslyn. What I want to do is refactor namespaces according to my default namespace. It succeed to find and replace the namespace when it is single word only but when my namespaces looks like that kuku.riku.example and I change my default namespace to aaa the result is kuku.riku.aaa instead of just aaa. What am I doing wrong?

My code:

    public sealed override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
    {
        SyntaxNode node = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

        NamespaceDeclarationSyntax namespaceDec = (NamespaceDeclarationSyntax)node.ChildNodes()
                                                                                  .FirstOrDefault(syntaxNode => syntaxNode as NamespaceDeclarationSyntax != null);

        string defaultNamespace = GetDefaultNamespace(context.Document);

        if (defaultNamespace != namespaceDec.Name.ToString())
        {
            var action = CodeAction.Create("Adjust Namespaces", c => AdjustNamespacesAsync(context.Document, namespaceDec, defaultNamespace, context.CancellationToken));

            // Register this code action.
            context.RegisterRefactoring(action);
        }

    }

    private static async Task<Solution> AdjustNamespacesAsync(Document document, NamespaceDeclarationSyntax declerationSyntax, string newName, CancellationToken cancelationToken)
    {
        SemanticModel semanticModel = await document.GetSemanticModelAsync(cancelationToken);
        var fist = declerationSyntax.Span;
        INamespaceSymbol symbol = semanticModel.GetDeclaredSymbol(declerationSyntax, cancelationToken);


        Solution origionalSolution = document.Project.Solution;
        OptionSet workspaceOptions = document.Project.Solution.Workspace.Options;



        return await Renamer.RenameSymbolAsync(origionalSolution, symbol, newName, workspaceOptions, cancelationToken);
    }
1
  • This isn't supported yet, because adding a dot can create very complicated corner cases if the new identifier conflicts with a using in a nested block. Commented May 31, 2016 at 18:04

1 Answer 1

2

RenameSymbolAsync renames just the part of the namespace that you're passing in, as you're seeing. Supporting namespace renames that add or remove dots is something we've wanted to build, but just haven't yet.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have an idea how can I do that with the current version? by the way I can add namespace with dots but I can't remove one...
The ability to add dots is more because we didn't block it, and might have bugs. Short of you adding code to support it to the core, dot removal just isn't supported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.