【发布时间】:2021-05-25 08:55:03
【问题描述】:
假设您正在编辑一个 .c 文件,并且您在如下一行:
#include {here}
然后你按下tab键,有没有办法让它完成所有与当前目录相关的头文件以及所有系统范围的头文件,如stdio.h和stdlib.h?
【问题讨论】:
-
还要注意CoC+ccls(我没有用clangd试过)允许完成各种事情,其中包括文件的名称。
标签: vim autocomplete
假设您正在编辑一个 .c 文件,并且您在如下一行:
#include {here}
然后你按下tab键,有没有办法让它完成所有与当前目录相关的头文件以及所有系统范围的头文件,如stdio.h和stdlib.h?
【问题讨论】:
标签: vim autocomplete
很遗憾,:help compl-filename 说:
注意:此处(目前)未使用“路径”选项。
但是如何根据 Vim 文档中的示例将我们自己的自定义完成组合在一起呢?
这是我们将要使用的内容,位于:help complete-functions:
fun! CompleteMonths(findstart, base)
if a:findstart
" locate the start of the word
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '\a'
let start -= 1
endwhile
return start
else
" find months matching with "a:base"
let res = []
for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
if m =~ '^' . a:base
call add(res, m)
endif
endfor
return res
endif
endfun
set completefunc=CompleteMonths
以下是对其工作原理的高级解释:
函数以两种不同的方式调用:
- 首先调用该函数来查找要完成的文本的开头。
- 稍后调用该函数以实际查找匹配项。
在代码中,条件的第一部分处理第一次调用,不需要更改。我们将要更改的是处理第二个调用的第二部分。由于我们想要&path 中的文件,我们可以使用:help globpath() 和给定的基数列出&path 和:help fnamemodify() 下的匹配文件,只返回文件名:
function! CompleteFromPath(findstart, base)
if a:findstart
" locate the start of the word
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '\a'
let start -= 1
endwhile
return start
else
return globpath(&path, a:base . '*.h', 0, 1)
\ ->map({ idx, val -> fnamemodify(val, ':t') })
endif
endfunction
set completefunc=CompleteFromPath
【讨论】: