如何在vim中对C#代码的using语句进行排序?
最近我在 vim 中编辑 C# 代码。并且构建系统启用了 StyleCop,因此所有 using 语句应按字母顺序排列。
因此,我尝试在可视模式下选择以下代码行,然后输入“:sort”。
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Security;
using System.ServiceModel;
结果是:
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Security;
using System.ServiceModel;
它没有通过 StyleCop 检查,因为“System.Security”不在“System.Security.Permissions”之前。 “;”的 ASCII 值大于 ASCII 值“.”。
首选的结果是:
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.ServiceModel;
如何实现?
Recently I edit C# code in vim. And the build system has StyleCop enabled so that all using statement should be in alphabetical order.
So, I tried to select below lines of code in visual mode, then type ":sort".
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Security;
using System.ServiceModel;
The result is:
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Security;
using System.ServiceModel;
It doesn't pass StyleCop checking because "System.Security" is not ahead of "System.Security.Permissions". The ASCII value of ";" is larger than ASCII value of ".".
The preferred result is:
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.ServiceModel;
How to achieve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
:h :sort
是你的朋友:如果你希望删除重复项,请添加 uniq 标志:(
如果你有不同的值,这不会有任何好处不过“;”之后的评论)
:h :sort
is your friend:If along the way you wish to remove duplicates, add the uniq flag:
(This won't do any good if you have different comments after the ';' though)
(其中 1,4 是带有 using 语句的行)
(where 1,4 are lines with using statements)
不使用 CodeRush 或 ReSharper 就是从你的雇主那里窃取
(是的,我知道这需要 VS(据我所知 VS10 有这个 OOTB))
Not using CodeRush or ReSharper is stealing from your employer
<ducks for downvotes>
(Yes, I know that requires VS (and AFAIK VS10 has this OOTB))
在我的 Linux 机器上,本地环境不是 C(测试了 fr_FR、fr_FR.UTF-8、en_US、en_GB),sort 命令按照您的预期进行排序。你可以很好地通过管道传输到 sort 命令:
如果你在 Windows 上,我想你可以安装可以完成这项工作的 unix 工具(如 SFU),因为 vim 的 sort 命令似乎无法处理语言环境。
On my linux box with a local other than C (tested fr_FR, fr_FR.UTF-8, en_US, en_GB), the sort command sorts as you expect. You could very well pipe to the sort command :
If you are on windows, I suppose you can install unix tools (like SFU) that could do the job since vim's sort command doesn't seem to handle locale.