diff options
author | Toby Vincent <tobyv13@gmail.com> | 2023-02-10 16:51:29 -0600 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2023-02-10 19:12:37 -0600 |
commit | 8cf873086c3a49fdc0f4aff5b5042c40f32b8537 (patch) | |
tree | 0f5430518ee418aac4366652e073ec88e56d547e /nvim/.config | |
parent | 5d9a8d9c5a54aa5ca8407633a5bcd993635043e6 (diff) |
feat(nvim): add telescope keymaps for pickers
add telescope picker mappings for renaming files in find_files and
deleting buffers
Diffstat (limited to 'nvim/.config')
-rw-r--r-- | nvim/.config/nvim/lua/tobyvin/plugins/telescope.lua | 57 |
1 files changed, 51 insertions, 6 deletions
diff --git a/nvim/.config/nvim/lua/tobyvin/plugins/telescope.lua b/nvim/.config/nvim/lua/tobyvin/plugins/telescope.lua index aad4bd3..e9b30e3 100644 --- a/nvim/.config/nvim/lua/tobyvin/plugins/telescope.lua +++ b/nvim/.config/nvim/lua/tobyvin/plugins/telescope.lua @@ -41,6 +41,12 @@ local M = { cache_picker = { num_pickers = 10, }, + mappings = { + i = { + ["<Esc>"] = "close", + ["<C-h>"] = "which_key", + }, + }, }, pickers = { find_files = { @@ -51,6 +57,11 @@ local M = { "--hidden", "--strip-cwd-prefix", }, + mappings = { + i = { + ["<C-r>"] = "move_file", + }, + }, }, oldfiles = { only_cwd = true, @@ -61,6 +72,11 @@ local M = { buffers = { show_all_buffers = true, sort_lastused = true, + mappings = { + i = { + ["<C-d>"] = "delete_buffer", + }, + }, }, }, extensions = { @@ -129,12 +145,41 @@ function M.init() end function M.config(_, opts) - opts.defaults.mappings = { - i = { - ["<Esc>"] = require("telescope.actions").close, - ["<C-h>"] = require("telescope.actions").which_key, - }, - } + local actions = require("telescope.actions") + + --- Rename files for |telescope.picker.find_files|.<br> + ---@param prompt_bufnr number: The prompt bufnr + actions.move_file = function(prompt_bufnr) + local Path = require("plenary.path") + + local finders = require("telescope.finders") + local action_state = require("telescope.actions.state") + local picker = action_state.get_current_picker(prompt_bufnr) + local entry = action_state.get_selected_entry() + + if not entry then + return + end + + local old_path = Path:new(entry[1]) + vim.ui.input( + { prompt = "Insert a new name: ", default = old_path:make_relative(), completion = "file" }, + function(file) + vim.cmd([[ redraw ]]) -- redraw to clear out vim.ui.prompt to avoid hit-enter prompt + + local new_path = Path:new(file) + if new_path.filename == "" or old_path.filename == new_path.filename then + return + end + + new_path:parent():mkdir({ parents = true }) + old_path:rename({ new_name = new_path.filename }) + + -- TODO: figure out how to properly refresh picker + picker:refresh(finders.new_oneshot_job(opts.pickers.find_files.find_command, {})) + end + ) + end require("telescope").setup(opts) require("telescope").load_extension("fzf") |