如何使vim的vimgrep命令保持缩进?

one*_*tok 3 vim

我正在尝试使用vim的快速修复(或本地)列表来获取从文件中提取的一些信息。例如,我想获取python模块的所有方法名称(这个想法是从pycharm借来的)。我想在vim的“本地列表”中得到的内容如下所示:

class Foo:
    def one():
    def two():
    def three():
def bar():
def bazz():
Run Code Online (Sandbox Code Playgroud)

为此,我大约执行以下步骤:

:" OK, the current buffer is being used.
:let file_name = expand('%:p')

:" The heart of the process is one of vim’s grep-like command.
:execute 'lvimgrep /\v^\s*(class|def)/ '.file_name

:" I open the results with the “lopen” command because “llist”
:" doesn’t allow me to use concealing.
:lopen

:" Since I’m working with one file, I don’t need information
:" about file name, line number etc.
:setlocal conceallevel=3
:syntax match NonText /\v^.+col \d+([:]|[|])/ transparent conceal
:" Please note, I‘m still able to jump to a line
:" with the “ll” command.
Run Code Online (Sandbox Code Playgroud)

但不幸的是,我得到了:

class Foo:
def one():
def two():
def three():
def bar():
def bazz():
Run Code Online (Sandbox Code Playgroud)

所有的凹痕都被吞没了!结果非常没用……我无法区分哪些功能属于一个类,哪些功能是独立的。

请注意,隐藏不会对结果产生有意义的影响。如果我拿走了最后两个命令(与隐蔽相关),则没有什么大的变化,只会显示文件名和行/列号,但行中的文本仍然没有缩进。

因此,我的问题是:

是否可以使lvimgrep(或类似物)保持原样以节省缩进?有魔术命令或选项吗?还是我应该自己编程实现lvimgrep

PS:我想使用vim的正则表达式。但是,如果不可能,我可以切换到外部“ grep”命令(我是linux专家),也可以使用BRE或ERE语法。

Rus*_*nov 6

不,目前,不可能lvimgrep(或什至是类似的命令)在快速修复(位置)列表条目中保留前导空白字符,因为如果文本长度大于3,则从开头无条件地跳过空格和制表符。

实现所需行为的唯一方法(至少使用*vimgrep命令)是修改源代码。例如,您可以添加一个选项,如以下修补程序所示:

diff --git a/runtime/optwin.vim b/runtime/optwin.vim                                                                                                                                                                            
index 7d3a8804d..caac55cf2 100644
--- a/runtime/optwin.vim
+++ b/runtime/optwin.vim
@@ -1299,6 +1299,7 @@ call <SID>OptionG("ve", &ve)
 call append("$", "eventignore\tlist of autocommand events which are to be ignored")
 call <SID>OptionG("ei", &ei)
 call append("$", "loadplugins\tload plugin scripts when starting up")
+call append("$", "locws\tenables whitespace characters for entries in the location window")
 call <SID>BinOptionG("lpl", &lpl)
 call append("$", "exrc\tenable reading .vimrc/.exrc/.gvimrc in the current directory")
 call <SID>BinOptionG("ex", &ex)
diff --git a/src/option.c b/src/option.c
index aabfc7f53..4ba280806 100644
--- a/src/option.c
+++ b/src/option.c
@@ -1791,6 +1791,9 @@ static struct vimoption options[] =
     {"loadplugins", "lpl",  P_BOOL|P_VI_DEF,
                            (char_u *)&p_lpl, PV_NONE,
                            {(char_u *)TRUE, (char_u *)0L} SCTX_INIT},
+    {"locws",      NULL,   P_BOOL|P_VI_DEF,
+                           (char_u *)&p_locws, PV_NONE,
+                           {(char_u *)FALSE, (char_u *)0L} SCTX_INIT},
     {"luadll",      NULL,   P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
 #if defined(DYNAMIC_LUA)
                            (char_u *)&p_luadll, PV_NONE,
diff --git a/src/option.h b/src/option.h
index c1a25b342..5e17c459e 100644
--- a/src/option.h
+++ b/src/option.h
@@ -602,6 +602,7 @@ EXTERN char_u       *p_lcs;         // 'listchars'

 EXTERN int     p_lz;           // 'lazyredraw'
 EXTERN int     p_lpl;          // 'loadplugins'
+EXTERN int     p_locws;        // 'locws'
 #if defined(DYNAMIC_LUA)
 EXTERN char_u  *p_luadll;      // 'luadll'
 #endif
diff --git a/src/quickfix.c b/src/quickfix.c
index 136c472e1..8e206ddd7 100644
--- a/src/quickfix.c
+++ b/src/quickfix.c
@@ -4417,8 +4417,9 @@ qf_update_buffer(qf_info_T *qi, qfline_T *old_last)
     static int
 qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
 {
-    int                len;
-    buf_T      *errbuf;
+    int    len;
+    buf_T *errbuf;
+    long   lval;

     if (qfp->qf_module != NULL)
     {
@@ -4472,10 +4473,12 @@ qf_buf_add_line(buf_T *buf, linenr_T lnum, qfline_T *qfp, char_u *dirname)
     IObuff[len++] = '|';
     IObuff[len++] = ' ';

-    // Remove newlines and leading whitespace from the text.
+    // Remove newlines and leading whitespace from the text,
+    // if the user not enabled whitespaces explicitly via locws option.
     // For an unrecognized line keep the indent, the compiler may
     // mark a word with ^^^^.
-    qf_fmt_text(len > 3 ? skipwhite(qfp->qf_text) : qfp->qf_text,
+    get_option_value((char_u *)"locws", &lval, NULL, 0);
+    qf_fmt_text(len > 3 ? (lval ? qfp->qf_text : skipwhite(qfp->qf_text)) : qfp->qf_text,
            IObuff + len, IOSIZE - len);

     if (ml_append_buf(buf, lnum, IObuff,
Run Code Online (Sandbox Code Playgroud)

使用locws选项,可以在quickfix / location条目中启用空格字符,如下所示:

:set locws
Run Code Online (Sandbox Code Playgroud)