vimPlugins.vim-ripgrep/plugin/vim-ripgrep.vim
2018-08-14 12:01:21 +08:00

123 lines
2.4 KiB
VimL

if exists('g:loaded_rg') || &cp
finish
endif
let g:loaded_rg = 1
if !exists('g:rg_binary')
let g:rg_binary = 'rg'
endif
if !exists('g:rg_format')
let g:rg_format = "%f:%l:%c:%m"
endif
if !exists('g:rg_command')
let g:rg_command = g:rg_binary . ' --vimgrep'
endif
if !exists('g:rg_root_types')
let g:rg_root_types = ['.git']
endif
if !exists('g:rg_window_location')
let g:rg_window_location = 'botright'
endif
fun! s:Rg(txt)
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
endfun
fun! s:RgSearchTerm(txt)
if empty(a:txt)
return expand("<cword>")
else
return a:txt
endif
endfun
fun! s:RgSearch(txt)
silent! exe 'grep! ' . a:txt
if len(getqflist())
exe g:rg_window_location 'copen'
redraw!
if exists('g:rg_highlight')
call s:RgHighlight(a:txt)
endif
else
cclose
redraw!
echo "No match found for " . a:txt
endif
endfun
fun! s:RgGrepContext(search, txt)
let l:grepprgb = &grepprg
let l:grepformatb = &grepformat
let &grepprg = g:rg_command
let &grepformat = g:rg_format
let l:te = &t_te
let l:ti = &t_ti
let l:shellpipe_bak=&shellpipe
set t_te=
set t_ti=
let &shellpipe="&>"
if exists('g:rg_derive_root')
call s:RgPathContext(a:search, a:txt)
else
call a:search(a:txt)
endif
let &shellpipe=l:shellpipe_bak
let &t_te=l:te
let &t_ti=l:ti
let &grepprg = l:grepprgb
let &grepformat = l:grepformatb
endfun
fun! s:RgPathContext(search, txt)
let l:cwdb = getcwd()
exe 'lcd '.s:RgRootDir()
call a:search(a:txt)
exe 'lcd '.l:cwdb
endfun
fun! s:RgHighlight(txt)
let @/=escape(substitute(a:txt, '"', '', 'g'), '|')
call feedkeys(":let &hlsearch=1\<CR>", 'n')
endfun
fun! s:RgRootDir()
let l:cwd = getcwd()
let l:dirs = split(getcwd(), '/')
for l:dir in reverse(copy(l:dirs))
for l:type in g:rg_root_types
let l:path = s:RgMakePath(l:dirs, l:dir)
if s:RgHasFile(l:path.'/'.l:type)
return l:path
endif
endfor
endfor
return l:cwd
endfun
fun! s:RgMakePath(dirs, dir)
return '/'.join(a:dirs[0:index(a:dirs, a:dir)], '/')
endfun
fun! s:RgHasFile(path)
return filereadable(a:path) || isdirectory(a:path)
endfun
fun! s:RgShowRoot()
if exists('g:rg_derive_root')
echo s:RgRootDir()
else
echo getcwd()
endif
endfun
command! -nargs=* -complete=file Rg :call s:Rg(<q-args>)
command! -complete=file RgRoot :call s:RgShowRoot()