From d40473faaf9212a77a53cdd5865dae42e04e89ea Mon Sep 17 00:00:00 2001 From: don Date: Tue, 13 Aug 2024 21:03:38 -0700 Subject: [PATCH] Refresh telescope picker after deleting a session file --- lua/seshi/config.lua | 2 +- lua/telescope/_extensions/seshi.lua | 76 ++++++++++++++++------------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/lua/seshi/config.lua b/lua/seshi/config.lua index 6b387d7..1199460 100644 --- a/lua/seshi/config.lua +++ b/lua/seshi/config.lua @@ -5,7 +5,7 @@ function M.defaults() save_dir = vim.fn.expand(vim.fn.stdpath('data') .. '/sessions/'), autoload = true, use_git_root = true, - silent = false, + silent = true, telescope = { mappings = { delete_session = '', diff --git a/lua/telescope/_extensions/seshi.lua b/lua/telescope/_extensions/seshi.lua index a4f150c..b68af15 100644 --- a/lua/telescope/_extensions/seshi.lua +++ b/lua/telescope/_extensions/seshi.lua @@ -7,49 +7,53 @@ local action_state = require('telescope.actions.state') local seshi = require('seshi') local utils = require('seshi.utils') -local function list_sessions() - local sessions = {} - local save_dir = seshi.options.save_dir - local files = vim.fn.glob(save_dir .. '*', false, true) +local function search_sessions(opts) + opts = opts or {} - for _, file in ipairs(files) do - local session_name = vim.fn.fnamemodify(file, ':t') - local dir_path, branch = utils.split_session_filename(session_name) - -- Decode the path and branch - dir_path = utils.decode_path(dir_path) - branch = branch and utils.decode_path(branch) or nil + local function list_sessions() + local sessions = {} + local save_dir = seshi.options.save_dir + local files = vim.fn.glob(save_dir .. '*', false, true) - table.insert(sessions, { - name = session_name, - file_path = file, - dir_path = dir_path, - branch = branch, - }) - end + for _, file in ipairs(files) do + local session_name = vim.fn.fnamemodify(file, ':t') + local dir_path, branch = utils.split_session_filename(session_name) + -- Decode the path and branch + dir_path = utils.decode_path(dir_path) + branch = branch and utils.decode_path(branch) or nil - return sessions -end + table.insert(sessions, { + name = session_name, + file_path = file, + dir_path = dir_path, + branch = branch, + }) + end -local function search_sessions(opts) - opts = opts or {} + return sessions + end + + local function make_finder() + return finders.new_table({ + results = list_sessions(), + entry_maker = function(entry) + local display = entry.dir_path + if entry.branch then + display = display .. ' (' .. entry.branch .. ')' + end + return { + value = entry, + display = display, + ordinal = entry.name, + } + end, + }) + end pickers .new(opts, { prompt_title = 'Sessions', - finder = finders.new_table({ - results = list_sessions(), - entry_maker = function(entry) - local display = entry.dir_path - if entry.branch then - display = display .. ' (' .. entry.branch .. ')' - end - return { - value = entry, - display = display, - ordinal = entry.name, - } - end, - }), + finder = make_finder(), sorter = conf.generic_sorter(opts), attach_mappings = function(prompt_bufnr, map) actions.select_default:replace(function() @@ -63,6 +67,8 @@ local function search_sessions(opts) if selection then local file_path = selection.value.file_path seshi.delete_session(file_path) + local current_picker = action_state.get_current_picker(prompt_bufnr) + current_picker:refresh(make_finder(), { reset_prompt = false }) end end) return true