删除块的内容缩进 [英] Outdenting of content of removed block
本文介绍了删除块的内容缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写带有代码修复功能的 Roslyn 诊断程序.如果有一个带有一个空 catch 块的 try 块,我想提供删除 catch 块并用其内容替换 try 块的选项.我的问题是 try 块内容的缩进.我尝试使用格式化程序,但行仍然缩进一级太多.这是我的代码:
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);
}
推荐答案
Roslyn 是非常不可变的,您的 Formatter
不会更改原始节点,而是返回一个已格式化的新节点.
Roslyn is very immutable, your Formatter
won't be changing the original node but instead return you a new one that is formatted.
相反,试试这个:
var formattedRoot = Formatter.Format(newRoot, MSBuildWorkspace.Create());
return document.WithSyntaxRoot(formattedRoot);
这篇关于删除块的内容缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文