Skip to content

Commit

Permalink
feat: customizable keymaps for session lens, fixes #347
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronr committed Aug 9, 2024
1 parent 47b5f27 commit 0408539
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<C-D>" },
alternate_session = { "i", "<C-S>" },
},
},
})
end,
Expand Down
24 changes: 23 additions & 1 deletion lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<C-D>" },
alternate_session = { "i", "<C-S>" },
},
},
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
}
Expand Down
31 changes: 7 additions & 24 deletions lua/auto-session/session-lens/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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", "<c-d>", Actions.delete_session)
map("i", "<c-s>", Actions.alternate_session)
map("i", "<c-a>", 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,
}
Expand Down

0 comments on commit 0408539

Please sign in to comment.