简体   繁体   English

从vimscript触发文件完成

[英]Trigger file completion from vimscript

probably the answer to my question is obvious but even after a straight our of searching I cannot find anything useful. 我的问题的答案可能很明显,但是即使经过一番搜索,我仍然找不到任何有用的东西。

I'm currently writing a small vim latex auto-completion plugin that suggests completions based on the editing context. 我目前正在编写一个小的vim乳胶自动完成插件,该插件根据编辑上下文建议完成情况。 The relevant part of the code looks like this: 代码的相关部分如下所示:

function! Complete_latex(findstart, base)
    if a:findstart
        " locate the start of the base
        "....
    else
        if s:envname_required()
            return s:env_complete(a:base)
        endif

        if s:citation_required()
            return s:cite_complete(a:base)
        endif

        if s:filename_required()
            " TODO: Trigger filename completion
        endif
    endif
endfunction

set omnifunc=Complete_latex

The *_required() functions basically throw a bunch of regexps at the current line I'm editing to figure out what I'm doing right now. *_required()函数基本上在我正在编辑的当前行上抛出一堆正则*_required()以弄清楚我现在正在做什么。 So if I am in INSERT mode at a position like ...\\input{|... I'd like my omnifunc to call the same completion I can trigger with CX CF in INSERT mode. 因此,如果我处于INSERT模式下的位置,例如... \\ input {| ...,我希望我的omnifunc调用相同的完成功能,那么我可以在INSERT模式下使用CX CF进行触发。

As I also use the YouCompleteMe plugin and set { as a trigger for semantic completion in *.tex files, the triggering is being take care of. 由于我还使用YouCompleteMe插件并将{设置为* .tex文件中语义完成的触发器,因此需要注意触发器。

I know that I can get a list of files and fill the popup menu myself, but I was nevertheless wondering If I can use a builtin function of vim. 我知道我可以获取文件列表并自己填写弹出菜单,但是我想知道是否可以使用vim的内置功能。

Thank you. 谢谢。

I'm not entirely sure if that is the best way to go, but I came up with 我不确定这是否是最好的方法,但是我想出了

let l:glob_pattern = a:base . '*' 

let l:files_pre = globpath('.', l:glob_pattern, 0, 1)
let l:files_post = []

for l:file in l:files_pre
     call add(l:files_post, substitute(l:file, '^\./', '', ''))
endfor

return l:files_post

Which basically gets all files in the current directory matching "base*" and returns a list of them. 它基本上获取当前目录中与“ base *”匹配的所有文件,并返回它们的列表。 The post processing part just removes the './' at the beginning of each filename returned by globpath 后处理部分只是删除globpath返回的每个文件名开头的“ ./”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM