简体   繁体   English

NRefactory如何保持原始格式

[英]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.

相关问题 如何获取包含日期的单元格的值并使用 NPOI 保留原始格式 - How to get the value of cell containing a date and keep the original formatting using NPOI 如何检查类是否在NRefactory中实现接口方法 - How to check if a class implements interface methods in NRefactory 如何增加数字但保持格式 - How to increment a number but keep formatting 如何使用join运算符保留原始值? - How to keep original values with join operator? C# 中不区分大小写的文本搜索:如何在突出显示匹配短语时保持原始大小写? - Case insensitive text search in C#: How can I keep the original case when highlighting matching phrases? 如何使用Regex保留原始行和修订行 - How to keep original line and ammended line with Regex 如何在MultiBinding中保持原始值不变 - How to keep the original value unchanged in MultiBinding 如何读取RTF文件的各个部分并保持格式 - How to read parts of an RTF file and keep formatting 如何保持对象在其原始位置/旋转范围内旋转,而不在每次新旋转时都添加/删除? - How can I keep the object to rotate in the range from it's original position/rotation and not to add/remove each time a new rotation? 如何只在一个文档中编译更改,而保留其他文档的原始版本? (C#,Asp.Net MVC) - How can I compile changes in only 1 document but keep the original build of the others? (C#, Asp.Net MVC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM