feat: add extension config logic, user path substitutions

main
Scott McKendry 2024-06-22 16:31:50 +12:00
parent 0f5c89d330
commit 0c6f777fb5
4 changed files with 74 additions and 12 deletions

View File

@ -1,5 +1,9 @@
local telescope = require("telescope")
local config = require("telescope._extensions.resession.config")
local picker = require("telescope._extensions.resession.picker")
return telescope.register_extension({
exports = { resession = require("telescope._extensions.resession.picker").resession_picker },
setup = config.setup,
exports = { resession = picker.resession_picker },
})

View File

@ -0,0 +1,20 @@
local M = {}
--- @class substitution
--- @field find string The string to find
--- @field replace string The string to replace it with
--- @class config
--- @field path_substitutions? substitution[] A list of substitutions to apply to paths
M.defaults = {
path_substitutions = {},
}
M.opts = {}
function M.setup(opts)
opts = opts or {}
M.opts = vim.tbl_deep_extend("force", M.defaults, opts)
end
return M

View File

@ -12,19 +12,21 @@ local M = {}
--- Load the selected session
---@param prompt_bufnr number
function M.load_session(prompt_bufnr)
local opts = require("telescope._extensions.resession.config").opts
local session = action_state.get_selected_entry()
local encoded = utils.encode_session(session[1])
local encoded = utils.encode_session(session[1], opts)
actions.close(prompt_bufnr)
require("resession").load(encoded, { dir = "dirsession" })
end
--- Render the session picker
function M.resession_picker()
local opts = require("telescope._extensions.resession.config").opts
local dropdown = themes.get_dropdown({})
local resession_opts = {
prompt_title = "Find session",
finder = require("telescope.finders").new_table({
results = utils.get_results(),
results = utils.get_results(opts),
}),
attach_mappings = function(_, map)

View File

@ -1,21 +1,56 @@
local M = {}
local internal_substitutions = {
{ find = ":/", replace = "__" },
{ find = "/", replace = "_" },
}
--- Apply a list of substitutions to a path/session string
--- @param path string The path/session string to apply the substitutions to
--- @param substitutions substitution[]? The list of substitutions to apply
--- @param reverse? boolean Whether to apply the substitutions in reverse, i.e. replace the replace string with the find string
M.apply_substitutions = function(path, substitutions, reverse)
reverse = reverse or false
if not substitutions then
return path
end
for _, substitution in ipairs(substitutions) do
if reverse then
path = path:gsub(substitution.replace, substitution.find)
else
path = path:gsub(substitution.find, substitution.replace)
end
end
return path
end
--- Encode a session string in the format used by resession
--- @param session_str string The session string to encode
--- @param opts config telescope-resession configuration
--- @return string The encoded session string
M.encode_session = function(session_str)
session_str = session_str:gsub(":/", "__"):gsub("/", "_")
M.encode_session = function(session_str, opts)
local user_substitutions = opts.path_substitutions
if user_substitutions == {} then
print("No path substitutions defined. Using default substitutions.")
end
session_str = M.apply_substitutions(session_str, user_substitutions, true)
session_str = M.apply_substitutions(session_str, internal_substitutions)
return session_str
end
--- Decode a list of session strings in the format used by resession to a friendlier format
--- TODO: Test in linux with different paths
--- TODO: Support path formatting options (e.g. relative paths) ✨
--- @param sessions string[] The list of session strings to decode. Usually the ouotput of resession.list()
--- @param sessions string[] The list of session strings to decode. Usually the output of resession.list()
--- @param opts config telescope-resession configuration
--- @return string[] The decoded session strings
M.decode_sessions = function(sessions)
M.decode_sessions = function(sessions, opts)
for i, session in ipairs(sessions) do
sessions[i] = session:gsub("__", ":/"):gsub("_", "/")
session = M.apply_substitutions(session, internal_substitutions, true)
session = M.apply_substitutions(session, opts.path_substitutions)
sessions[i] = session
end
return sessions
end
@ -27,9 +62,10 @@ M.get_sessions = function()
end
--- Get a list of sessions from resession and decode them
--- @param opts config telescope-resession configuration
--- @return string[] The list of decoded sessions
M.get_results = function()
return M.decode_sessions(M.get_sessions())
M.get_results = function(opts)
return M.decode_sessions(M.get_sessions(), opts)
end
return M