From cecebaf3a25c5c2f2a311877cce6a11b99262b60 Mon Sep 17 00:00:00 2001 From: James Trew Date: Wed, 29 May 2024 23:00:43 -0400 Subject: [PATCH 1/3] feat(builtin.buffers): enhance and bind `delete_buffer` action --- lua/telescope/actions/init.lua | 13 +++++++++++++ lua/telescope/builtin/__internal.lua | 3 +++ lua/telescope/pickers.lua | 1 + 3 files changed, 17 insertions(+) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 3daaefd287..13168d46ad 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -1176,9 +1176,22 @@ end ---@param prompt_bufnr number: The prompt bufnr actions.delete_buffer = function(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr) + current_picker:delete_selection(function(selection) local force = vim.api.nvim_buf_get_option(selection.bufnr, "buftype") == "terminal" local ok = pcall(vim.api.nvim_buf_delete, selection.bufnr, { force = force }) + + -- If the current buffer is deleted, switch to the previous buffer + -- according to bdelete behavior + if ok and selection.bufnr == current_picker.original_bufnr then + local jumplist = vim.fn.getjumplist(current_picker.original_win_id)[1] + for i = #jumplist, 1, -1 do + if jumplist[i].bufnr ~= selection.bufnr and vim.fn.bufloaded(jumplist[i].bufnr) == 1 then + vim.api.nvim_win_set_buf(current_picker.original_win_id, jumplist[i].bufnr) + break + end + end + end return ok end) end diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 8fa988b966..5011d1a892 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -972,6 +972,9 @@ internal.buffers = function(opts) previewer = conf.grep_previewer(opts), sorter = conf.generic_sorter(opts), default_selection_index = default_selection_idx, + attach_mappings = function(_, map) + map({ "i", "n" }, "", actions.delete_buffer) + end, }) :find() end diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 189e923dfc..1cb62f6543 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -535,6 +535,7 @@ function Picker:find() self.__original_mousemoveevent = vim.o.mousemoveevent vim.o.mousemoveevent = true + self.original_bufnr = a.nvim_get_current_buf() self.original_win_id = a.nvim_get_current_win() _, self.original_cword = pcall(vim.fn.expand, "") _, self.original_cWORD = pcall(vim.fn.expand, "") From ef6d7dded83a72833a0b7e26d19f777a7d47cb6e Mon Sep 17 00:00:00 2001 From: James Trew Date: Sat, 15 Jun 2024 10:19:11 -0400 Subject: [PATCH 2/3] change default bind and add docs --- lua/telescope/builtin/__internal.lua | 2 +- lua/telescope/builtin/init.lua | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 5011d1a892..fc49fdd1a2 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -973,7 +973,7 @@ internal.buffers = function(opts) sorter = conf.generic_sorter(opts), default_selection_index = default_selection_idx, attach_mappings = function(_, map) - map({ "i", "n" }, "", actions.delete_buffer) + map({ "i", "n" }, "", actions.delete_buffer) end, }) :find() diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index 031eff1f79..a6cd5b81b8 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -339,6 +339,8 @@ builtin.man_pages = require_on_exported_call("telescope.builtin.__internal").man builtin.reloader = require_on_exported_call("telescope.builtin.__internal").reloader --- Lists open buffers in current neovim instance, opens selected buffer on `` +--- - Default keymaps: +--- - ``: delete the currently selected buffer ---@param opts table: options to pass to the picker ---@field cwd string: specify a working directory to filter buffers list by ---@field show_all_buffers boolean: if true, show all buffers, including unloaded buffers (default: true) From 2b7eef4eb94195787a842f03b5e273c3848c480e Mon Sep 17 00:00:00 2001 From: Github Actions Date: Sat, 15 Jun 2024 14:19:27 +0000 Subject: [PATCH 3/3] [docgen] Update doc/telescope.txt skip-checks: true --- doc/telescope.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/telescope.txt b/doc/telescope.txt index 7d3713b4a5..f970ef47a7 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -1434,6 +1434,8 @@ builtin.reloader({opts}) *telescope.builtin.reloader()* builtin.buffers({opts}) *telescope.builtin.buffers()* Lists open buffers in current neovim instance, opens selected buffer on `` + - Default keymaps: + - ``: delete the currently selected buffer Parameters: ~