0

我正在从 github 上的 spf13-vim 中阅读 .vimrc,但有些东西我不明白:

" Snippets & AutoComplete
    if count(g:spf13_bundle_groups, 'snipmate')
        Bundle 'garbas/vim-snipmate'
        Bundle 'honza/vim-snippets'
        " Source support_function.vim to support vim-snippets.
        if filereadable(expand("~/.vim/bundle/vim-snippets/snippets/support_functions.vim"))
            source ~/.vim/bundle/vim-snippets/snippets/support_functions.vim
        endif
    elseif count(g:spf13_bundle_groups, 'neocomplcache')
        Bundle 'Shougo/neocomplcache'
        Bundle 'Shougo/neosnippet'
        Bundle 'honza/vim-snippets'
    elseif count(g:spf13_bundle_groups, 'neocomplete')
    Bundle 'Shougo/neocomplete.vim.git'
        Bundle 'Shougo/neosnippet'
        Bundle 'honza/vim-snippets'
    endif

函数在这里做什么count()

4

2 回答 2

1

:help count()告诉你:

返回值为 {expr} 的项目在 |List| 中出现的次数 或 |字典| {comp}。

所以这计算了值(例如'snipmate')在 List 变量中出现的频率g:spf13_bundle_groups。然后将得到的数字解释为布尔值(0 = 假,其他一切 = 真)。

一个更规范(也许更快一点)的方法是使用index()

if index(g:spf13_bundle_groups, 'snipmate') != -1
    ...
于 2013-08-07T10:25:51.583 回答
1

检查项目(snipmate 或 neocomplcache ...)是否存在于g:spf13_bundle_groups.

于 2013-08-07T10:18:28.040 回答