在Vim中调整/对齐错误格式输出

3

我使用Vim/gVim编写javascript(node)代码。 我已经在我的文件类型插件中将jslint设置为makeprg。以下是errorformat:

efm=%-P%f,
        \%A%>%\\s%\\?#%*\\d\ %m,%Z%.%#Line\ %l\\,\ Pos\ %c,
        \%-G%f\ is\ OK.,%-Q

以下是jslint的输出:

routes/pcr.js
#1 'db' was used before it was defined.
db.collection('pcrs', function (err, collection) { // Line 11, Pos 5
#2 'db' was used before it was defined.
db.collection('pcrs', function (err, collection) { // Line 23, Pos 5
#3 'BSON' was used before it was defined.
collection.findOne({'_id': new BSON.ObjectID(id)}, function (err, item) { // Line 24, Pos 40

这是快速修复窗口的输出:

routes/pcr.js|11 col 5| 'db' was used before it was defined.
routes/pcr.js|23 col 5| 'db' was used before it was defined.
routes/pcr.js|24 col 40| 'BSON' was used before it was defined.

在列数后,我想将其左侧填充为两位数字(我希望一个文件不会有超过99个错误!),以便它看起来像:

routes/pcr.js|11 col  5| 'db' was used before it was defined.
routes/pcr.js|23 col  5| 'db' was used before it was defined.
routes/pcr.js|24 col 40| 'BSON' was used before it was defined.

我想这也会影响行号0-9。是否可以有条件地填充输出?
2个回答

2

对齐的数字肯定很好,但我认为这需要对Vim进行源代码修补。

快速修复窗口中的信息来自Vim的内部数据结构(有关格式,请参见:help getqflist()),Vim决定如何将其可视化。


谢谢。随着夜晚的深入,我四处搜寻并意识到错误格式字符串只是从makeprg输出中提取信息的方式,但并不对格式化输出做任何事情。 - regretoverflow

1

:help quickfix-window提到了有关错误列表的重新格式化。

以下设置适用于我(更新):

au BufRead quickfix setl modifiable
            \| silent exe "%!perl -ple '
                \my ($file, $pos, $msg) = split qr{[|]}, $_, 3;
                \my $aligned_pos = sub {
                \  my @p = split qr{[ ]}, shift;
                \  return                                        if @p == 0;
                \  return sprintf q{\\%3s}, @p                   if @p == 1;
                \  return sprintf q{\\%3s \\%s}, @p              if @p == 2;
                \  return sprintf q{\\%3s \\%s \\%2s}, @p        if @p == 3;
                \  return sprintf q{\\%3s \\%s \\%2s \\%-8s}, @p if @p == 4;
                \  return join q{ }, @p;
                \}->($pos);
                \$_ = join q{|}, $file, $aligned_pos, $msg;
            \'"
            \| setl nomodifiable

哇,真是太棒了!你很厉害,回答得非常好。现在让我们来理解一下 Perl 的奥秘,看看那些神奇的东西... - regretoverflow
抱歉,我重写了我的丑陋的一行脚本。新的脚本可能比之前更清晰易懂。 - ernix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接