From faad0474d4af38856201fc58dcf0594c2fa11422 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 14 Aug 2021 14:39:19 -0700 Subject: [PATCH] added neovim builtin providers commands, command_history, search_history, keymaps, registers, spell_suggest --- README.md | 61 ++++--- lua/fzf-lua/actions.lua | 24 +++ lua/fzf-lua/config.lua | 37 ++++- lua/fzf-lua/init.lua | 7 + lua/fzf-lua/providers/colorschemes.lua | 1 - lua/fzf-lua/providers/files.lua | 2 - lua/fzf-lua/providers/git.lua | 1 - lua/fzf-lua/providers/grep.lua | 1 - lua/fzf-lua/providers/helptags.lua | 1 - lua/fzf-lua/providers/lsp.lua | 3 - lua/fzf-lua/providers/manpages.lua | 1 - lua/fzf-lua/providers/module.lua | 1 - lua/fzf-lua/providers/nvim.lua | 220 +++++++++++++++++++++++++ lua/fzf-lua/providers/oldfiles.lua | 3 - lua/fzf-lua/providers/quickfix.lua | 2 - lua/fzf-lua/utils.lua | 2 +- 16 files changed, 324 insertions(+), 43 deletions(-) create mode 100644 lua/fzf-lua/providers/nvim.lua diff --git a/README.md b/README.md index 17362f4..be70e4d 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,7 @@ nnoremap lua require('fzf-lua').files() ## Commands +### Buffers & Files | Command | List | | --- | --- | |`buffers`|open buffers| @@ -123,6 +124,10 @@ nnoremap lua require('fzf-lua').files() |`oldfiles`|opened files history| |`quickfix`|quickfix list| |`loclist`|location list| + +### Search +| Command | List | +| --- | --- | |`grep`|search for a pattern with `grep` or `rg`| |`grep_last`|run search again with the last pattern| |`grep_cword`|search word under cursor| @@ -130,18 +135,18 @@ nnoremap lua require('fzf-lua').files() |`grep_visual`|search visual selection| |`grep_curbuf`|live grep current buffer| |`live_grep`|live grep current project| -|`help_tags`|help tags| -|`man_pages`|man pages| -|`colorschemes`|color schemes| -|`builtin`|fzf-lua builtin methods| + + +### Git +| Command | List | +| --- | --- | |`git_files`|`git ls-files`| |`git_status`|`git status`| |`git_commits`|git commit log (project)| |`git_bcommits`|git commit log (buffer)| |`git_branch`|git branches| -## LSP Commands - +### LSP | Command | List | | --- | --- | |`lsp_references`|References| @@ -155,6 +160,21 @@ nnoremap lua require('fzf-lua').files() |`lsp_document_diagnostics`|Document Diagnostics| |`lsp_workspace_diagnostics`|Workspace Diagnostics| +### Misc +| Command | List | +| --- | --- | +|`builtin`|fzf-lua builtin methods| +|`help_tags`|help tags| +|`man_pages`|man pages| +|`colorschemes`|color schemes| +|`commands`|neovim commands| +|`command_history`|command history| +|`search_history`|search history| +|`registers`|:registers| +|`keymaps`|key mappings| +|`spell_suggest`|spelling suggestions| + + ## Customization I tried to make it as customizable as possible, if you find you need to change something that isn’t below, open an issue and I’ll do my best to add it. @@ -397,31 +417,24 @@ require('fzf-lua').setup{ EOF ``` -### Known issues - -- [ ] `live_grep` has icons disabled until I find a solution for fzf's - `change:reload` event -- [ ] Tested mostly with both `rg`, `fd` and `bat` installed, there might be - issues with the default `grep`, `find` and `head` alternatives - ## TODO -- Add more providers +- [ ] Add more providers + [x] ~~LSP (refs, symbols, etc)~~ (2021-07-20) + [x] ~~git commits~~ (2021-08-05) + [x] ~~git branches~~ (2021-08-05) - + [ ] vim commands - + [ ] vim command history - + [ ] vim keymaps - + [ ] vim options - + [ ] search history - + [ ] tags + + [x] nvim builtin: + * [x] ~~commands~~ (2021-08-14) + * [x] ~~command history~~ (2021-08-14) + * [x] ~~search history~~ (2021-08-14) + * [x] ~~registers~~ (2021-08-14) + * [x] ~~keymaps~~ (2021-08-14) + * [x] ~~spelling suggestions~~ (2021-08-14) + [ ] marks - + [ ] registers - + [ ] spelling suggestions + + [ ] tags +- [ ] Built-in previewer with treesitter support - [ ] Add built-in plugin documentation -- [ ] Add "hidden" options documentation -- [ ] Add FAQ +- [ ] Complete the Wiki ### Credits diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index 0dbc4d6..9df7dd6 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -132,6 +132,30 @@ M.run_builtin = function(selected) vim.cmd(string.format("lua require'fzf-lua'.%s()", method)) end +M.ex_run = function(selected) + if not selected then return end + local cmd = selected[1] + if #selected>1 then cmd = selected[2] end + vim.cmd("stopinsert") + vim.fn.feedkeys(string.format(":%s ", cmd)) +end + +M.search = function(selected) + if not selected then return end + local query = selected[1] + if #selected>1 then query = selected[2] end + vim.cmd("stopinsert") + utils.feed_keys_termcodes(string.format("/%s", query)) +end + +M.spell_apply = function(selected) + if not selected then return end + local word = selected[1] + if #selected>1 then word = selected[2] end + vim.cmd("normal! ciw" .. word) + vim.cmd("stopinsert") +end + M.help = function(selected) local vimcmd = "help" M.vimcmd(vimcmd, selected) diff --git a/lua/fzf-lua/config.lua b/lua/fzf-lua/config.lua index 5b0780c..8e1cdf3 100644 --- a/lua/fzf-lua/config.lua +++ b/lua/fzf-lua/config.lua @@ -308,13 +308,46 @@ M.globals.lsp = { M.globals.builtin = { prompt = 'Builtin> ', winopts = { - win_height = 0.65, - win_width = 0.50, + win_height = 0.65, + win_width = 0.50, }, actions = { ["default"] = actions.run_builtin, }, } +M.globals.nvim = { + commands = { + prompt = 'Commands> ', + actions = { + ["default"] = actions.ex_run, + }, + }, + command_history = { + prompt = 'Command History> ', + actions = { + ["default"] = actions.ex_run, + }, + }, + search_history = { + prompt = 'Search History> ', + actions = { + ["default"] = actions.search, + }, + }, + registers = { + prompt = 'Registers> ', + ignore_empty = true, + }, + keymaps = { + prompt = 'Keymaps> ', + }, + spell_suggest = { + prompt = 'Spelling Suggestions> ', + actions = { + ["default"] = actions.spell_apply, + }, + }, + } M.globals.file_icon_colors = { ["lua"] = "blue", ["vim"] = "green", diff --git a/lua/fzf-lua/init.lua b/lua/fzf-lua/init.lua index 5b30c69..d09be73 100644 --- a/lua/fzf-lua/init.lua +++ b/lua/fzf-lua/init.lua @@ -68,6 +68,13 @@ M.help_tags = require'fzf-lua.providers.helptags'.helptags M.man_pages = require'fzf-lua.providers.manpages'.manpages M.colorschemes = require'fzf-lua.providers.colorschemes'.colorschemes +M.keymaps = require'fzf-lua.providers.nvim'.keymaps +M.registers = require'fzf-lua.providers.nvim'.registers +M.commands = require'fzf-lua.providers.nvim'.commands +M.command_history = require'fzf-lua.providers.nvim'.command_history +M.search_history = require'fzf-lua.providers.nvim'.search_history +M.spell_suggest = require'fzf-lua.providers.nvim'.spell_suggest + M.lsp_typedefs = require'fzf-lua.providers.lsp'.typedefs M.lsp_references = require'fzf-lua.providers.lsp'.references M.lsp_definitions = require'fzf-lua.providers.lsp'.definitions diff --git a/lua/fzf-lua/providers/colorschemes.lua b/lua/fzf-lua/providers/colorschemes.lua index afd54cd..faba4ee 100644 --- a/lua/fzf-lua/providers/colorschemes.lua +++ b/lua/fzf-lua/providers/colorschemes.lua @@ -2,7 +2,6 @@ if not pcall(require, "fzf") then return end -local fzf = require "fzf" local action = require("fzf.actions").action local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" diff --git a/lua/fzf-lua/providers/files.lua b/lua/fzf-lua/providers/files.lua index e767fff..bbe7bf5 100644 --- a/lua/fzf-lua/providers/files.lua +++ b/lua/fzf-lua/providers/files.lua @@ -2,9 +2,7 @@ if not pcall(require, "fzf") then return end --- local fzf = require "fzf" local fzf_helpers = require("fzf.helpers") --- local path = require "fzf-lua.path" local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" local config = require "fzf-lua.config" diff --git a/lua/fzf-lua/providers/git.lua b/lua/fzf-lua/providers/git.lua index 648bdb2..6646482 100644 --- a/lua/fzf-lua/providers/git.lua +++ b/lua/fzf-lua/providers/git.lua @@ -2,7 +2,6 @@ if not pcall(require, "fzf") then return end -local fzf = require "fzf" local fzf_helpers = require("fzf.helpers") local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" diff --git a/lua/fzf-lua/providers/grep.lua b/lua/fzf-lua/providers/grep.lua index 902564d..d0e23b3 100644 --- a/lua/fzf-lua/providers/grep.lua +++ b/lua/fzf-lua/providers/grep.lua @@ -2,7 +2,6 @@ if not pcall(require, "fzf") then return end --- local fzf = require "fzf" local fzf_helpers = require("fzf.helpers") local path = require "fzf-lua.path" local core = require "fzf-lua.core" diff --git a/lua/fzf-lua/providers/helptags.lua b/lua/fzf-lua/providers/helptags.lua index b88b174..c5c818b 100644 --- a/lua/fzf-lua/providers/helptags.lua +++ b/lua/fzf-lua/providers/helptags.lua @@ -2,7 +2,6 @@ if not pcall(require, "fzf") then return end -local fzf = require "fzf" local path = require "fzf-lua.path" local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" diff --git a/lua/fzf-lua/providers/lsp.lua b/lua/fzf-lua/providers/lsp.lua index 302c474..4e47295 100644 --- a/lua/fzf-lua/providers/lsp.lua +++ b/lua/fzf-lua/providers/lsp.lua @@ -2,9 +2,6 @@ if not pcall(require, "fzf") then return end -local fzf = require "fzf" --- local fzf_helpers = require("fzf.helpers") --- local path = require "fzf-lua.path" local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" local config = require "fzf-lua.config" diff --git a/lua/fzf-lua/providers/manpages.lua b/lua/fzf-lua/providers/manpages.lua index 90a12e3..8b275c9 100644 --- a/lua/fzf-lua/providers/manpages.lua +++ b/lua/fzf-lua/providers/manpages.lua @@ -2,7 +2,6 @@ if not pcall(require, "fzf") then return end -local fzf = require "fzf" local fzf_helpers = require("fzf.helpers") local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" diff --git a/lua/fzf-lua/providers/module.lua b/lua/fzf-lua/providers/module.lua index 84ed6a8..26b979a 100644 --- a/lua/fzf-lua/providers/module.lua +++ b/lua/fzf-lua/providers/module.lua @@ -2,7 +2,6 @@ if not pcall(require, "fzf") then return end -local fzf = require "fzf" local action = require("fzf.actions").action local core = require "fzf-lua.core" local config = require "fzf-lua.config" diff --git a/lua/fzf-lua/providers/nvim.lua b/lua/fzf-lua/providers/nvim.lua new file mode 100644 index 0000000..dd25c91 --- /dev/null +++ b/lua/fzf-lua/providers/nvim.lua @@ -0,0 +1,220 @@ +if not pcall(require, "fzf") then + return +end + +local action = require("fzf.actions").action +local core = require "fzf-lua.core" +local utils = require "fzf-lua.utils" +local config = require "fzf-lua.config" +local actions = require "fzf-lua.actions" + +local M = {} + +M.commands = function(opts) + + opts = config.normalize_opts(opts, config.globals.nvim.commands) + + coroutine.wrap(function () + + local commands = vim.api.nvim_get_commands {} + + local prev_act = action(function (args) + local cmd = args[1] + if commands[cmd] then + cmd = vim.inspect(commands[cmd]) + end + return cmd + end) + + local entries = {} + for k, _ in pairs(commands) do + table.insert(entries, utils.ansi_codes.magenta(k)) + end + + table.sort(entries, function(a, b) return a 0) or not opts.ignore_empty then + table.insert(entries, string.format("[%s] %s", + utils.ansi_codes.yellow(r), contents)) + end + end + + opts.nomulti = true + opts.preview = prev_act + + local selected = core.fzf(opts, entries, + core.build_fzf_cli(opts), + config.winopts(opts)) + + if not selected then return end + actions.act(opts.actions, selected) + + end)() +end + +M.keymaps = function(opts) + + opts = config.normalize_opts(opts, config.globals.nvim.keymaps) + + coroutine.wrap(function () + + local modes = { "n", "i", "c" } + local keymaps = {} + + local add_keymap = function(keymap) + -- hijack field + keymap.str = string.format("[%s:%s:%s]", + utils.ansi_codes.yellow(tostring(keymap.buffer)), + utils.ansi_codes.green(keymap.mode), + utils.ansi_codes.magenta(keymap.lhs)) + local k = string.format("[%s:%s:%s]", + keymap.buffer, keymap.mode, keymap.lhs) + keymaps[k] = keymap + end + + for _, mode in pairs(modes) do + local global = vim.api.nvim_get_keymap(mode) + for _, keymap in pairs(global) do + add_keymap(keymap) + end + local buf_local = vim.api.nvim_buf_get_keymap(0, mode) + for _, keymap in pairs(buf_local) do + add_keymap(keymap) + end + end + + local prev_act = action(function (args) + local k = args[1]:match("(%[.*%]) ") + local v = keymaps[k] + if v then + -- clear hijacked field + v.str = nil + k = vim.inspect(v) + end + return k + end) + + local entries = {} + for _, v in pairs(keymaps) do + table.insert(entries, string.format("%-50s %s", + v.str, v.rhs)) + end + + opts.nomulti = true + opts.preview = prev_act + + local selected = core.fzf(opts, entries, + core.build_fzf_cli(opts), + config.winopts(opts)) + + if not selected then return end + actions.act(opts.actions, selected) + + end)() +end + +M.spell_suggest = function(opts) + + -- if not vim.wo.spell then return false end + opts = config.normalize_opts(opts, config.globals.nvim.spell_suggest) + + coroutine.wrap(function () + + local cursor_word = vim.fn.expand "" + local entries = vim.fn.spellsuggest(cursor_word) + + if vim.tbl_isempty(entries) then return end + + opts.nomulti = true + opts.preview = nil + opts.preview_window = 'hidden:down:0' + + local selected = core.fzf(opts, entries, + core.build_fzf_cli(opts), + config.winopts(opts)) + + if not selected then return end + actions.act(opts.actions, selected) + + end)() + +end + +return M diff --git a/lua/fzf-lua/providers/oldfiles.lua b/lua/fzf-lua/providers/oldfiles.lua index 34fcd2c..8007a4a 100644 --- a/lua/fzf-lua/providers/oldfiles.lua +++ b/lua/fzf-lua/providers/oldfiles.lua @@ -2,9 +2,6 @@ if not pcall(require, "fzf") then return end --- local fzf = require "fzf" -local fzf_helpers = require("fzf.helpers") --- local path = require "fzf-lua.path" local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" local config = require "fzf-lua.config" diff --git a/lua/fzf-lua/providers/quickfix.lua b/lua/fzf-lua/providers/quickfix.lua index e6e90af..66df297 100644 --- a/lua/fzf-lua/providers/quickfix.lua +++ b/lua/fzf-lua/providers/quickfix.lua @@ -3,8 +3,6 @@ if not pcall(require, "fzf") then end --- local fzf = require "fzf" --- local fzf_helpers = require("fzf.helpers") local core = require "fzf-lua.core" local utils = require "fzf-lua.utils" local config = require "fzf-lua.config" diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index d1dfe88..e13f082 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -194,7 +194,7 @@ function M.send_ctrl_c() vim.api.nvim_replace_termcodes("", true, false, true), 'n', true) end -function M.feed_key(key) +function M.feed_keys_termcodes(key) vim.api.nvim_feedkeys( vim.api.nvim_replace_termcodes(key, true, false, true), 'n', true) end