Use the global caching option for extensions

This commit is contained in:
Kien N 2012-06-19 00:12:54 +07:00
parent 5929844814
commit cb86f0f386
4 changed files with 38 additions and 22 deletions

View File

@ -138,8 +138,9 @@ en
let s:lash = ctrlp#utils#lash() let s:lash = ctrlp#utils#lash()
" Limiters let s:compare_lim = 3000
let [s:compare_lim, s:nocache_lim] = [3000, 4000]
let s:ficounts = {}
" Regexp " Regexp
let s:fpats = { let s:fpats = {
@ -291,7 +292,7 @@ endf
" * Files {{{1 " * Files {{{1
fu! ctrlp#files() fu! ctrlp#files()
let cafile = ctrlp#utils#cachefile() let cafile = ctrlp#utils#cachefile()
if g:ctrlp_newcache || !filereadable(cafile) || !s:caching if g:ctrlp_newcache || !filereadable(cafile) || s:nocache()
let [lscmd, s:initcwd, g:ctrlp_allfiles] = [s:lsCmd(), s:dyncwd, []] let [lscmd, s:initcwd, g:ctrlp_allfiles] = [s:lsCmd(), s:dyncwd, []]
" Get the list of files " Get the list of files
if empty(lscmd) if empty(lscmd)
@ -313,6 +314,7 @@ fu! ctrlp#files()
let g:ctrlp_allfiles = ctrlp#utils#readfile(cafile) let g:ctrlp_allfiles = ctrlp#utils#readfile(cafile)
en en
en en
cal extend(s:ficounts, { s:dyncwd : len(g:ctrlp_allfiles) })
retu g:ctrlp_allfiles retu g:ctrlp_allfiles
endf endf
@ -1765,14 +1767,16 @@ fu! s:mmode()
endf endf
" Cache {{{2 " Cache {{{2
fu! s:writecache(cache_file) fu! s:writecache(cache_file)
let fwrite = len(g:ctrlp_allfiles) > s:nocache_lim if ( g:ctrlp_newcache || !filereadable(a:cache_file) ) && !s:nocache()
if ( g:ctrlp_newcache || !filereadable(a:cache_file) ) && s:caching || fwrite
if fwrite | let s:caching = 1 | en
cal ctrlp#utils#writecache(g:ctrlp_allfiles) cal ctrlp#utils#writecache(g:ctrlp_allfiles)
let g:ctrlp_newcache = 0 let g:ctrlp_newcache = 0
en en
endf endf
fu! s:nocache()
retu !s:caching || ( s:caching > 1 && get(s:ficounts, s:dyncwd) < s:caching )
endf
fu! s:insertcache(str) fu! s:insertcache(str)
let [data, g:ctrlp_newcache, str] = [g:ctrlp_allfiles, 1, a:str] let [data, g:ctrlp_newcache, str] = [g:ctrlp_allfiles, 1, a:str]
if strlen(str) <= strlen(data[0]) if strlen(str) <= strlen(data[0])

View File

@ -10,12 +10,7 @@ if exists('g:loaded_ctrlp_dir') && g:loaded_ctrlp_dir
en en
let [g:loaded_ctrlp_dir, g:ctrlp_newdir] = [1, 0] let [g:loaded_ctrlp_dir, g:ctrlp_newdir] = [1, 0]
let s:ars = [ let s:ars = ['s:maxdepth', 's:maxfiles', 's:compare_lim', 's:glob', 's:caching']
\ 's:maxdepth',
\ 's:maxfiles',
\ 's:compare_lim',
\ 's:glob',
\ ]
cal add(g:ctrlp_ext_vars, { cal add(g:ctrlp_ext_vars, {
\ 'init': 'ctrlp#dir#init('.join(s:ars, ', ').')', \ 'init': 'ctrlp#dir#init('.join(s:ars, ', ').')',
@ -27,6 +22,8 @@ cal add(g:ctrlp_ext_vars, {
\ }) \ })
let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars) let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
let s:dircounts = {}
" Utilities {{{1 " Utilities {{{1
fu! s:globdirs(dirs, depth) fu! s:globdirs(dirs, depth)
let entries = split(globpath(a:dirs, s:glob), "\n") let entries = split(globpath(a:dirs, s:glob), "\n")
@ -42,6 +39,10 @@ endf
fu! s:max(len, max) fu! s:max(len, max)
retu a:max && a:len > a:max ? 1 : 0 retu a:max && a:len > a:max ? 1 : 0
endf endf
fu! s:nocache()
retu !s:caching || ( s:caching > 1 && get(s:dircounts, s:cwd) < s:caching )
endf
" Public {{{1 " Public {{{1
fu! ctrlp#dir#init(...) fu! ctrlp#dir#init(...)
let s:cwd = getcwd() let s:cwd = getcwd()
@ -50,7 +51,7 @@ fu! ctrlp#dir#init(...)
endfo endfo
let cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'dir' let cadir = ctrlp#utils#cachedir().ctrlp#utils#lash().'dir'
let cafile = cadir.ctrlp#utils#lash().ctrlp#utils#cachefile('dir') let cafile = cadir.ctrlp#utils#lash().ctrlp#utils#cachefile('dir')
if g:ctrlp_newdir || !filereadable(cafile) if g:ctrlp_newdir || s:nocache() || !filereadable(cafile)
let [s:initcwd, g:ctrlp_alldirs] = [s:cwd, []] let [s:initcwd, g:ctrlp_alldirs] = [s:cwd, []]
cal s:globdirs(s:cwd, 0) cal s:globdirs(s:cwd, 0)
cal ctrlp#rmbasedir(g:ctrlp_alldirs) cal ctrlp#rmbasedir(g:ctrlp_alldirs)
@ -65,6 +66,7 @@ fu! ctrlp#dir#init(...)
let g:ctrlp_alldirs = ctrlp#utils#readfile(cafile) let g:ctrlp_alldirs = ctrlp#utils#readfile(cafile)
en en
en en
cal extend(s:dircounts, { s:cwd : len(g:ctrlp_alldirs) })
retu g:ctrlp_alldirs retu g:ctrlp_alldirs
endf endf

View File

@ -11,7 +11,7 @@ en
let [g:loaded_ctrlp_rtscript, g:ctrlp_newrts] = [1, 0] let [g:loaded_ctrlp_rtscript, g:ctrlp_newrts] = [1, 0]
cal add(g:ctrlp_ext_vars, { cal add(g:ctrlp_ext_vars, {
\ 'init': 'ctrlp#rtscript#init()', \ 'init': 'ctrlp#rtscript#init(s:caching)',
\ 'accept': 'ctrlp#acceptfile', \ 'accept': 'ctrlp#acceptfile',
\ 'lname': 'runtime scripts', \ 'lname': 'runtime scripts',
\ 'sname': 'rts', \ 'sname': 'rts',
@ -20,9 +20,16 @@ cal add(g:ctrlp_ext_vars, {
\ }) \ })
let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars) let s:id = g:ctrlp_builtins + len(g:ctrlp_ext_vars)
let s:filecounts = {}
" Utilities {{{1
fu! s:nocache()
retu !s:caching || ( s:caching > 1 && get(s:filecounts, s:cwd) < s:caching )
endf
" Public {{{1 " Public {{{1
fu! ctrlp#rtscript#init() fu! ctrlp#rtscript#init(caching)
if g:ctrlp_newrts let [s:caching, s:cwd] = [a:caching, getcwd()]
if g:ctrlp_newrts || s:nocache()
\ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[0] == &rtp ) \ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[0] == &rtp )
sil! cal ctrlp#progress('Indexing...') sil! cal ctrlp#progress('Indexing...')
let entries = split(globpath(&rtp, '**/*.*'), "\n") let entries = split(globpath(&rtp, '**/*.*'), "\n")
@ -31,13 +38,13 @@ fu! ctrlp#rtscript#init()
el el
let [entries, results] = g:ctrlp_rtscache[2:3] let [entries, results] = g:ctrlp_rtscache[2:3]
en en
let cwd = getcwd() if g:ctrlp_newrts || s:nocache()
if g:ctrlp_newrts \ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[:1] == [&rtp, s:cwd] )
\ || !( exists('g:ctrlp_rtscache') && g:ctrlp_rtscache[:1] == [&rtp, cwd] )
if !exists('echoed') | sil! cal ctrlp#progress('Processing...') | en if !exists('echoed') | sil! cal ctrlp#progress('Processing...') | en
let results = map(copy(entries), 'fnamemodify(v:val, '':.'')') let results = map(copy(entries), 'fnamemodify(v:val, '':.'')')
en en
let [g:ctrlp_rtscache, g:ctrlp_newrts] = [[&rtp, cwd, entries, results], 0] let [g:ctrlp_rtscache, g:ctrlp_newrts] = [[&rtp, s:cwd, entries, results], 0]
cal extend(s:filecounts, { s:cwd : len(results) })
retu results retu results
endf endf

View File

@ -164,10 +164,13 @@ Use this to set your own root markers in addition to the default ones (.git,
< <
*'g:ctrlp_use_caching'* *'g:ctrlp_use_caching'*
Set this to 0 to disable per-session caching. When disabled, caching will still Enable/Disable per-session caching: >
be enabled for directories that have more than 4000 files: >
let g:ctrlp_use_caching = 1 let g:ctrlp_use_caching = 1
< <
0 - Disable caching.
1 - Enable caching.
n - When bigger than 1, disable caching and use the number as the limit to
enable caching again.
Note: you can quickly purge the cache by pressing <F5> while inside CtrlP. Note: you can quickly purge the cache by pressing <F5> while inside CtrlP.
*'g:ctrlp_clear_cache_on_exit'* *'g:ctrlp_clear_cache_on_exit'*