Skip to content

Commit

Permalink
fix issue #176: slow start in neovim
Browse files Browse the repository at this point in the history
move much code to autoload
only load python module when LeaderF's command is executed.
  • Loading branch information
Yggdroot committed Jun 26, 2018
1 parent f3844ab commit d222b21
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 231 deletions.
8 changes: 4 additions & 4 deletions ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ Search existing issues and check the FAQ.
Make sure that you have the latest version of LeaderF.
-->

- vim or neovim?
- vim or neovim? <!-- use [x] to select, note: no space in between brackets -->
- [ ] vim
- [ ] neovim
- `vim --version` or `nvim --version`:

- Output of `vim --version` or `nvim --version`:
- Output of `:echo has("python")`:
- Output of `:echo has("python3")`:
- Output of `:echo &pythondll`(only vim, not neovim):
- Output of `:echo &pythonthreedll`(only vim, not neovim):
- Output of `:py print(sys.version)`:
- Output of `:py3 print(sys.version)`:
- Operating system:
- Output of `:echo g:Lf_Debug_Cmd`: <!-- should only provide this information if it's an issue of :Leaderf file -->
- Operating system: <!-- use [x] to select, note: no space in between brackets -->
- [ ] Linux
- [ ] Mac OS X
- [ ] Windows
Expand Down
199 changes: 199 additions & 0 deletions autoload/leaderf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,209 @@
" License: Apache License, Version 2.0
" ============================================================================

if exists('g:leaderf#loaded')
finish
else
let g:leaderf#loaded = 1
endif

if !exists("g:Lf_PythonVersion")
if has("python3")
let g:Lf_PythonVersion = 3
let g:Lf_py = "py3 "
elseif has("python")
let g:Lf_PythonVersion = 2
let g:Lf_py = "py "
else
echohl Error
echo "Error: LeaderF requires vim compiled with +python or +python3"
echohl None
finish
endif
else
if g:Lf_PythonVersion == 2
if has("python")
let g:Lf_py = "py "
else
echohl Error
echo 'LeaderF Error: has("python") == 0'
echohl None
finish
endif
else
if has("python3")
let g:Lf_py = "py3 "
else
echohl Error
echo 'LeaderF Error: has("python3") == 0'
echohl None
finish
endif
endif
endif

exec g:Lf_py "import vim, sys, os.path"
exec g:Lf_py "cwd = vim.eval('expand(\"<sfile>:p:h\")')"
exec g:Lf_py "sys.path.insert(0, os.path.join(cwd, 'leaderf', 'python'))"

function! s:InitVar(var, value)
if !exists(a:var)
exec 'let '.a:var.'='.string(a:value)
endif
endfunction

function! s:InitDict(var, dict)
if !exists(a:var)
exec 'let '.a:var.'='.string(a:dict)
else
let tmp = a:dict
for [key, value] in items(eval(a:var))
let tmp[key] = value
endfor
exec 'let '.a:var.'='.string(tmp)
endif
endfunction

call s:InitVar('g:Lf_WindowHeight', '0.5')
call s:InitVar('g:Lf_TabpagePosition', 2)
call s:InitVar('g:Lf_ShowRelativePath', 1)
call s:InitVar('g:Lf_DefaultMode', 'NameOnly')
call s:InitVar('g:Lf_CursorBlink', 1)
call s:InitVar('g:Lf_NeedCacheTime', '1.5')
call s:InitVar('g:Lf_NumberOfCache', 5)
call s:InitVar('g:Lf_UseMemoryCache', 1)
call s:InitVar('g:Lf_IndexTimeLimit', 120)
call s:InitVar('g:Lf_FollowLinks', 0)
call s:InitVar('g:Lf_DelimiterChar', ';')
call s:InitVar('g:Lf_MruFileExclude', [])
call s:InitVar('g:Lf_MruMaxFiles', 100)
call s:InitVar('g:Lf_HighlightIndividual', 1)
call s:InitVar('g:Lf_NumberOfHighlight', 100)
call s:InitVar('g:Lf_WildIgnore', {
\ 'dir': [],
\ 'file': []
\})
call s:InitVar('g:Lf_MruWildIgnore', {
\ 'dir': [],
\ 'file': []
\})
if &encoding ==? "utf-8"
call s:InitVar('g:Lf_StlSeparator', {
\ 'left': '',
\ 'right': '',
\ 'font': ''
\})
else
call s:InitVar('g:Lf_StlSeparator', {
\ 'left': '',
\ 'right': '',
\ 'font': ''
\})
endif
call s:InitVar('g:Lf_StlPalette', {})
call s:InitVar('g:Lf_Ctags', 'ctags')
call s:InitVar('g:Lf_PreviewCode', 0)
call s:InitVar('g:Lf_UseVersionControlTool', 1)
call s:InitVar('g:Lf_RememberLastSearch', 0)
call s:InitVar('g:Lf_UseCache', 1)
call s:InitVar('g:Lf_RootMarkers', ['.git', '.hg', '.svn'])
call s:InitVar('g:Lf_WorkingDirectoryMode', 'c')
call s:InitVar('g:Lf_ShowHidden', 0)
call s:InitDict('g:Lf_PreviewResult', {
\ 'File': 0,
\ 'Buffer': 0,
\ 'Mru': 0,
\ 'Tag': 0,
\ 'BufTag': 1,
\ 'Function': 1,
\ 'Line': 0,
\ 'Colorscheme': 0
\})
call s:InitDict('g:Lf_NormalMap', {})
call s:InitVar('g:Lf_Extensions', {})

