Skip to content

Commit

Permalink
Add ability to pass path argument to override project root (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddrscott authored Nov 18, 2021
1 parent 9a655c9 commit ace1038
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,26 @@ cabbrev SS SideSearch

> How to search for multi-word terms?
Surround terms with double quotes
Use backslash `\` to escape spaces. Double quoted strings are no longer supported.

```
:SideSearch "cats and dogs"
:SideSearch cats\ and\ dogs
```

> How to pass extra args to `rg`?
> How to pass extra arguments to `rg`?
Just do it :)
Just do it™, but please add search terms first and extra arguments afterwards.
```
:SideSearch -t js MyAwesomeComponent
:SideSearch MyAwesomeComponent -t js
```

> How to restrict the search path?
Pass the search path as the last argument. If it is a valid relative or absolute path it is passed to the underlying
search program. Otherwise, SideSearch will guess the project's root directory.

```
:SideSearch MyAwesomeComponent -t js relative/path
```

> What happened to using The Silver Searcher?
Expand Down
28 changes: 15 additions & 13 deletions plugin/side-search.vim
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function! s:custom_mappings() abort
nnoremap <buffer> <silent> <CR> :call <SID>preview_main()<CR>
nnoremap <buffer> <silent> <2-LeftMouse> :call <SID>preview_main()<CR>
nnoremap <buffer> <silent> <C-w><CR> :call <SID>open_main()<CR>
nnoremap <buffer> <silent> qf :silent exec 'grep!' b:escaped_query<CR>
nnoremap <buffer> <silent> qf :silent exec 'grep!' b:escaped<CR>
endfunction

" Appends header guide to buffer
Expand Down Expand Up @@ -187,7 +187,7 @@ endfunction
" This will name the buffer the search term so it's easier to identify.
" After opening the search results, the cursor should remain in it's
" original position.
function! SideSearch(args) abort
function! SideSearch(term, ...) abort
call s:defaults()

let found = SideSearchWinnr()
Expand All @@ -202,23 +202,25 @@ function! SideSearch(args) abort

call s:append_guide()

" determine root directory
let l:cwd = s:guessProjectRoot()
" execute showing summary of stuff read (without silent)
let b:cmd = g:side_search_prg . ' ' . a:args . ' ' . l:cwd
" Thanks: https://github.com/rking/ag.vim/blob/master/autoload/ag.vim#L154
let query = matchstr(a:args, "\\v(-)\@<!(\<)\@<=\\w+|['\"]\\zs.{-}\\ze['\"]")
let b:escaped_query = shellescape(query)
let b:escaped = shellescape(a:term)
let b:cmd = g:side_search_prg . ' ' . b:escaped . ' ' . join(a:000, ' ')

" Guess the directory if value path is not provided as last argument
if len(a:000) == 0 || isdirectory(a:000[-1]) == 0
" guess the directory
let l:cwd = s:guessProjectRoot()
let b:cmd = b:cmd . l:cwd
endif

" echom 'a:term => ' . a:term
" echom 'b:cmd => '.b:cmd
silent execute 'read!' b:cmd

" name the buffer something useful
silent execute 'file [SS '.a:args.', '.s:parse_matches().']'
silent execute 'file [SS ' . b:escaped . ', ' . s:parse_matches() . ']'

" save search term in search register
" strip wrapped quotes as needed
let @/ = substitute(query, '\v([.\-])', "\\\\\\1", 'g')
let @/ = a:term

" 1. go to top of file
" 2. forward search the term
Expand All @@ -233,4 +235,4 @@ function! SideSearch(args) abort
endfunction

" Create a command to call SideSearch
command! -complete=file -nargs=+ SideSearch call SideSearch(<q-args>)
command! -complete=file -nargs=+ SideSearch call SideSearch(<f-args>)

0 comments on commit ace1038

Please sign in to comment.