aboutsummaryrefslogtreecommitdiff
path: root/.vim/after/plugin/fzf.vim
blob: 0694caaf67036f0ea114ed66b811452377ca6981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
let s:fzf_default_opts = $FZF_DEFAULT_OPTS

function! s:build_quickfix_list(lines) abort
  call setqflist(map(copy(a:lines), '{ "filename": v:val }'))
  copen
  cc
endfunction

function! s:format_qf_line(line) abort
  let parts = split(a:line, ':')
  return {
        \ 'filename': parts[0],
        \ 'lnum': parts[1],
        \ 'col': parts[2],
        \ 'text': join(parts[3:], ':')
        \ }
endfunction

function! s:fzf_qf(key, line) abort
  let filepath = expand('#' . a:line.bufnr . ':p')
  return filepath . ':' . a:line.lnum . ':' . a:line.col . ':' . a:line.text
endfunction

" TODO make it accept multiple lines
function! s:fzf_qf_sink(line) abort
  let line = s:format_qf_line(a:line[0])
  execute 'edit +' . line.lnum . ' ' . line.filename
endfunction

let s:command_git_main_branch = 'git symbolic-ref --short refs/remotes/origin/HEAD'
let s:command_git_patch_files_changed = 'git diff --name-only --merge-base $('
      \ . s:command_git_main_branch . ')'

command! FzfQuickFix call fzf#run({
      \ 'source': map(getqflist(), function('s:fzf_qf')),
      \ 'down': 20,
      \ 'sink*': function('s:fzf_qf_sink'),
      \ 'options': s:fzf_default_opts. ' --multi --prompt "QuickFix> "'
      \ })

if executable('fzf') && get(g:, 'loaded_fzf', 0) == 1
  let g:fzf_command_prefix = 'Fzf'

  command! -bang -nargs=* FzfRg
    \ call fzf#vim#grep(
    \   'rg --hidden --column --line-number --no-heading --color=always --smart-case --glob !.git ' . <q-args>, 1,
    \   fzf#vim#with_preview(), <bang>0)

  command! FzfGitChangedFiles call fzf#run({
        \ 'source': s:command_git_patch_files_changed,
        \ 'sink': 'e'
        \ })

  noremap <leader>ff :FzfFiles<CR>
  noremap <leader>fgf :FzfGFiles<CR>
  noremap <leader>fb :FzfBuffers<CR>
  noremap <leader>fm :FzfHistory<CR>
  noremap <leader>ft :FzfBTags<CR>
  noremap <leader>fT :FzfTags<CR>
  noremap <leader>fr :FzfRg<space>
  noremap <leader>fc :FzfCommands<CR>
  nnoremap <leader>fC :FzfGitChangedFiles<CR>
  noremap <leader>fgc :FzfBCommits<CR>
  noremap <leader>fgC :FzfCommits<CR>
  noremap <leader>fl :FzfBLines<CR>
  noremap <leader>fL :FzfLines<CR>
  noremap <leader>fq :FzfQuickFix<CR>

  let g:fzf_action = {
        \ 'ctrl-q': function('s:build_quickfix_list')
        \ }
  let g:fzf_layout = { 'down': '40%' }
  let g:fzf_preview_window = [ 'right:80%:hidden', 'ctrl-/' ]

  packadd fzf.vim
endif