Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HgFiles command for Mercurial repositories. #1517

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion autoload/fzf/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -680,11 +680,17 @@ endfunction
" ------------------------------------------------------------------

function! s:get_git_root(dir)
let dir = len(a:dir) ? a:dir : substitute(split(expand('%:p:h'), '[/\\]\.git\([/\\]\|$\)')[0], '^fugitive://', '', '')
let dir = len(a:dir) ? a:dir : substitute(split(getcwd(), '[/\\]\.git\([/\\]\|$\)')[0], '^fugitive://', '', '')
let root = systemlist('git -C ' . fzf#shellescape(dir) . ' rev-parse --show-toplevel')[0]
return v:shell_error ? '' : (len(a:dir) ? fnamemodify(a:dir, ':p') : root)
endfunction

function! s:get_hg_root(dir)
let dir = len(a:dir) ? a:dir : split(getcwd(), '[/\\]\.hg\([/\\]\|$\)')[0]
let root = systemlist('hg --cwd ' . fzf#shellescape(dir) . ' root')[0]
return v:shell_error ? '' : (len(a:dir) ? fnamemodify(a:dir, ':p') : root)
endfunction

function! s:version_requirement(val, min)
for idx in range(0, len(a:min) - 1)
let v = get(a:val, idx, 0)
Expand Down Expand Up @@ -747,6 +753,61 @@ function! fzf#vim#gitfiles(args, ...)
return s:fzf('gfiles-diff', wrapped, a:000)
endfunction

function! fzf#vim#hgfiles(args, ...)
let dir = get(get(a:, 1, {}), 'dir', '')
let root = s:get_hg_root(dir)
if empty(root)
return s:warn('Not in hg repo')
endif
let prefix = 'hg --cwd ' . fzf#shellescape(root) . ' '
if a:args != '?'
let source = prefix . 'files -0 ' . a:args
return s:fzf('hgfiles', {
\ 'source': source,
\ 'dir': root,
\ 'options': '-m --read0 --prompt "HgFiles> "'
\}, a:000)
endif

" Here be dragons!
" We're trying to access the common sink function that fzf#wrap injects to
" the options dictionary.
let bar = s:is_win ? '^|' : '|'
let diff_prefix = 'hg --cwd ' . s:escape_for_bash(root) . ' '
let preview = printf(
\ s:bash() . ' -c "if [[ {1} =~ M ]]; then %s; else %s {-1}; fi"',
\ executable('delta')
\ ? diff_prefix . 'diff -- {-1} ' . bar . ' delta --width $FZF_PREVIEW_COLUMNS --file-style=omit ' . bar . ' sed 1d'
\ : diff_prefix . 'diff --color=always -- {-1} ' . bar . ' sed 1,4d',
\ s:escape_for_bash(s:bin.preview))
let wrapped = fzf#wrap({
\ 'source': prefix . '-c color.status=always status --short --untracked-files=all',
\ 'dir': root,
\ 'options': ['--ansi', '--multi', '--nth', '2..,..', '--tiebreak=index', '--prompt', 'GitFiles?> ', '--preview', preview]
\})
call s:remove_layout(wrapped)
let wrapped.common_sink = remove(wrapped, 'sink*')
function! wrapped.newsink(lines)
let lines = extend(a:lines[0:0], map(a:lines[1:], 'substitute(v:val[3:], ".* -> ", "", "")'))
return self.common_sink(lines)
endfunction
let wrapped['sink*'] = remove(wrapped, 'newsink')
return s:fzf('hgfiles-diff', wrapped, a:000)
endfunction

function! fzf#vim#vcfiles(args, ...)
let dir = get(get(a:, 1, {}), 'dir', '')
let root = s:get_git_root(dir)
if !empty(root)
return call('fzf#vim#gitfiles', [a:args] + a:000)
endif
let root = s:get_hg_root(dir)
if !empty(root)
return call('fzf#vim#hgfiles', [a:args] + a:000)
endif
return s:warn('Neither git nor hg repo')
endfunction

" ------------------------------------------------------------------
" Buffers
" ------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions plugin/fzf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ call s:defs([
\'command! -bang -nargs=? -complete=dir Files call fzf#vim#files(<q-args>, fzf#vim#with_preview(), <bang>0)',
\'command! -bang -nargs=? GitFiles call fzf#vim#gitfiles(<q-args>, fzf#vim#with_preview(<q-args> == "?" ? { "placeholder": "" } : {}), <bang>0)',
\'command! -bang -nargs=? GFiles call fzf#vim#gitfiles(<q-args>, fzf#vim#with_preview(<q-args> == "?" ? { "placeholder": "" } : {}), <bang>0)',
\'command! -bang -nargs=? HgFiles call fzf#vim#hgfiles(<q-args>, fzf#vim#with_preview(<q-args> == "?" ? { "placeholder": "" } : {}), <bang>0)',
\'command! -bang -nargs=? VcFiles call fzf#vim#vcfiles(<q-args>, fzf#vim#with_preview(<q-args> == "?" ? { "placeholder": "" } : {}), <bang>0)',
\'command! -bar -bang -nargs=? -complete=buffer Buffers call fzf#vim#buffers(<q-args>, fzf#vim#with_preview({ "placeholder": "{1}" }), <bang>0)',
\'command! -bang -nargs=* Lines call fzf#vim#lines(<q-args>, <bang>0)',
\'command! -bang -nargs=* BLines call fzf#vim#buffer_lines(<q-args>, <bang>0)',
Expand Down