Skip to content

Commit

Permalink
Add limited search syntax for grep provider (#150)
Browse files Browse the repository at this point in the history
* Add specified filetype searching for grep

* .

* Fix highlight pattern when using specificed filetype search

* Extract s:translate_query_and_opts()

* Support .filetype

* Exact search and subsearches

* Update CHANGELOG.md

* .
  • Loading branch information
liuchengxu authored Dec 9, 2019
1 parent 4874356 commit 0a05388
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
.*
__pycache__
*.pyc
*.log
# Ignore all the vim files on the top directory
/*.vim
/*.rc
[1-9]*.vim
/doc/tags
/target

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 2 additions & 7 deletions autoload/clap/filter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
61 changes: 45 additions & 16 deletions autoload/clap/provider/grep.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Empty file added pythonx/clap/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion pythonx/clap/fzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import vim

from .fzy_impl import fzy_scorer
from clap.fzy_impl import fzy_scorer


def fuzzy_match(query, candidates):
Expand Down

0 comments on commit 0a05388

Please # to comment.