kristijanhusak / vim-packager

Vim plugin manager that utilizes "jobs" and "pack" features.
MIT License
245 stars 9 forks source link

How to lazy load a plugin on event ? #33

Closed krisfans closed 3 years ago

krisfans commented 3 years ago

For example , i want to lazy load a autocompletion plugin like nvim-compe on InsertEnter ,what should i do ? I tried the following, but failed

if &compatible
    set nocompatible
endif

" Load packager only when you need it
function! PackagerInit() abort
    packadd vim-packager
    call packager#init()
    call packager#add('kristijanhusak/vim-packager', { 'type': 'opt' })
    " complete
    call packager#add('neovim/nvim-lspconfig')
    call packager#add('hrsh7th/nvim-compe', { 'type': 'opt' })
    " call packager#add('hrsh7th/nvim-compe')
    " call packager#add('hrsh7th/vim-vsnip-integ', {'requires': ['hrsh7th/vim-vsnip'] })

" ui
call packager#add('bagrat/vim-buffet', { 'type': 'opt' })

endfunction

" These commands are automatically added when using `packager#setup()`
command! -nargs=* -bar PackagerInstall call PackagerInit() | call packager#install(<args>)
command! -nargs=* -bar PackagerUpdate call PackagerInit() | call packager#update(<args>)
command! -bar PackagerClean call PackagerInit() | call packager#clean()
command! -bar PackagerStatus call PackagerInit() | call packager#status()

"Load plugins only for specific filetype
"Note that this should not be done for plugins that handle their loading using ftplugin file.
"More info in :help pack-add
" augroup packager_filetype
"   autocmd!
"   autocmd FileType javascript packadd vim-js-file-import
"   autocmd FileType go packadd vim-go
" augroup END

"Lazy load plugins with a mapping
" nnoremap <silent><Leader>ww :unmap <Leader>ww<BAR>packadd vimwiki<BAR>VimwikiIndex<CR>
set completeopt=menuone,noselect " required
augroup packager_envent
    autocmd!
    autocmd InsertEnter * packadd nvim-compe
    autocmd BufReadPre,BufNewFile * packadd vim-buffet
augroup END

let g:buffet_powerline_separators = 1
let g:buffet_left_trunc_icon = "\uf0a8"
let g:buffet_right_trunc_icon = "\uf0a9"

" compe
let g:compe = {}
let g:compe.enabled = v:true
let g:compe.autocomplete = v:true
let g:compe.debug = v:false
let g:compe.min_length = 0
let g:compe.preselect = 'enable'
let g:compe.throttle_time = 80
let g:compe.source_timeout = 200
let g:compe.incomplete_delay = 400
let g:compe.max_abbr_width = 100
let g:compe.max_kind_width = 100
let g:compe.max_menu_width = 100
let g:compe.documentation = v:true

let g:compe.source = {}
let g:compe.source.path = v:true
let g:compe.source.buffer = v:true
let g:compe.source.calc = v:false
let g:compe.source.nvim_lsp = v:true
let g:compe.source.nvim_lua = v:true
let g:compe.source.vsnip = v:true
highlight link CompeDocumentation NormalFloat
inoremap <silent><expr> <C-Space> compe#complete()
inoremap <silent><expr> <CR>      compe#confirm('<CR>')
inoremap <silent><expr> <C-e>     compe#close('<C-e>')
inoremap <silent><expr> <C-f>     compe#scroll({ 'delta': +4 })
inoremap <silent><expr> <C-d>     compe#scroll({ 'delta': -4 })
" lsp
lua require("lsp_config")

In addition , is there anyway to control a plugin config before/after a lazy load plugin ? Thanks.

kristijanhusak commented 3 years ago

You add it as opt and then do packadd on autocmd:

autocmd InsertEnter * packadd nvim-compe

If you are referring to something like https://github.com/wbthomason/packer.nvim has, that is not supported.

krisfans commented 3 years ago

autocmd InsertEnter * packadd nvim-compe

It doesn't work , and i don't know why .

kristijanhusak commented 3 years ago

That's a question for nvim-compe author. I tried it, and it loaded the plugin (:echo g:loaded_compe returns true), so I'm not sure why it's not working.