Skip to content

Commit

Permalink
fix issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Yggdroot committed Nov 10, 2017
1 parent 11a455b commit c253ca4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
71 changes: 71 additions & 0 deletions autoload/leaderf/python/leaderf/fileExpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,77 @@ def _createHelp(self):
help.append('" ---------------------------------------------------------')
return help

def _nearestAncestor(self, markers, path):
"""
return the nearest ancestor path(including itself) of `path` that contains
one of files or directories in `markers`.
`markers` is a list of file or directory names.
"""
if os.name == 'nt':
# e.g. C:\\
root = os.path.splitdrive(os.path.abspath(path))[0] + os.sep
else:
root = '/'

path = os.path.abspath(path)
while path != root:
for name in markers:
if os.path.exists(os.path.join(path, name)):
return path
path = os.path.abspath(os.path.join(path, ".."))

for name in markers:
if os.path.exists(os.path.join(path, name)):
return path

return ""

def startExplorer(self, win_pos, *args, **kwargs):
if len(args) > 0: # behavior no change for `LeaderfFile <directory>`
super(FileExplManager, self).startExplorer(win_pos, *args, **kwargs)
return

orig_cwd = os.getcwd()
root_markers = lfEval("g:Lf_RootMarkers")
mode = lfEval("g:Lf_WorkingDirectoryMode")

fall_back = False
if 'a' in mode:
working_dir = self._nearestAncestor(root_markers, orig_cwd)
if working_dir: # there exists a root marker in nearest ancestor path
os.chdir(working_dir)
else:
fall_back = True
elif 'A' in mode:
if vim.current.buffer.name:
working_dir = self._nearestAncestor(root_markers, os.path.dirname(vim.current.buffer.name))
else:
working_dir = ""
if working_dir: # there exists a root marker in nearest ancestor path
os.chdir(working_dir)
else:
fall_back = True
else:
fall_back = True

if fall_back:
if 'f' in mode:
if vim.current.buffer.name:
os.chdir(os.path.dirname(vim.current.buffer.name))
elif 'F' in mode:
if vim.current.buffer.name and \
not os.path.dirname(vim.current.buffer.name).startswith(orig_cwd):
os.chdir(os.path.dirname(vim.current.buffer.name))

try:
super(FileExplManager, self).startExplorer(win_pos, *args, **kwargs)

if int(lfEval("&autochdir")) == 0:
os.chdir(orig_cwd)
except:
os.chdir(orig_cwd)


#*****************************************************
# fileExplManager is a singleton
#*****************************************************
Expand Down
26 changes: 26 additions & 0 deletions doc/leaderf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,32 @@ g:Lf_NormalMap *g:Lf_NormalMap*
\ "Colorscheme": []
\}
<
g:Lf_RootMarkers *g:Lf_RootMarkers*
Use this option to set the root markers.
e.g., >
let g:Lf_RootMarkers = ['.project', '.project2']
<
Default value is ['.git', '.hg', '.svn']

g:Lf_WorkingDirectoryMode *g:Lf_WorkingDirectoryMode*
This option customizes LeaderF's working directory.
e.g., >
let g:Lf_WorkingDirectoryMode = 'Ac'
<
c - the directory of the current working directory.(default)
a - the nearest ancestor of current working directory that contains one of
directories or files defined in |g:Lf_RootMarkers|. Fall back to 'c' if
no such ancestor directory found.
A - the nearest ancestor of current file that contains one of directories
or files defined in |g:Lf_RootMarkers|. Fall back to 'c' if no such
ancestor directory found.
f - the directory of the current file.
F - if the current working directory is not the direct ancestor of current
file, use the directory of the current file as LeaderF's working
directory, otherwise, same as 'c'.
Note: if "f", "F" is included with "a" or "A", use the behavior of "f" or
"F"(as a fallback) when a root can't be found.

g:Lf_CommandMap *g:Lf_CommandMap*
Use this option to customize the mappings inside LeaderF's
prompt(|leaderf-prompt|).
Expand Down
2 changes: 2 additions & 0 deletions plugin/leaderf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ 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:InitDict('g:Lf_PreviewResult', {
\ 'File': 0,
\ 'Buffer': 0,
Expand Down

0 comments on commit c253ca4

Please sign in to comment.