From 0408539ac49c92253e72bd66bbd7ea3bcc7d7a45 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Thu, 8 Aug 2024 23:14:34 -0700 Subject: [PATCH] feat: customizable keymaps for session lens, fixes #347 --- README.md | 5 +++++ lua/auto-session/init.lua | 24 +++++++++++++++++++- lua/auto-session/session-lens/init.lua | 31 ++++++-------------------- 3 files changed, 35 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 3ac580b..a7f9c5f 100644 --- a/README.md +++ b/README.md @@ -391,6 +391,11 @@ You can use Telescope to see, load, and delete your sessions. It's enabled by de load_on_setup = true, theme_conf = { border = true }, previewer = false, + mappings = { + -- Mode can be a string or a table, e.g. {"i", "n"} for both insert and normal mode + delete_session = { "i", "" }, + alternate_session = { "i", "" }, + }, }, }) end, diff --git a/lua/auto-session/init.lua b/lua/auto-session/init.lua index 7e0c2a7..9245388 100644 --- a/lua/auto-session/init.lua +++ b/lua/auto-session/init.lua @@ -100,19 +100,41 @@ local luaOnlyConf = { --- post_cwd_changed_hook = nil, -- lua function hook. This is called after auto_session code runs for the `DirChanged` autocmd --- } cwd_change_handling = false, + + ---Session Lens Config + ---@class session_lens_config + ---@field load_on_setup? boolean + ---@field shorten_path? boolean Deprecated, pass { 'shorten' } to path_display + ---@field path_display? table An array that specifies how to handle paths. Read :h telescope.defaults.path_display + ---@field theme_conf? table + ---@field buftypes_to_ignore? table Deprecated, if you're using this please report your usage on github + ---@field previewer? boolean + ---@field session_control? session_control + ---@field mappings? session_lens_mapping + ---Session Control Config ---@class session_control ---@field control_dir string ---@field control_filename string + ---Session Lens Mapping + ---@class session_lens_mapping + ---@field delete_session table mode and key for deleting a session from the picker + ---@field alternate_session table mode and key for swapping to alertnate session from the picker + ---@type session_lens_config session_lens = { - buftypes_to_ignore = {}, -- list of bufftypes to ignore when switching between sessions load_on_setup = true, + buftypes_to_ignore = {}, session_control = { control_dir = vim.fn.stdpath "data" .. "/auto_session/", -- Auto session control dir, for control files, like alternating between two sessions with session-lens control_filename = "session_control.json", -- File name of the session control file }, + mappings = { + -- Mode can be a string or a table, e.g. {"i", "n"} for both insert and normal mode + delete_session = { "i", "" }, + alternate_session = { "i", "" }, + }, }, silent_restore = true, -- Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error } diff --git a/lua/auto-session/session-lens/init.lua b/lua/auto-session/session-lens/init.lua index bb47ecf..96c4865 100644 --- a/lua/auto-session/session-lens/init.lua +++ b/lua/auto-session/session-lens/init.lua @@ -7,28 +7,8 @@ local SessionLens = { conf = {}, } ----Session Lens Config ----@class session_lens_config ----@field shorten_path? boolean Deprecated, pass { 'shorten' } to path_display ----@field path_display? table An array that specifies how to handle paths. Read :h telescope.defaults.path_display ----@field theme_conf? table ----@field buftypes_to_ignore? table Deprecated, if you're using this please report your usage on github ----@field previewer? boolean ----@field session_control? session_control ----@field load_on_setup? boolean - ----@type session_lens_config -local defaultConf = { - theme_conf = {}, - previewer = false, - buftypes_to_ignore = {}, -} - --- Set default config on plugin load -SessionLens.conf = defaultConf - function SessionLens.setup() - SessionLens.conf = vim.tbl_deep_extend("force", SessionLens.conf, AutoSession.conf.session_lens) + SessionLens.conf = AutoSession.conf.session_lens if SessionLens.conf.buftypes_to_ignore ~= nil and not vim.tbl_isempty(SessionLens.conf.buftypes_to_ignore) then Lib.logger.warn "buftypes_to_ignore is deprecated. If you think you need this option, please file a bug on GitHub. If not, please remove it from your config" @@ -131,9 +111,12 @@ SessionLens.search_session = function(custom_opts) cwd = session_root_dir, attach_mappings = function(_, map) telescope_actions.select_default:replace(Actions.source_session) - map("i", "", Actions.delete_session) - map("i", "", Actions.alternate_session) - map("i", "", Actions.alternate_session) + + local mappings = AutoSession.conf.session_lens.mappings + if mappings then + map(mappings.delete_session[1], mappings.delete_session[2], Actions.delete_session) + map(mappings.alternate_session[1], mappings.alternate_session[2], Actions.alternate_session) + end return true end, }