2016-05-31 61 views
0

我正在尝试使用Roslyn创建代码重构扩展。我想要做的是根据我的默认命名空间重构命名空间。它成功的查找和替换的命名空间时,它仅仅是一个单词,但是当我的名字空间看起来像kuku.riku.example和更改我的默认命名空间aaa结果是kuku.riku.aaa而不是只aaa。我究竟做错了什么?创建名称空间代码使用Roslyn重构

我的代码:

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); 
    } 
+0

这还不支持,因为加一个点可以创建非常复杂的角落情况下,如果在一个嵌套块'using'新标识的冲突。 – SLaks

回答

1

RenameSymbolAsync重命名,你传递,就像你所看到的命名空间的只是一部分。支持命名空间重命名,添加或删除点是我们想要构建的东西,但还没有。

+0

你有一个想法,我怎么能用当前版本做到这一点?顺便说一句,我可以添加点名称空间,但我不能删除一个... –

+0

添加点的能力更多,因为我们没有阻止它,并可能有错误。在您添加代码以支持核心的情况下,不支持点删除。 –