Skip to content

Commit

Permalink
feat(auto-complete): :FzfLua completes options
Browse files Browse the repository at this point in the history
It is possible to run nested options like so:
```
:FzfLua files winopts.fullscreen=true
```

- Requires nvim >= 0.7, below uses the old code
- Renamed `help_tags|man_pages` to `helptags|manpages`
  (backward compat, can still call `:FzfLua help_tags`
   but it will not show in auto-complete)
  • Loading branch information
ibhagwan committed Apr 4, 2024
1 parent be720e0 commit 878eba3
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ Alternatively, resuming work on a specific provider:
| `resume` | resume last command/query |
| `builtin` | fzf-lua builtin commands |
| `profiles` | fzf-lua configuration profiles |
| `help_tags` | help tags |
| `man_pages` | man pages |
| `helptags` | help tags |
| `manpages` | man pages |
| `colorschemes` | color schemes |
| `awesome_colorschemes` | Awesome Neovim color schemes |
| `highlights` | highlight groups |
Expand Down
8 changes: 2 additions & 6 deletions lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,7 @@ M.fzf = function(contents, opts)
-- fzf 0.40 added 'zero' event for when there's no match
-- clears the preview when there are no matching entries
if opts.__FZF_VERSION and opts.__FZF_VERSION >= 0.40 and previewer.zero then
opts.keymap = opts.keymap or {}
opts.keymap.fzf = opts.keymap.fzf or {}
opts.keymap.fzf["zero"] = previewer:zero()
utils.map_set(opts, "keymap.fzf.zero", previewer:zero())
end
if opts.__FZF_VERSION
and opts.__FZF_VERSION >= 0.46
Expand All @@ -361,9 +359,7 @@ M.fzf = function(contents, opts)
tonumber(opts.winopts.preview.flip_columns),
opts.winopts.preview.vertical,
opts.winopts.preview.horizontal)
opts.keymap = opts.keymap or {}
opts.keymap.fzf = opts.keymap.fzf or {}
opts.keymap.fzf["resize"] = string.format("transform(%s)", transformer)
utils.map_set(opts, "keymap.fzf.resize", string.format("transform(%s)", transformer))
end
if type(previewer.preview_window) == "function" then
-- do we need to override the preview_window args?
Expand Down
8 changes: 8 additions & 0 deletions lua/fzf-lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ do
tabs = { "fzf-lua.providers.buffers", "tabs" },
lines = { "fzf-lua.providers.buffers", "lines" },
blines = { "fzf-lua.providers.buffers", "blines" },
helptags = { "fzf-lua.providers.helptags", "helptags" },
manpages = { "fzf-lua.providers.manpages", "manpages" },
-- backward compat
help_tags = { "fzf-lua.providers.helptags", "helptags" },
man_pages = { "fzf-lua.providers.manpages", "manpages" },
colorschemes = { "fzf-lua.providers.colorschemes", "colorschemes" },
Expand Down Expand Up @@ -349,6 +352,11 @@ M._excluded_meta = {
"get_info",
"set_info",
"get_last_query",
-- Exclude due to rename:
-- help_tags -> helptags
-- man_pages -> manpages
"help_tags",
"man_pages",
}

for _, m in ipairs(M._exported_modules) do
Expand Down
82 changes: 82 additions & 0 deletions plugin/fzf-lua.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
if vim.g.loaded_fzf_lua == 1 then
return
end
vim.g.loaded_fzf_lua = 1

-- Should never be called, below nvim 0.7 "plugin/fzf-lua.vim"
-- sets `vim.g.loaded_fzf_lua=1`
if vim.fn.has("nvim-0.7") ~= 1 then
vim.api.nvim_err_writeln("Fzf-lua minimum requirement is Neovim versions 0.5")
return
end

vim.api.nvim_create_user_command("FzfLua", function(opts)
require("fzf-lua.cmd").load_command(unpack(opts.fargs))
end, {
nargs = "*",
complete = function(_, line)
local metatable = require("fzf-lua")
local builtin_list = vim.tbl_filter(function(k)
return metatable._excluded_metamap[k] == nil
end, vim.tbl_keys(metatable))

local l = vim.split(line, "%s+")
local n = #l - 2

if n == 0 then
local commands = vim.tbl_flatten({ builtin_list })
table.sort(commands)

return vim.tbl_filter(function(val)
return vim.startswith(val, l[2])
end, commands)
end

local defaults = require("fzf-lua.defaults").defaults
local cmd_opts = defaults[l[2]] or {}
local opts = vim.tbl_filter(function(k)
return not k:match("^_")
end, vim.tbl_keys(cmd_opts))

-- Flatten map's keys recursively
-- { a = { a1 = ..., a2 = ... } }
-- will be transformed to:
-- {
-- ["a.a1"] = ...,
-- ["a.a2"] = ...,
-- }
local function map_flatten(t, prefix)
if vim.tbl_isempty(t) then return {} end
local ret = {}
prefix = prefix and string.format("%s.", prefix) or ""
for k, v in pairs(t) do
if type(v) == "table" then
local inner = map_flatten(v)
for ki, vi in pairs(inner) do
ret[prefix .. k .. "." .. ki] = vi
end
else
ret[prefix .. k] = v
end
end
return ret
end

-- Add globals recursively, e.g. `winopts.fullscreen`
-- will be later retrieved using `utils.map_get(...)`
for _, k in ipairs({ "winopts", "keymap", "fzf_opts", "fzf_tmux_opts", "hls" }) do
opts = vim.tbl_flatten({ opts, vim.tbl_keys(map_flatten(defaults[k] or {}, k)) })
end

-- Add generic options that apply to all pickers
for _, o in ipairs({ "query" }) do
table.insert(opts, o)
end

table.sort(opts)

return vim.tbl_filter(function(val)
return vim.startswith(val, l[#l])
end, opts)
end,
})
5 changes: 5 additions & 0 deletions plugin/fzf-lua.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
" Neovim >= v0.7 uses 'plugin/fzf-lua.lua'
if has('nvim-0.7')
finish
end

if !has('nvim-0.5')
echohl Error
echomsg "Fzf-lua is only available for Neovim versions 0.5 and above"
Expand Down

0 comments on commit 878eba3

Please sign in to comment.