From a52b97431644b949ad5cc05cd50a690a535185e4 Mon Sep 17 00:00:00 2001 From: woodyZootopia Date: Thu, 14 May 2020 19:36:09 +0900 Subject: [PATCH] Add location list feature Inspired by #35 (https://github.com/jremmen/vim-ripgrep/pull/35) --- README.md | 20 +++++++++++--------- plugin/vim-ripgrep.vim | 29 +++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 1e605bd..8820610 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ```vim :Rg +:LRg "store to location list ``` Word under cursor will be searched if no argument is passed to `Rg` @@ -9,15 +10,16 @@ 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 diff --git a/plugin/vim-ripgrep.vim b/plugin/vim-ripgrep.vim index 4e29263..3d09e7e 100644 --- a/plugin/vim-ripgrep.vim +++ b/plugin/vim-ripgrep.vim @@ -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 @@ -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] @@ -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 @@ -161,4 +181,5 @@ fun! s:RgGetCwd() abort endfun command! -nargs=* -complete=file Rg :call s:Rg() +command! -nargs=* -complete=file LRg :call s:LRg() command! -complete=file RgRoot :call s:RgShowRoot()