let s:Lf_CommandMap = {
\ '<C-A>': ['<C-A>'],
\ '<C-B>': ['<C-B>'],
\ '<C-C>': ['<C-C>'],
\ '<C-D>': ['<C-D>'],
\ '<C-E>': ['<C-E>'],
\ '<C-F>': ['<C-F>'],
\ '<C-G>': ['<C-G>'],
\ '<C-H>': ['<C-H>'],
\ '<C-J>': ['<C-J>'],
\ '<C-K>': ['<C-K>'],
\ '<C-L>': ['<C-L>'],
\ '<C-N>': ['<C-N>'],
\ '<C-O>': ['<C-O>'],
\ '<C-P>': ['<C-P>'],
\ '<C-Q>': ['<C-Q>'],
\ '<C-R>': ['<C-R>'],
\ '<C-S>': ['<C-S>'],
\ '<C-T>': ['<C-T>'],
\ '<C-U>': ['<C-U>'],
\ '<C-V>': ['<C-V>', '<S-Insert>'],
\ '<C-W>': ['<C-W>'],
\ '<C-X>': ['<C-X>'],
\ '<C-Y>': ['<C-Y>'],
\ '<C-Z>': ['<C-Z>'],
\ '<C-]>': ['<C-]>'],
\ '<F1>': ['<F1>'],
\ '<F2>': ['<F2>'],
\ '<F3>': ['<F3>'],
\ '<F4>': ['<F4>'],
\ '<F5>': ['<F5>'],
\ '<F6>': ['<F6>'],
\ '<F7>': ['<F7>'],
\ '<F8>': ['<F8>'],
\ '<F9>': ['<F9>'],
\ '<F10>': ['<F10>'],
\ '<F11>': ['<F11>'],
\ '<F12>': ['<F12>'],
\ '<CR>': ['<CR>'],
\ '<BS>': ['<BS>'],
\ '<Tab>': ['<Tab>', '<C-I>'],
\ '<Del>': ['<Del>'],
\ '<Esc>': ['<Esc>'],
\ '<Up>': ['<Up>'],
\ '<Down>': ['<Down>'],
\ '<Left>': ['<Left>'],
\ '<Right>': ['<Right>'],
\ '<Home>': ['<Home>', '<C-B>'],
\ '<End>': ['<End>'],
\ '<PageUp>': ['<PageUp>'],
\ '<PageDown>': ['<PageDown>'],
\ '<S-Left>': ['<S-Left>'],
\ '<S-Right>': ['<S-Right>'],
\ '<LeftMouse>': ['<LeftMouse>'],
\ '<RightMouse>': ['<RightMouse>'],
\ '<MiddleMouse>': ['<MiddleMouse>'],
\ '<2-LeftMouse>': ['<2-LeftMouse>'],
\ '<C-LeftMouse>': ['<C-LeftMouse>'],
\ '<S-LeftMouse>': ['<S-LeftMouse>']
\}

function! s:InitCommandMap(var, dict)
if !exists(a:var)
exec 'let '.a:var.'='.string(a:dict)
else
let tmp = a:dict
for [key, value] in items(eval(a:var))
call filter(tmp, 'v:key !=? key')
for i in value
if index(['<TAB>', '<C-I>'], toupper(i)) >= 0
call filter(tmp, "v:key != '<Tab>'")
endif
call filter(tmp, '!empty(filter(tmp[v:key], "v:val !=? i"))')
endfor
let tmp[toupper(key)] = map(value, 'toupper(v:val)')
endfor
exec 'let '.a:var.'='.string(tmp)
endif
endfunction

call s:InitCommandMap('g:Lf_CommandMap', s:Lf_CommandMap)

function! leaderf#versionCheck()
if g:Lf_PythonVersion == 2 && pyeval("sys.version_info < (2, 7)")
echohl Error
Expand Down
2 changes: 0 additions & 2 deletions autoload/leaderf/BufTag.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ if leaderf#versionCheck() == 0 " this check is necessary
finish
endif

let g:Lf_bufTagExpl_loaded = 1

exec g:Lf_py "from leaderf.bufTagExpl import *"

