Last active
May 28, 2022 06:21
-
-
Save Milly/48685e845040ec647a041d757edcd464 to your computer and use it in GitHub Desktop.
Grep command with ddu.vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" autoload/my/ddu.vim | |
" | |
" plugins: | |
" https://github.com/Shougo/ddu.vim | |
" https://github.com/Shougo/ddu-filter-matcher_substring | |
" https://github.com/Shougo/ddu-ui-ff | |
" https://github.com/Shougo/ddu-kind-file | |
" https://github.com/shun/ddu-source-rg | |
let s:rg_highlights = { | |
\ 'path': 'String', | |
\ 'lineNr': 'Number', | |
\ 'word': 'Search', | |
\} | |
function! my#ddu#setup() | |
" disable default command definition | |
let g:loaded_ddu_rg = 1 | |
" Setup ddu | |
"call ddu#custom#patch_global({ | |
"\... | |
"\}) | |
" `:Grep` command. | |
command! -nargs=+ -complete=custom,my#ddu#cmd_grep_complete Grep call my#ddu#cmd_grep([<f-args>]) | |
" Grep word under cursor with interactive mode | |
nnoremap <nowait> <Space>i <Cmd>call my#ddu#cmd_grep_interactive(expand('<cword>'))<CR> | |
vnoremap <nowait> <Space>i y<Cmd>call my#ddu#cmd_grep_interactive(@@)<CR> | |
" Grep word under cursor. | |
nnoremap <nowait> <Space>g <Cmd>call my#ddu#cmd_grep(['-F', '--', expand('<cword>')])<CR> | |
vnoremap <nowait> <Space>g y<Cmd>call my#ddu#cmd_grep(['-F', '--', @@])<CR> | |
endfunction | |
function! my#ddu#cmd_grep_interactive(input) abort | |
call ddu#start({ | |
\ 'name': 'grep', | |
\ 'volatile': v:true, | |
\ 'input': a:input, | |
\ 'sources': [{ | |
\ 'name': 'rg', | |
\ 'options': {'matchers': []}, | |
\ 'params': { | |
\ 'args': ['--json'], | |
\ 'highlights': s:rg_highlights, | |
\ }, | |
\ }], | |
\ 'uiParams': {'ff': { | |
\ 'autoResize': v:false, | |
\ 'ignoreEmpty': v:false, | |
\ 'startFilter': v:true, | |
\ }}, | |
\}) | |
endfunction | |
function! my#ddu#cmd_grep_complete(...) abort | |
let list = [ | |
\ '-F', '--fixed-strings', | |
\ '-S', '--smart-case', | |
\ '-i', '--ignore-case', | |
\ '-s', '--case-sensitive', | |
\ '-w', '--word-regexp', | |
\ '-x', '--line-regexp', | |
\ '-P', '--pcre2', | |
\ '--no-ignore', | |
\ '--', | |
\] | |
return join(list, "\n") | |
endfunction | |
function! my#ddu#cmd_grep(fargs) abort | |
let args = copy(a:fargs) | |
let path = '' | |
let opts = [] | |
let search = '' | |
let optend = 0 | |
while len(args) | |
let arg = remove(args, 0) | |
if arg ==# '--' | |
let optend = 1 | |
elseif !optend && arg[0] ==# '-' | |
call add(opts, arg) | |
elseif search ==# '' | |
let search = arg | |
elseif path ==# '' | |
let path = fnamemodify(arg, ':p') | |
if !isdirectory(path) && !filereadable(path) | |
echoe 'Grep: path does not exist:' path | |
return | |
endif | |
else | |
let search = '' | |
break | |
endif | |
endwhile | |
if search ==# '' | |
echoe 'usage: Grep [options] <search> [path]' | |
return | |
endif | |
call ddu#start({ | |
\ 'name': 'grep', | |
\ 'sources': [{ | |
\ 'name': 'rg', | |
\ 'params': { | |
\ 'args': ['--json'] + opts, | |
\ 'input': search, | |
\ 'path': path, | |
\ 'highlights': s:rg_highlights, | |
\ }, | |
\ }], | |
\}) | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment