mirror of
https://github.com/jremmen/vim-ripgrep.git
synced 2025-12-26 11:25:28 +08:00
94 lines
1.8 KiB
VimL
94 lines
1.8 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_rootfiles')
|
|
let g:rg_root_types = ['.git']
|
|
endif
|
|
|
|
fun! s:Rg(txt)
|
|
call s:RgGrepContext(function('s:RgSearch'), a:txt)
|
|
endfun
|
|
|
|
fun! s:RgSearch(txt)
|
|
silent! exe 'grep! ' . a:txt
|
|
if len(getqflist())
|
|
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
|
|
|
|
if exists('g:rg_derive_root')
|
|
call s:RgPathContext(a:search, a:txt)
|
|
else
|
|
call a:search(a:txt)
|
|
endif
|
|
|
|
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
|
|
|
|
command! -nargs=* Rg :call s:Rg(<q-args>)
|
|
command! RgDir :call s:RgRootDir()
|