function! leaderf#BufTag#Maps()
Expand Down
2 changes: 0 additions & 2 deletions autoload/leaderf/File.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ if leaderf#versionCheck() == 0 " this check is necessary
finish
endif

let g:Lf_fileExpl_loaded = 1

exec g:Lf_py "from leaderf.fileExpl import *"

function! leaderf#File#Maps()
Expand Down
2 changes: 0 additions & 2 deletions autoload/leaderf/Function.vim
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ if leaderf#versionCheck() == 0 " this check is necessary
finish
endif

let g:Lf_functionExpl_loaded = 1

exec g:Lf_py "from leaderf.functionExpl import *"

function! leaderf#Function#Maps()
Expand Down
8 changes: 8 additions & 0 deletions autoload/leaderf/python/leaderf/bufExpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def __init__(self):
self._max_bufname_len = 0

def getContent(self, *args, **kwargs):
mru_bufnrs = []
for num in reversed(lfEval("g:Lf_MruBufnrs")):
if num not in mru_bufnrs:
mru_bufnrs.append(int(num))
for num in reversed(mru_bufnrs):
mru.setBufferTimestamp(num)
lfCmd("let g:Lf_MruBufnrs = []")

if "--all" not in kwargs.get("arguments", {}):
if "--tabpage" not in kwargs.get("arguments", {}):
buffers = {b.number: b for b in vim.buffers
Expand Down
49 changes: 28 additions & 21 deletions autoload/leaderf/python/leaderf/mru.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def getCacheFileName(self):
def normalize(self, name):
if '~' in name:
name = os.path.expanduser(name)
name = os.path.abspath(name)
if sys.platform[:3] == 'win':
if name[:4] == '\\\\?\\' and os.path.isabs(name):
if os.path.isabs(name[4:]) and name[5:6] == ':':
Expand All @@ -48,29 +47,37 @@ def normalize(self, name):
name = name[:11].lower() + name[11:]
return name

def saveToCache(self, buf_name):
buf_name = self.normalize(buf_name)
if True in (fnmatch.fnmatch(buf_name, i)
for i in lfEval("g:Lf_MruFileExclude")):
def saveToCache(self, buf_name_list):
buf_names = []
for name in buf_name_list:
name = self.normalize(name)
if True in (fnmatch.fnmatch(name, i)
for i in lfEval("g:Lf_MruFileExclude")):
continue
buf_names.append(name)

if not buf_names:
return
nocase = False
compare = buf_name
if sys.platform[:3] == 'win' or sys.platform in ('cygwin', 'msys'):
nocase = True
compare = buf_name.lower()

with lfOpen(self._cache_file, 'r+', errors='ignore') as f:
lines = f.readlines()
for i, line in enumerate(lines):
text = line.rstrip()
if (compare == text) or (nocase and compare == text.lower()):
if i == 0:
return
del lines[i]
break

lines.insert(0, buf_name + '\n')
if len(lines) > int(lfEval("g:Lf_MruMaxFiles")):
del lines[-1]
for name in buf_names:
nocase = False
compare = name
if sys.platform[:3] == 'win' or sys.platform in ('cygwin', 'msys'):
nocase = True
compare = name.lower()

for i, line in enumerate(lines):
text = line.rstrip()
if (compare == text) or (nocase and compare == text.lower()):
del lines[i]
break

lines = [name + '\n' for name in buf_names] + lines
max_files = int(lfEval("g:Lf_MruMaxFiles"))
if len(lines) > max_files:
del lines[max_files:]
f.seek(0)
f.truncate(0)
f.writelines(lines)
Expand Down
7 changes: 5 additions & 2 deletions autoload/leaderf/python/leaderf/mruExpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def __init__(self):
self._max_bufname_len = 0

def getContent(self, *args, **kwargs):
mru.saveToCache(lfEval("readfile(g:Lf_MruCacheFileName)"))
lfCmd("call writefile([], g:Lf_MruCacheFileName)")

with lfOpen(mru.getCacheFileName(), 'r+', errors='ignore') as f:
lines = f.readlines()
lines = [name for name in lines if os.path.exists(lfDecode(name.rstrip()))]
Expand Down Expand Up @@ -81,6 +84,7 @@ def getPrefixLength(self):
def getMaxBufnameLen(self):
return self._max_bufname_len


#*****************************************************
# MruExplManager
#*****************************************************
Expand Down Expand Up @@ -187,14 +191,13 @@ def deleteMru(self):
line = vim.current.line
dirname = self._getDigest(line, 2)
basename = self._getDigest(line, 1)
self._explorer.delFromCache(escSpecial(dirname + basename))
self._explorer.delFromCache(dirname + basename)
if len(self._content) > 0:
self._content.remove(line)
del vim.current.line
lfCmd("setlocal nomodifiable")



#*****************************************************
# mruExplManager is a singleton
#*****************************************************
Expand Down
Loading

0 comments on commit d222b21

Please sign in to comment.