[英]How can NRefactory keep the original formatting
Hi I use NRefactory to extract String constant and remplace it by static variable for i18n, I can successfully do it but when I output my code with GetText() it doesn't keep the original formatting. 嗨我使用NRefactory提取String常量并通过i18n的静态变量重新设置它,我可以成功地做到但是当我用GetText()输出我的代码时它不保留原始格式。 I want to be do that with the minimum formatting change.
我希望通过最小的格式更改来做到这一点。
Thanks. 谢谢。
请查看NRefactory CodeProject文章的“修改C#代码”部分(最后一部分)。
If you want to keep current formatting you can use DocumentScript
to apply modifications on AST. 如果要保留当前格式,可以使用
DocumentScript
对AST应用修改。 It works based in TextLocation
property stored in syntax tree. 它基于存储在语法树中的
TextLocation
属性。
To create a script you need to do this : 要创建脚本,您需要执行以下操作:
IDocument document = new StringBuilderDocument("source code");
CSharpFormattingOptions policy = FormattingOptionsFactory.CreateAllman();
var options = new TextEditorOptions();
var script = new DocumentScript(document, policy, options);
Now you can use methods like Replace, Remove, InsertAfter and etc. on you AST. 现在,您可以在AST上使用Replace,Remove,InsertAfter等方法。 It works with both ast nodes as well as offsets.
它适用于ast节点和偏移。
It also keeps track of modifications, so you don't have to be worry about conflicts on multiple changes. 它还跟踪修改,因此您不必担心多个更改的冲突。
For more examples on NRefactory you may wanna also check my blog . 有关NRefactory的更多示例,您可能还需要查看我的博客 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.