Skip to content
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

Grep with commandline like arguments and options #1186

Closed
1 task done
Grueslayer opened this issue May 10, 2024 · 8 comments
Closed
1 task done

Grep with commandline like arguments and options #1186

Grueslayer opened this issue May 10, 2024 · 8 comments
Labels
feature request New feature

Comments

@Grueslayer
Copy link

Grueslayer commented May 10, 2024

Have you RTFM'd?

  • I have done proper research

Feature Request

Would be fine to have the same features like jesseleite/vim-agriculture in fzf-lua.

Or maybe you can give an example how to refactor

return call('fzf#vim#grep', extend([command, 1], [s:preview(bang), bang]))

while using the build in preview mechanism of fzf-lua, so we can make a fork.

@Grueslayer Grueslayer added the feature request New feature label May 10, 2024
@ibhagwan
Copy link
Owner

ibhagwan commented May 10, 2024

You can already do that with:

require('fzf-lua').grep({ cmd = '<any command>' })

You can also dynamically add arguments as part of live grep with rg_glob_fn, see #373 (comment)

@ibhagwan
Copy link
Owner

@Grueslayer
Copy link
Author

@ibhagwan Hi, Live Grep is something what never correctly worked on my system.... Agricultures tries to let you define all search parameters in the VIM commandline as you would write this in the shell.

E.g.

:RgRaw -i "TaBlE 5" --iglob "*g.table.al"

will be translated to

rg --column --line-number --no-heading --color=always -i "TaBlE 5" --iglob "*g.table.al"

If I use that as "cmd" for grep it still prompts for search input.....

I can give it to fzf_exec as first parameter but it does not have the preview and no actions are defined.
So it would nice to have a "grep" where the cmd will be taken as is (like in fzf_exec), no input request, preview available and default actions still available.

@ibhagwan
Copy link
Owner

Hi, Live Grep is something what never correctly worked on my system

Not sure if I understand this, if something doesn't work as expected open a bug and I'll fix it.

Agricultures tries to let you define all search parameters in the VIM commandline as you would write this in the shell
So it would nice to have a "grep" where the cmd will be taken as is (like in fzf_exec), no input request, preview available and default actions still available.

That's very easy to accomplish with a simple user command calling grep (so you get to keep previews, actions, etc):

vim.api.nvim_create_user_command("RgRaw", function(o)
  require("fzf-lua").grep({
    debug = true, -- to display the generated cmd on 1st line
    -- Using `raw_cmd` avoids the `Grep For> ` input and passes the command as is to fzf
    raw_cmd = "rg --column --line-number --no-heading --color=always " .. o.args,
  })
end, { nargs = "*" })

Now if I run:

:RgRaw --glob "*.lua" -e "pcall"

I get the below (notice the first DEBUG line with the generated command):
image

@Grueslayer
Copy link
Author

@ibhagwan What a nice small hack, thanx!

I've added fzf-lua support as PR to jesseleite/vim-agriculture which can be used from my branch in the meantime:

  {
    'grueslayer/vim-agriculture',
    branch = 'fzf-lua-support',
    config = function()
      vim.keymap.set('n','<leader>/',"<Plug>RgRawSearch",{})
      vim.keymap.set('v','<leader>/',"<Plug>RgRawVisualSelection",{})
      vim.keymap.set('n','<leader>*',"<Plug>RgRawWordUnderCursor",{})
    end
  }

This plugin has some small additional little things, which can be fast moved to lua I think but was to lazy for it.

@Grueslayer
Copy link
Author

Hi, Live Grep is something what never correctly worked on my system

Not sure if I understand this, if something doesn't work as expected open a bug and I'll fix it.

Maybe my expectation is wrong, vim/nvim handles asynchronous processes wrong or MS Windows process handling is the problem. Let's say if I write 'tableextension' in the live grep in a big and deep directory struture, it starts finding 'tabl' and displays many matches in the list. I think I've to wait until it has finished it's search before it begins to display any 'tableextension' matches and removes the wrong ones. So until it starts while I am not finshed to type or not immediately restarts search after altering the input (and removes wrong / outdated matches), it's not my solution.

@ibhagwan
Copy link
Owner

ibhagwan commented May 12, 2024

I've added fzf-lua support as PR to jesseleite/vim-agriculture#21 which can be used from my branch in the meantime

Very nice, lua is indeed one of the simplest of languages.

Maybe my expectation is wrong, vim/nvim handles asynchronous processes wrong or MS Windows process handling is the problem. Let's say if I write 'tableextension' in the live grep in a big and deep directory struture, it starts finding 'tabl' and displays many matches in the list. I think I've to wait until it has finished it's search before it begins to display any 'tableextension' matches and removes the wrong ones. So until it starts while I am not finshed to type or not immediately restarts search after altering the input (and removes wrong / outdated matches), it's not my solution.

I see what you mean, when using live_grep you’re indeed restarting the query with each keystroke as it generates a new underlying command, for that reason exactly I’ve added the default ctrl-g bind that switches back and forth from live grep to grep, the recommended live grep process is to cast a search with a wider than expected regex, say in your case table and then press ctrl-g and continue the search, at this point you’ve switched from live grep to a fixed command grep and you can start fuzzy matching before the rg command finished (similar to you running :RgRaw … “table”).

Basically, live_grep is a UI for :RgRaw to experiment with different regexes (I.e. table.*=, to search for all table assignments) and when you’re ready to reduce the set you press ctrl-g (which would bring your to the same place as pressing Enter on a :RgRaw command.

@ibhagwan
Copy link
Owner

This plugin has some small additional little things, which can be fast moved to lua I think but was to lazy for it.

btw, if you’re referring to the visual selection you can reuse fzf-lua’s utils.get_visual_selection as used by grep_visual:

M.grep_visual = function(opts)
if not opts then opts = {} end
opts.search = utils.get_visual_selection()
return M.grep(opts)
end

Similarly, grep word under cursor in grep_cword:

M.grep_cword = function(opts)
if not opts then opts = {} end
opts.no_esc = true
-- match whole words only (#968)
opts.search = [[\b]] .. utils.rg_escape(vim.fn.expand("<cword>")) .. [[\b]]
return M.grep(opts)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature
Projects
None yet
Development

No branches or pull requests

2 participants