Skip to content

enable specifying a root-relative subdirectory to be searched in #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

jakutis
Copy link
Contributor

@jakutis jakutis commented Nov 10, 2021

Motivation: missing :SideSearch ability to select a root-relative subdirectory to be searched in.

This PR:

  • satisfies the motivation by allowing to implement SideSearchRestrictedAndRelative command/function (see below) and then do :SideSearchRestrictedAndRelative some\ query some/root-relative/directory (or :SS some\ query some/root-relative/directory)
  • does not make breaking changes to the SideSearch command/function API

Then to use this possibility - local user config would contain:

function! SideSearchRestrictedAndRelative(query, ...) abort
  let l:subdir = get(a:, 1, '')
  let l:subdir = (l:subdir == '' ? '' : '/') . l:subdir
  call SideSearch(shellescape(a:query, 1), l:subdir)
endfunction
command! -complete=file -nargs=+ SideSearchRestrictedAndRelative call SideSearchRestrictedAndRelative(<f-args>)

Having these (possibly with better naming) as part of side-search.vim, not user config, may also be a good idea. If you agree, then there would be less additions to README.

In my config I also have the command abbreviation setup: cabbrev SS SideSearchRestrictedAndRelative

@jakutis jakutis marked this pull request as ready for review November 10, 2021 15:10
@ddrscott
Copy link
Owner

ddrscott commented Nov 10, 2021

Thanks for the suggestion!

I'd like to understand the use case a little bit. I think you're trying to search the root file system instead of from the current project subdirectory. That seems out of scope for how vim would be expected to work. Usually, we start vim from a base path and go from there.

The current SideSearch command tries to pass arguments directly to the terminal command. For example, if you want to find the word useful in /etc we can already do :SS useful /etc. If we want to search subdirectory of the current project then :SS useful etc would work.

I don't think it is necessary to have code in place that concatenates / for us when the underlying command already does the right thing.

Please correct me if I'm misunderstanding anything.

@ddrscott
Copy link
Owner

Also, I just realized that the current behavior is unintended. By accident, the Vim args take precedent over the l:cwd in https://github.com/ddrscott/vim-side-search/pull/14/files#diff-553aea6cf1ce113ff84501ff936916c9d52baf7d65873ce423993f45aac7e781L208

Anyway, without any further changes to the plugin source, I think the previous comment still stands. We can override the search path simply by adding it after the search term via :SS term [path]

@jakutis
Copy link
Contributor Author

jakutis commented Nov 11, 2021

With :SideSearch query /some/directory the arg /some/diretory does not take precedent over the l:cwd, because vim-side-search would pass two paths to search in, and rg accepts both of the paths - try running something similar as this: rg grub /etc/ /boot/

The use case exactly the opposite. With :SideSearchRestrictedAndRelative query path command we actually want to start from a base path and go from there - we do not want to search the root file system, but to search only the specified subdirectory in the project (s:guessProjectRoot() . '/path'). If path is not specified (:SideSearchRestrictedAndRelative query) then act just like :SideSearch, except :SideSearchRestrictedAndRelative cannot take multiple arguments and pass all of them to rg.

So because /some/directory does not take precedent over the l:cwd, but adds to l:cwd, without this PR there is no way to make SideSearch function to search only in a subdirectory of the project root.

@ddrscott
Copy link
Owner

Okay, I think I understand. The new function allows the user to specify a subdirectory instead of using the guessed project root.
It still feels strange to require a 2nd function to add a subdirectory when rg allows a path to be specified naturally.
I propose we modify the normal SideSearch function to detect when the last argument is a path and skip the let l:cwd = s:guessProjectRoot() step when it is. That way :SideSearch [args] will behave exactly as rg [args], which is what I was intending in the first place.

So the logical steps would be:

if args.length > 1 and last arg looks like a path
     execute `rg` + args
else
     execute `rg` + args + guessProjectRoot
end

(Sorry for the pseudo-code, I don't have Vim syntax in my brain at the moment)

@jakutis What do you feel about this solution? Want to take a shot at it? Otherwise, I will later this weekend.

@jakutis
Copy link
Contributor Author

jakutis commented Nov 13, 2021

Go ahead with it! I think this is a good solution - it is simple and enables my use case and possibly other.

It works for me when "What looks like a path" would be defined as "if the string is a path to an existing directory" - any path - relative or absolute

@ddrscott
Copy link
Owner

Thanks for the PR. After the discussions, we now have this PR instead: #16

@ddrscott ddrscott closed this Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants