Browse Source

Fix many subtle issues regarding on-demand loading etc.

- On-demand loading
    - Fix loading of unwanted files (e.g. colors/*.vim, syntax/*.vim, etc.)
- Filetyp-based on-demand loading
    - Load `after/ftdetect` as well
    - Make sure indent files are loaded by invoking
      `doautocmd filetypeindent FileType`
- Ensure plugin loaded when it was added after Vim started
- Do not reload $MYVIMRC after installtion/update
    - Instead simply call plug#end()
Junegunn Choi 11 years ago
parent
commit
61b77bc8e8
3 changed files with 106 additions and 40 deletions
  1. 24 16
      plug.vim
  2. 17 0
      test/run
  3. 65 24
      test/workflow.vader

+ 24 - 16
plug.vim

@@ -123,7 +123,16 @@ function! s:to_a(v)
   return type(a:v) == s:TYPE.list ? a:v : [a:v]
 endfunction
 
+function! s:source(from, ...)
+  for pattern in a:000
+    for vim in split(globpath(a:from, pattern), '\n')
+      execute 'source '.vim
+    endfor
+  endfor
+endfunction
+
 function! plug#end()
+  let reload = !has('vim_starting')
   if !exists('g:plugs')
     return s:err('Call plug#begin() first')
   endif
@@ -144,7 +153,11 @@ function! plug#end()
   for name in reverse(copy(g:plugs_order))
     let plug = g:plugs[name]
     if !has_key(plug, 'on') && !has_key(plug, 'for')
-      call s:add_rtp(s:rtp(plug))
+      let rtp = s:rtp(plug)
+      call s:add_rtp(rtp)
+      if reload
+        call s:source(rtp, 'plugin/**/*.vim', 'after/plugin/**/*.vim')
+      endif
       continue
     endif
 
@@ -168,9 +181,7 @@ function! plug#end()
     endif
 
     if has_key(plug, 'for')
-      for vim in split(globpath(s:rtp(plug), 'ftdetect/**/*.vim'), '\n')
-        execute 'source '.vim
-      endfor
+      call s:source(s:rtp(plug), 'ftdetect/**/*.vim', 'after/ftdetect/**/*.vim')
       for key in s:to_a(plug.for)
         if !has_key(lod, key)
           let lod[key] = []
@@ -263,24 +274,23 @@ function! s:lod(plug, types)
   let rtp = s:rtp(a:plug)
   call s:add_rtp(rtp)
   for dir in a:types
-    for vim in split(globpath(rtp, dir.'/**/*.vim'), '\n')
-      execute 'source '.vim
-    endfor
+    call s:source(rtp, dir.'/**/*.vim')
   endfor
 endfunction
 
 function! s:lod_ft(pat, names)
   for name in a:names
-    call s:lod(g:plugs[name], ['plugin', 'after'])
+    call s:lod(g:plugs[name], ['plugin', 'after/plugin'])
   endfor
   call s:reorg_rtp()
   execute 'autocmd! PlugLOD FileType ' . a:pat
   silent! doautocmd filetypeplugin FileType
+  silent! doautocmd filetypeindent FileType
 endfunction
 
 function! s:lod_cmd(cmd, bang, l1, l2, args, name)
   execute 'delc '.a:cmd
-  call s:lod(g:plugs[a:name], ['plugin', 'ftdetect', 'after'])
+  call s:lod(g:plugs[a:name], ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin'])
   call s:reorg_rtp()
   execute printf('%s%s%s %s', (a:l1 == a:l2 ? '' : (a:l1.','.a:l2)), a:cmd, a:bang, a:args)
 endfunction
@@ -288,7 +298,7 @@ endfunction
 function! s:lod_map(map, name, prefix)
   execute 'unmap '.a:map
   execute 'iunmap '.a:map
-  call s:lod(g:plugs[a:name], ['plugin', 'ftdetect', 'after'])
+  call s:lod(g:plugs[a:name], ['ftdetect', 'after/ftdetect', 'plugin', 'after/plugin'])
   call s:reorg_rtp()
   let extra = ''
   while 1
@@ -363,16 +373,13 @@ function! s:update(...)
   call s:update_impl(1, a:000)
 endfunction
 
-function! s:apply()
+function! s:helptags()
   for spec in values(g:plugs)
     let docd = join([spec.dir, 'doc'], '/')
     if isdirectory(docd)
       silent! execute 'helptags '. join([spec.dir, 'doc'], '/')
     endif
   endfor
-  runtime! plugin/*.vim
-  runtime! after/**/*.vim
-  silent! source $MYVIMRC
 endfunction
 
 function! s:syntax()
@@ -503,10 +510,11 @@ endfunction
 function! s:finish(pull)
   call append(3, '- Finishing ... ')
   redraw
-  call s:apply()
-  call s:syntax()
+  call s:helptags()
+  call plug#end()
   call setline(4, getline(4) . 'Done!')
   normal! gg
+  call s:syntax()
   redraw
   let msgs = []
   if !empty(s:prev_update.errors)

+ 17 - 0
test/run

@@ -14,6 +14,23 @@ if [ ! -d fzf-staged ]; then
   git clone https://github.com/junegunn/fzf.git fzf-staged
 fi
 
+make_dirs() {
+  mkdir -p "$1"
+  cd "$1"
+  mkdir -p autoload colors ftdetect ftplugin indent plugin syntax
+  for d in *; do
+    cat > $d/xxx.vim << EOF
+    " echom expand('<sfile>')
+    let g:xxx = get(g:, 'xxx', [])
+    call add(g:xxx, '${1:4}/$d')
+EOF
+  done
+  cd - > /dev/null
+}
+
+make_dirs xxx/
+make_dirs xxx/after
+
 cat > /tmp/mini-vimrc << VIMRC
 set rtp+=vader.vim
 source $PLUG_SRC

+ 65 - 24
test/workflow.vader

@@ -1,5 +1,5 @@
 Execute (Initialize test environment):
-  Save &rtp, g:plugs, g:plug_home, $MYVIMRC
+  Save &rtp, g:plugs, g:plug_home
 
   let first_rtp = split(&rtp, ',')[0]
   let last_rtp  = split(&rtp, ',')[-1]
@@ -33,11 +33,6 @@ Execute (Initialize test environment):
   endfunction
   command! -nargs=+ -bang AssertExpect call AssertExpect('<bang>' == '!', <args>)
 
-  let g:vimrc_reloaded = 0
-  let vimrc    = tempname()
-  call writefile(['let g:vimrc_reloaded += 1'], vimrc)
-  let $MYVIMRC = vimrc
-
 Execute (plug#end() before plug#begin() should fail):
   redir => out
   AssertEqual 0, plug#end()
@@ -133,7 +128,6 @@ Execute (Yet, plugins are not available):
 
 Execute (PlugInstall):
   PlugInstall
-  AssertEqual 1, g:vimrc_reloaded
   q
 
 Execute (Plugin available after installation):
@@ -167,7 +161,6 @@ Expect:
 Execute (PlugUpdate to set the right branch):
   PlugUpdate
   call PlugStatusSorted()
-  AssertEqual 2, g:vimrc_reloaded
 
 Expect:
   - goyo.vim: OK
@@ -254,7 +247,6 @@ Execute (PlugClean! to remove vim-emoji):
 Execute (PlugUpdate to install both again):
   PlugUpdate
   AssertExpect '^- [^:]*:', 2
-  AssertEqual 3, g:vimrc_reloaded
   Assert !empty(globpath(&rtp, 'colors/seoul256.vim')), 'seoul256.vim should be found'
   Assert !empty(globpath(&rtp, 'autoload/emoji.vim')), 'vim-emoji should be found'
   q
@@ -262,7 +254,6 @@ Execute (PlugUpdate to install both again):
 Execute (PlugUpdate only to find out plugins are up-to-date, D key to check):
   PlugUpdate
   AssertExpect 'Already up-to-date', 2
-  AssertEqual 4, g:vimrc_reloaded
   normal D
   AssertEqual 'No updates.', getline(1)
   q
@@ -384,9 +375,14 @@ Given (Unaligned code):
   aa=2
 
 Execute (Check installed plugins):
+  if has('vim_starting')
+    Log 'Vader is run from commandline'
+    runtime! plugin/**/*.vim
+  endif
   Assert exists(':FNR'),           'FNR command should be found'
-  Assert exists(':EasyAlign'),     'EasyAlign command should be found'
   Assert !exists(':RedisExecute'), 'RedisExecute command still should not be found'
+
+  Assert exists(':EasyAlign'), 'EasyAlign command should be found'
   %EasyAlign=
 
 Expect (Aligned code):
@@ -683,19 +679,64 @@ Execute (Using custom dir):
   q
   Assert isdirectory(g:plugs['vim-easy-align'].dir)
 
-Execute (Cleanup):
-  call system('rm -rf '.temp_plugged)
-  call rename('fzf', 'fzf-staged')
+**********************************************************************
+~ On-demand loading load order
+**********************************************************************
+Before (Clear global vars):
+  let g:xxx = []
+  set rtp-=$PWD/xxx/
+  set rtp-=$PWD/xxx/after
 
-  unlet g:plugs
-  unlet g:plug_home
-  unlet g:vimrc_reloaded
-  unlet temp_plugged vader plug basertp save_rtp repo lnum fzf out
-  delf PlugStatusSorted
-  delf AssertExpect
-  delf PlugUpdated
-  delc AssertExpect
-  unmap /
-  unmap ?
+Execute (Immediate loading):
+  call plug#begin()
+  Plug '$PWD/xxx'
+  call plug#end()
+
+  " FIXME:
+  " Different result when Vader is run from commandline with `-c` option
+  Log g:xxx
+  if has('vim_starting')
+    AssertEqual ['/ftdetect', 'after/ftdetect'], g:xxx
+  else
+    AssertEqual ['/plugin', 'after/plugin', '/ftdetect', 'after/ftdetect'], g:xxx
+  endif
+
+Execute (Command-based on-demand loading):
+  call plug#begin()
+  Plug '$PWD/xxx', { 'on': 'XXX' }
+  call plug#end()
+
+  AssertEqual [], g:xxx
+
+  silent! XXX
+  AssertEqual ['/ftdetect', 'after/ftdetect', '/plugin', 'after/plugin'], g:xxx
+
+  setf xxx
+  AssertEqual ['/ftdetect', 'after/ftdetect', '/plugin', 'after/plugin', '/ftplugin', 'after/ftplugin', '/indent', 'after/indent', '/syntax', 'after/syntax'], g:xxx
+
+Execute (Filetype-based on-demand loading):
+  call plug#begin()
+  Plug '$PWD/xxx', { 'for': 'xxx' }
+  call plug#end()
+
+  AssertEqual ['/ftdetect', 'after/ftdetect'], g:xxx
+
+  setf xxx
+  AssertEqual ['/ftdetect', 'after/ftdetect', '/plugin', 'after/plugin', '/ftplugin', 'after/ftplugin', '/indent', 'after/indent', '/syntax', 'after/syntax'], g:xxx
+
+Before:
+Execute (Cleanup):
+  silent! call system('rm -rf '.temp_plugged)
+  silent! call rename('fzf', 'fzf-staged')
+  silent! unlet g:plugs
+  silent! unlet g:plug_home
+  silent! unlet temp_plugged vader plug basertp save_rtp repo lnum fzf out
+  silent! delf PlugStatusSorted
+  silent! delf AssertExpect
+  silent! delf PlugUpdated
+  silent! delc AssertExpect
+  silent! unmap /
+  silent! unmap ?
 
   Restore
+