Roslyn을 사용하여 코드 리팩터링 확장을 만들려고합니다. 내가하고 싶은 것은 기본 네임 스페이스에 따라 리펙터 네임 스페이스입니다. 이름 공간이 단일 단어 일 때만 찾아서 바꿀 수 있지만 내 네임 스페이스가 kuku.riku.example
처럼 보이고 기본 네임 스페이스를 aaa
으로 변경하면 결과는 aaa
대신 kuku.riku.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);
}
새로운 식별자가 중첩 된 블록에서 '사용 중'과 충돌하면 점을 추가하면 매우 복잡한 구석을 만들 수 있으므로 아직 지원되지 않습니다. – SLaks