Outdenting of content of removed block

假如想象 提交于 2019-12-10 16:15:14

问题


I'm writing a Roslyn Diagnostic with Code Fix. If there's a try block with one empty catch block, i want to provide the option to remove the catch block and replace the try block with its content. My problem is the outdenting of the content of the try block. I tried using the Formatter, but the lines are still indentend one level too much. Here's my code:

private async Task<Document> RemoveTryCatchBlockAsync(Document document, TryStatementSyntax tryBlock, CancellationToken cancellationToken)
{
    var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
    var newRoot = oldRoot.ReplaceNode(tryBlock, tryBlock.Block.ChildNodes());
    Formatter.Format(newRoot, MSBuildWorkspace.Create());

    // Return document with transformed tree. 
    return document.WithSyntaxRoot(newRoot);
}

回答1:


Roslyn is very immutable, your Formatter won't be changing the original node but instead return you a new one that is formatted.

Instead, try this:

var formattedRoot = Formatter.Format(newRoot, MSBuildWorkspace.Create());
return document.WithSyntaxRoot(formattedRoot);


来源:https://stackoverflow.com/questions/25013869/outdenting-of-content-of-removed-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!