如何在vim中对C#代码的using语句进行排序?

发布于 2024-08-14 05:56:18 字数 716 浏览 10 评论 0原文

最近我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北陌 2024-08-21 05:56:18

:h :sort 是你的朋友:

:[range]sort r /[^;]*/

如果你希望删除重复项,请添加 uniq 标志:(

:[range]sort ur /[^;]*/

如果你有不同的值,这不会有任何好处不过“;”之后的评论)

:h :sort is your friend:

:[range]sort r /[^;]*/

If along the way you wish to remove duplicates, add the uniq flag:

:[range]sort ur /[^;]*/

(This won't do any good if you have different comments after the ';' though)

ら栖息 2024-08-21 05:56:18
:1,4s/;$//
:sort
:1,4s/$/;/

(其中 1,4 是带有 using 语句的行)

:1,4s/;$//
:sort
:1,4s/$/;/

(where 1,4 are lines with using statements)

零度° 2024-08-21 05:56:18

不使用 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))

今天小雨转甜 2024-08-21 05:56:18

在我的 Linux 机器上,本地环境不是 C(测试了 fr_FR、fr_FR.UTF-8、en_US、en_GB),sort 命令按照您的预期进行排序。你可以很好地通过管道传输到 sort 命令:

:1,4!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 :

:1,4!sort

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文