Skip to content

Commit

Permalink
Create fzf-contrib for Vim, cleanup .vimrc
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Apr 10, 2017
1 parent c377939 commit 335783f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 96 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ To add your own Vim, Tmux or Zsh plugin you can just clone it to proper
* [editorconfig-vim](https://github.com/editorconfig/editorconfig-vim):
EditorConfig plugin for Vim
* [fzf.vim](https://github.com/junegunn/fzf.vim): fzf :heart: vim
* [fzf-contrib](https://github.com/deathbeam/dotfiles/tree/master/vim/.vim/bundle/fzf-contrib):
Completion menu using FZF, tab-completion, support for `ripgrep` in FZF
* [syntastic](https://github.com/scrooloose/syntastic): Syntax checking hacks
for vim
* [tsuquyomi](https://github.com/Quramy/tsuquyomi): A Vim plugin for TypeScript
Expand Down
2 changes: 1 addition & 1 deletion vim/.vim/bundle/editorconfig-vim
56 changes: 56 additions & 0 deletions vim/.vim/bundle/fzf-contrib/autoload/fzf/contrib.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function! fzf#contrib#complete(findstart, base)
let Func = function(get(g:, 'fzf#contrib#completefunc', &omnifunc))
let results = Func(a:findstart, a:base)
echom a:base

if a:findstart
return results
endif

let words = type(results) == type({}) && has_key(results, 'words')
\ ? map(results.words, 'v:val.word . "\t" . v:val.menu')
\ : results

let results = fzf#run({
\ 'source': words,
\ 'down': '~40%',
\ 'options': printf('--query "%s" +s -m', a:base)
\ })

if exists('*UltiSnips#ExpandSnippet')
\ && len(results) == 1
\ && len(results[0]) > 1
\ && match(split(results[0], "\t")[1], '\[snip\]') != -1
call feedkeys("\<c-r>=UltiSnips#ExpandSnippet()\<cr>")
endif

return map(results, 'split(v:val, "\t")[0]')
endfunction

function! fzf#contrib#trigger_complete()
setlocal completefunc=fzf#contrib#complete
setlocal completeopt=menu
call feedkeys("\<c-x>\<c-u>", 'n')
endfunction

function! fzf#contrib#tab_complete()
let col = col('.') - 1

if !col || getline('.')[col - 1] !~# '\k'
call feedkeys("\<tab>", 'n')
else
call fzf#contrib#trigger_complete()
endif
endfunction

function! fzf#contrib#grep(query, ...)
if executable('rg')
let query = empty(a:query) ? '^.' : a:query
let args = copy(a:000)
let opts = len(args) > 1 ? remove(args, 0) : ''
let command = opts . ' ' . "'".substitute(query, "'", "'\\\\''", 'g')."'"
return call('fzf#vim#grep', extend(['rg --no-heading --column --color always '.command, 1], args))
endif

return call('fzf#vim#ag', insert(copy(a:000), a:query, 0))
endfunction
3 changes: 3 additions & 0 deletions vim/.vim/bundle/fzf-contrib/plugin/fzf-contrib.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
imap <c-x><c-j> <c-o>:call fzf#contrib#trigger_complete()<cr>
inoremap <silent> <tab> <c-o>:call fzf#contrib#tab_complete()<cr>
command! -bang -nargs=* Grep call fzf#contrib#grep(<q-args>, <bang>0)
2 changes: 1 addition & 1 deletion vim/.vim/bundle/tsuquyomi
2 changes: 1 addition & 1 deletion vim/.vim/bundle/vim-snippets
2 changes: 1 addition & 1 deletion vim/.vim/bundle/vim-test
112 changes: 20 additions & 92 deletions vim/.vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ if has('clipboard')
set clipboard=unnamedplus
endif

" use syntax complete if nothing else available
autocmd VimRc Filetype *
\ if &omnifunc == ""
\| setlocal omnifunc=syntaxcomplete#Complete
\| endif

" Use faster grep alternatives if possible
if executable('rg')
set grepprg=rg\ --vimgrep\ --no-heading
set grepformat^=%f:%l:%c:%m
elseif executable('ag')
set grepprg=ag\ --nogroup\ --nocolor\ --vimgrep
set grepformat^=%f:%l:%c:%m
endif

" }}}

" User interface {{{
Expand Down Expand Up @@ -134,21 +149,6 @@ set synmaxcol=256
" Automatically rebalance windows on vim resize
autocmd VimRc VimResized * :wincmd =

" use syntax complete if nothing else available
autocmd VimRc Filetype *
\ if &omnifunc == ""
\| setlocal omnifunc=syntaxcomplete#Complete
\| endif

" Use faster grep alternatives if possible
if executable('rg')
set grepprg=rg\ --vimgrep\ --no-heading
set grepformat^=%f:%l:%c:%m
elseif executable('ag')
set grepprg=ag\ --nogroup\ --nocolor\ --vimgrep
set grepformat^=%f:%l:%c:%m
endif

" }}}

" Colors {{{
Expand Down Expand Up @@ -205,7 +205,7 @@ set fileformats=unix,dos,mac

" }}}

" Mappings {{{
" Mappings and commands {{{

" With a map leader it's possible to do extra key combinations
let mapleader = ' '
Expand Down Expand Up @@ -302,85 +302,14 @@ let g:syntastic_check_on_wq = 0
set statusline+=%#warningmsg#%{SyntasticStatuslineFlag()}%*

" Vim Test
let test#strategy = 'make'
let g:test#strategy = 'make'
nmap <silent> <leader>mt :TestFile<CR>
nmap <silent> <leader>mT :TestSuite<CR>
nmap <silent> <leader>mtt :TestNearest<CR>
" FZF {{{

" Fuzzy completion
function! FuzzyCompleteFunc(findstart, base)
let Func = function(get(g:, 'fuzzyfunc', &omnifunc))
let results = Func(a:findstart, a:base)

if a:findstart
return results
endif

if type(results) == type({}) && has_key(results, 'words')
let l:words = []
for result in results.words
call add(words, result.word . ' ' . result.menu)
endfor
elseif len(results)
let l:words = results
endif

if len(l:words)
let result = fzf#run({ 'source': l:words, 'down': '~40%', 'options': printf('--query "%s" +s', a:base) })

if empty(result)
return [ a:base ]
endif

return [ split(result[0])[0] ]
else
return [ a:base ]
endif
endfunction

let g:fuzzyfunc = 'completor#completefunc'

" Custom trigger for our fuzzy function
function! FuzzyFuncTrigger()
setlocal completefunc=FuzzyCompleteFunc
setlocal completeopt=menu
call feedkeys("\<c-x>\<c-u>", 'n')
endfunction

imap <c-x><c-j> <c-o>:call FuzzyFuncTrigger()<cr>
" Awesome TAB fuzzy completion
function! TabComplete()
let col = col('.') - 1

if !col || getline('.')[col - 1] !~# '\k'
call feedkeys("\<tab>", 'n')
return
endif

call feedkeys("\<c-x>\<c-j>")
endfunction

inoremap <silent> <tab> <c-o>:call TabComplete()<cr>
" Try to use ripgrep, otherwise fallback to ag
function! s:fzf_rg(query, ...)
if executable('rg')
let query = empty(a:query) ? '^.' : a:query
let args = copy(a:000)
let opts = len(args) > 1 ? remove(args, 0) : ''
let command = opts . ' ' . "'".substitute(query, "'", "'\\\\''", 'g')."'"
return call('fzf#vim#grep', extend(['rg --no-heading --column --color always '.command, 1], args))
endif

return call('fzf#vim#ag', insert(copy(a:000), a:query, 0))
endfunction

command! -bang -nargs=* Find call s:fzf_rg(<q-args>, <bang>0)

nmap <leader>/ :Find<cr>
" FZF
let g:fzf#contrib#completefunc = 'completor#completefunc'
nmap <leader>/ :Grep<cr>
nmap <leader>T :Tags<cr>
nmap <leader>t :BTags<cr>
nmap <leader>F :Files<cr>
Expand All @@ -392,7 +321,6 @@ nmap <leader>w :Windows<cr>
nmap <leader>s :Snippets<cr>
nmap <leader>c :Commits<cr>
nmap <leader>? :Helptags<cr>
" }}}
" }}}

Expand Down

0 comments on commit 335783f

Please sign in to comment.