diff --git a/.gitignore b/.gitignore index a62234bb6..281c3040a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,11 @@ .* __pycache__ *.pyc +*.log +# Ignore all the vim files on the top directory +/*.vim +/*.rc +[1-9]*.vim /doc/tags /target diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e33dda07..6678fb203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ CHANGELOG - Add the preview support for `:Clap grep`. - Add the preview support for `:Clap blines`. - Support running from any specified directory by passing it via the last argument for `:Clap files` and `:Clap grep`. +- Add limited fzf like search syntax([#127](https://github.com/liuchengxu/vim-clap/issues/127)) for `:Clap grep`.([#150](https://github.com/liuchengxu/vim-clap/issues/150)) ### Changed diff --git a/autoload/clap/filter.vim b/autoload/clap/filter.vim index e1e56e26d..e3035898d 100644 --- a/autoload/clap/filter.vim +++ b/autoload/clap/filter.vim @@ -134,11 +134,7 @@ let s:can_use_python = v:false if s:py_exe isnot v:null function! s:setup_python() abort - if has('nvim') -execute s:py_exe "<< EOF" -from clap.fzy import clap_fzy -EOF - else + if !has('nvim') execute s:py_exe "<< EOF" import sys from os.path import normpath, join @@ -147,14 +143,13 @@ plugin_root_dir = vim.eval('g:clap#autoload_dir') python_root_dir = normpath(join(plugin_root_dir, '..', 'pythonx')) sys.path.insert(0, python_root_dir) import clap - -from clap.fzy import clap_fzy EOF endif endfunction try call s:setup_python() + execute s:py_exe 'from clap.fzy import clap_fzy' let s:can_use_python = v:true catch endtry diff --git a/autoload/clap/provider/grep.vim b/autoload/clap/provider/grep.vim index 237275568..ca37a9a85 100644 --- a/autoload/clap/provider/grep.vim +++ b/autoload/clap/provider/grep.vim @@ -31,25 +31,59 @@ function! s:draw_icon(line) abort return a:line endfunction -function! s:cmd(query) abort - if !executable(s:grep_executable) - call g:clap.abort(s:grep_executable . ' not found') - return - endif +" Translate `[query] *.rs` to `[query] -g '*.rs'` for rg. +function! s:translate_query_and_opts(query) abort if has_key(g:clap.context, 'opt') let grep_opts = s:grep_opts.' '.g:clap.context.opt else let grep_opts = s:grep_opts endif - if !empty(g:clap.provider.args) - let dir = g:clap.provider.args[-1] - if isdirectory(expand(dir)) - let g:__clap_provider_cwd = dir + " Exact mode + if a:query[0] ==# "'" + return [grep_opts, a:query[1:]] + endif + + let ridx = strridx(a:query, ' ') + if ridx == -1 + let query = a:query + else + " .vim => -g '*.vim' + if a:query[ridx+1:] =~# '^\.\a\+' + let ft = matchstr(a:query[ridx+1:], '^\.\zs\(.\+\)') + let grep_opts .= ' -g "*.'.ft.'"' + let query = a:query[:ridx-1] + return [grep_opts, query] + endif + + let matched = matchlist(a:query[ridx+1:], '^\(.*\)\.\(.*\)$') + if !empty(matched) + let grep_opts .= ' -g "'.a:query[ridx+1:].'"' + let query = a:query[:ridx-1] + else + let query = a:query endif endif - let cmd = printf(s:grep_cmd_format, s:grep_executable, grep_opts, a:query) + let query = join(split(query), '.*') + + return [grep_opts, query] +endfunction + +function! s:cmd(query) abort + if !executable(s:grep_executable) + call g:clap.abort(s:grep_executable . ' not found') + return + endif + + let [grep_opts, query] = s:translate_query_and_opts(a:query) + + " Consistent with --smart-case of rg + " Searches case insensitively if the pattern is all lowercase. Search case sensitively otherwise. + let ignore_case = query =~# '\u' ? '\C' : '\c' + let s:hl_pattern = ignore_case.'^.*\d\+:\d\+:.*\zs'.query + + let cmd = printf(s:grep_cmd_format, s:grep_executable, grep_opts, query) let g:clap.provider.cmd = cmd return cmd endfunction @@ -86,12 +120,7 @@ function! s:spawn(query) abort call clap#rooter#run(function('clap#dispatcher#job_start'), s:cmd(query)) - " Consistent with --smart-case of rg - " Searches case insensitively if the pattern is all lowercase. Search case sensitively otherwise. - let ignore_case = query =~# '\u' ? '\C' : '\c' - let pattern = ignore_case.'^.*\d\+:\d\+:.*\zs'.query - - call g:clap.display.add_highlight(pattern) + call g:clap.display.add_highlight(s:hl_pattern) call clap#spinner#set_busy() endfunction diff --git a/pythonx/clap/__init__.py b/pythonx/clap/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pythonx/clap/fzy.py b/pythonx/clap/fzy.py index 011548f70..c19ce3491 100644 --- a/pythonx/clap/fzy.py +++ b/pythonx/clap/fzy.py @@ -3,7 +3,7 @@ import vim -from .fzy_impl import fzy_scorer +from clap.fzy_impl import fzy_scorer def fuzzy_match(query, candidates):