-
Notifications
You must be signed in to change notification settings - Fork 151
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
live_grep_glob feature request #373
Comments
Modified to support
|
Hi @mikesart, I see no harm in this enhancement as there aren’t many useful globs (if any) with a dash followed by a letter so it’s somewhat of a “free upgrade”. That said, I’m not sure how many people are aware of all If this were to be added to the code I’d prefer it being more consistent in behavior, not sure how much I like a single hard coded exception for I’m thinking maybe it’s best to just parse the string for an additional glob separator (use the same separator or different one?) and pass everything after it “as is” to the command? I.e. you can type |
Could also make it so this function is overridable by the user if you’d prefer that? Thanks! |
Already thought about that but it’s not as straight forward as you’d think, for performance reasons most commands are run externally (with There are a couple of solutions for that but they aren’t pretty, the first would be to compile the user function to bytes, write it in a temp file and load it by the external instance which adds complexity and many other potential issues, the below is from
The other option would be to perform the parsing in the main instance by executing the function over an RPC request, this would break the model of the external processing instance being independent of the main instance. What I can try to do is query the user config in the pre-processing and pass the function override to the external instance as bytecode using |
On this page: https://github.com/nanotee/nvim-lua-guide There is a way to load a module and not error out if it doesn't exist. Ie:
Would something like this work where you use an override module if it exists and use yours if not? You are way ahead of me on everything lua and neovim, so I'm for sure asking these questions for my own education. I very much appreciate your continued patience. :) Also, I'm more than happy to just fork fzf-lua and keep it up to date with some minor tweaks I want that don't make sense for other folks. That's super easy as well. Thanks ibhagwan! |
This is unrelated and won't work as these modules don't exist in the context of the neovim headless instance (it's a different process altogether), that's why I have the below "hacks" in Lines 5 to 19 in 97a63e6
The issue here is that your custom config module doesn't exist in this context and would require
No need for that, I personally would hate that (having to keep updaging my own fork). f3d0789 - this commit adds the ability to include custom function in your setup under The only limiation is that you can't call upvalues (no require'fzf-lua'.setup {
grep = {
rg_glob = true,
rg_glob_fn = function(opts, query)
local glob_args = nil
local search_query, glob_str = query:match("(.*)"..opts.glob_separator.."(.*)")
for _, s in ipairs(vim.split(glob_str, " ")) do
s = vim.trim(s)
if #s>0 then
glob_args = glob_args or ""
if string.match(s, '^-%a$') then
glob_args = glob_args .. s .. ' '
else
glob_args = glob_args .. ("%s %s ")
:format(opts.glob_flag, vim.fn.shellescape(s))
end
end
end
-- UNCOMMENT TO DEBUG PRINT INTO FZF
-- if glob_args then
-- io.write(("q: %s -> flags: %s, query: %s\n"):format(
-- query, glob_args, search_query))
-- end
return search_query, glob_args
end,
}
} |
Interesting. Thanks for the description. Also, your patch works great. I've got a rg_glob_fn in my init.lua and it's perfect. Thank you very, very much ibhagwan! |
@mikesart quick note, turns out that when I implemented this I changed the way glob parsing worked by a tiny bit, 58320a2 - latest commit restores the functionality where the glob separator is removed from the search query immedietly after typing the separator. I recommend you do this for your custom function too, all you need to do is change I found this out during a test I ran for #381 which you also might find interesting as you might have some uses for the |
Ah, very cool - I updated my filter. And thanks for the pointer to 381, learned some things from reading that. Thanks for the pointer and have a great weekend ibhagwan! |
Hi @mikesart, quick FYI, I'm doing some major refactoring in order to make the plugin much easier to extend, as part of that I'm changing some of the API's, you can read about it more here (still WIP): https://github.com/ibhagwan/fzf-lua/wiki/Advanced One important thing for you is that the signature of rg_glob_fn = function(opts, query) to: rg_glob_fn = function(query, opts) Keep an eye out for the next commit (still testing and making more changes in the develop branch), once that's commited just change the order of of the call args and place |
Oh, awesome. Thank you very much for the heads up - much appreciated! |
FYI, change was just pushed. Also, if you're intestsed in the new API and making your own commands (rather easily I might say), read this: |
This is great to hear. I've got the below right now and will look at moving it over to your new api when I get some free time. Thanks very much, really appreciate all your work on this super useful plugin! local shortcut_contents = {
{ text = "<leader>e" , data = "(netrw) Open :Lexplore" },
{ text = "<leader>l" , data = "set invlist" },
{ text = "<leader>c" , data = "Clear highlighting" },
{ text = "<leader>t" , data = "TabToggle" },
{ text = "<leader>rv" , data = "(vimrc) Reload .vimrc" },
{ text = ":%!xxd" , data = "(edit) Show buffer in hex" },
{ text = ":%!xxd -r" , data = "(edit) Convert from hex back to text" },
{ text = ":%!xxd -i" , data = "(edit) Convert to char array for C files" },
-- ...
}
local utils = require "fzf-lua.utils"
-- Previewer class inherits from base previewer
-- the only required method is 'populate_preview_buf'
local Previewer = {}
Previewer.base = require('fzf-lua.previewer.builtin').base
-- inheriting from 'buffer_or_file' gives us access to filetype
-- detection and syntax highlighting helpers for file based previews
-- Previewer.buffer_or_file = require('fzf-lua.previewer.builtin').buffer_or_file
function Previewer:new(o, opts, fzf_win)
self = setmetatable(Previewer.base(o, opts, fzf_win), {
__index = vim.tbl_deep_extend("keep",
self, Previewer.base
-- only if you need access to specific file methods
-- self, Previewer.buffer_or_file, Previewer.base
)})
return self
end
local function SplitStr(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
function Previewer:populate_preview_buf(entry_str)
local lines = { SplitStr(entry_str, " ")[3] }
-- vim.api.nvim_buf_set_option(self.preview_bufnr, 'modifiable', true)
vim.api.nvim_buf_set_lines(self.preview_bufnr, 0, -1, false, lines)
-- mark the buffer for unloading on next preview call
self.preview_bufloaded = true
-- enable syntax highlighting
local filetype = 'cpp'
vim.api.nvim_buf_set_option(self.preview_bufnr, 'filetype', filetype)
end
local my_ex_run = function(selected)
vim.cmd("stopinsert")
local cmd = SplitStr(selected[1], " ")[1]
local keys = vim.api.nvim_replace_termcodes(cmd, true, false, true)
vim.api.nvim_feedkeys(keys, "t", true)
return cmd
end
function Shortcuts()
-- this function feeds elements into fzf
-- each call to `fzf_cb()` equals one line
-- `fzf_cb(nil)` closes the pipe and marks EOL to fzf
local fn = function(fzf_cb)
local i = 1
for _, e in ipairs(shortcut_contents) do
local pad = string.rep(' ', 20 - #e.text)
fzf_cb(("%s %s %s"):format(utils.ansi_codes.yellow(e.text), pad, e.data))
i = i + 1
end
fzf_cb(nil)
end
local actions = {
["default"] = my_ex_run,
["ctrl-e"] = my_ex_run,
}
coroutine.wrap(function()
local selected = require('fzf-lua').fzf({
prompt = 'Shortcuts❯ ',
previewer = Previewer,
actions = actions,
fzf_opts = {
["--delimiter"] = '.',
-- ["--header"] = arg_header("<CR>", "<Ctrl-e>", "execute"),
},
}, fn)
require('fzf-lua').actions.act(actions, selected, {})
end)()
end |
Ty @mikesart, let me know if you need help, btw I added lua type to your code snippet so it displays lua highlights. |
I added the below check for some ripgrep search flags to glob_parse and pass those through.
This allows me to do a live_grep_glob and add -w, -i, etc, etc on the fly to ripgrep, which is pretty useful. (I'm currently using vim-agriculture for this, but I like FzfLua better. :)
There might be a better way to do this, but for quick and dirty the below is working pretty well for me right now.
No pressure taking this or not, of course. Thanks!
The text was updated successfully, but these errors were encountered: