Skip to content

Commit

Permalink
Add location list feature
Browse files Browse the repository at this point in the history
Inspired by jremmen#35 (jremmen#35)
  • Loading branch information
swnakamura committed May 14, 2020
1 parent 1ee3f0b commit a52b974
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

```vim
:Rg <string|pattern>
:LRg <string|pattern> "store to location list
```

Word under cursor will be searched if no argument is passed to `Rg`

## configuration


| Setting | Default | Details
| --------------------- | --------------------------- | ----------
| `g:rg_binary` | `rg` | path to rg
| `g:rg_format` | `%f:%l:%c:%m` | value of grepformat
| `g:rg_option` | `--vimgrep` | search command option
| `g:rg_highlight` | `false` | true if you want matches highlighted
| `g:rg_derive_root` | `false` | true if you want to find project root from cwd
| `g:rg_root_types` | `['.git']` | list of files/dir found in project root
| `g:rg_window_location` | `botright` | quickfix window location
| Setting | Default | Details
| --------------------- | --------------------------- | ----------
| `g:rg_binary` | `rg` | path to rg
| `g:rg_format` | `%f:%l:%c:%m` | value of grepformat
| `g:rg_option` | `--vimgrep` | search command option
| `g:rg_highlight` | `0` | true if you want matches highlighted
| `g:rg_derive_root` | `0` | true if you want to find project root from cwd
| `g:rg_root_types` | `['.git']` | list of files/dir found in project root
| `g:rg_use_location_list` | `0` | if `1`, use location list instead of quickfix list
| `g:rg_window_location` | `botright` | quickfix window location

## misc

Expand Down
29 changes: 25 additions & 4 deletions plugin/vim-ripgrep.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ if !exists('g:rg_window_location')
let g:rg_window_location = 'botright'
endif

if !exists('g:rg_use_location_list')
let g:rg_use_location_list = 0
endif

fun! g:RgVisual() range
call s:RgGrepContext(function('s:RgSearch'), '"' . s:RgGetVisualSelection() . '"')
endfun
Expand All @@ -36,6 +40,13 @@ fun! s:Rg(txt)
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
endfun

fun! s:LRg(txt)
let l:rg_use_location_list_bak = g:rg_use_location_list
let g:rg_use_location_list = 1
call s:RgGrepContext(function('s:RgSearch'), s:RgSearchTerm(a:txt))
let g:rg_use_location_list = l:rg_use_location_list_bak
endfun

fun! s:RgGetVisualSelection()
" Why is this not a built-in Vim script function?!
let [line_start, column_start] = getpos("'<")[1:2]
Expand Down Expand Up @@ -65,15 +76,24 @@ fun! s:RgSearch(txt)
if &smartcase == 1
let l:rgopts = l:rgopts . '-S '
endif
silent! exe 'grep! ' . l:rgopts . a:txt
if len(getqflist())
exe g:rg_window_location 'copen'
if (g:rg_use_location_list==1)
let l:rg_grep_cmd = 'lgrep! '
let l:rg_window_cmd = 'lopen'
let l:rg_window_close_cmd = 'lclose'
else
let l:rg_grep_cmd = 'grep! '
let l:rg_window_cmd = 'copen'
let l:rg_window_close_cmd = 'cclose'
endif
silent! exe l:rg_grep_cmd . l:rgopts . a:txt
if (g:rg_use_location_list ? len(getloclist(0)) : len(getqflist()))
exe g:rg_window_location . ' ' . l:rg_window_cmd
redraw!
if exists('g:rg_highlight')
call s:RgHighlight(a:txt)
endif
else
cclose
exe l:rg_window_close_cmd
redraw!
echo "No match found for " . a:txt
endif
Expand Down Expand Up @@ -161,4 +181,5 @@ fun! s:RgGetCwd() abort
endfun

command! -nargs=* -complete=file Rg :call s:Rg(<q-args>)
command! -nargs=* -complete=file LRg :call s:LRg(<q-args>)
command! -complete=file RgRoot :call s:RgShowRoot()

0 comments on commit a52b974

Please sign in to comment.