feat: add extension config logic, user path substitutions
parent
0f5c89d330
commit
0c6f777fb5
|
|
@ -1,5 +1,9 @@
|
||||||
local telescope = require("telescope")
|
local telescope = require("telescope")
|
||||||
|
|
||||||
|
local config = require("telescope._extensions.resession.config")
|
||||||
|
local picker = require("telescope._extensions.resession.picker")
|
||||||
|
|
||||||
return telescope.register_extension({
|
return telescope.register_extension({
|
||||||
exports = { resession = require("telescope._extensions.resession.picker").resession_picker },
|
setup = config.setup,
|
||||||
|
exports = { resession = picker.resession_picker },
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -12,19 +12,21 @@ local M = {}
|
||||||
--- Load the selected session
|
--- Load the selected session
|
||||||
---@param prompt_bufnr number
|
---@param prompt_bufnr number
|
||||||
function M.load_session(prompt_bufnr)
|
function M.load_session(prompt_bufnr)
|
||||||
|
local opts = require("telescope._extensions.resession.config").opts
|
||||||
local session = action_state.get_selected_entry()
|
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)
|
actions.close(prompt_bufnr)
|
||||||
require("resession").load(encoded, { dir = "dirsession" })
|
require("resession").load(encoded, { dir = "dirsession" })
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Render the session picker
|
--- Render the session picker
|
||||||
function M.resession_picker()
|
function M.resession_picker()
|
||||||
|
local opts = require("telescope._extensions.resession.config").opts
|
||||||
local dropdown = themes.get_dropdown({})
|
local dropdown = themes.get_dropdown({})
|
||||||
local resession_opts = {
|
local resession_opts = {
|
||||||
prompt_title = "Find session",
|
prompt_title = "Find session",
|
||||||
finder = require("telescope.finders").new_table({
|
finder = require("telescope.finders").new_table({
|
||||||
results = utils.get_results(),
|
results = utils.get_results(opts),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
attach_mappings = function(_, map)
|
attach_mappings = function(_, map)
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,56 @@
|
||||||
local M = {}
|
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
|
--- Encode a session string in the format used by resession
|
||||||
--- @param session_str string The session string to encode
|
--- @param session_str string The session string to encode
|
||||||
|
--- @param opts config telescope-resession configuration
|
||||||
--- @return string The encoded session string
|
--- @return string The encoded session string
|
||||||
M.encode_session = function(session_str)
|
M.encode_session = function(session_str, opts)
|
||||||
session_str = session_str:gsub(":/", "__"):gsub("/", "_")
|
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
|
return session_str
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Decode a list of session strings in the format used by resession to a friendlier format
|
--- Decode a list of session strings in the format used by resession to a friendlier format
|
||||||
--- TODO: Test in linux with different paths
|
--- @param sessions string[] The list of session strings to decode. Usually the output of resession.list()
|
||||||
--- TODO: Support path formatting options (e.g. relative paths) ✨
|
--- @param opts config telescope-resession configuration
|
||||||
--- @param sessions string[] The list of session strings to decode. Usually the ouotput of resession.list()
|
|
||||||
--- @return string[] The decoded session strings
|
--- @return string[] The decoded session strings
|
||||||
M.decode_sessions = function(sessions)
|
M.decode_sessions = function(sessions, opts)
|
||||||
for i, session in ipairs(sessions) do
|
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
|
end
|
||||||
return sessions
|
return sessions
|
||||||
end
|
end
|
||||||
|
|
@ -27,9 +62,10 @@ M.get_sessions = function()
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Get a list of sessions from resession and decode them
|
--- Get a list of sessions from resession and decode them
|
||||||
|
--- @param opts config telescope-resession configuration
|
||||||
--- @return string[] The list of decoded sessions
|
--- @return string[] The list of decoded sessions
|
||||||
M.get_results = function()
|
M.get_results = function(opts)
|
||||||
return M.decode_sessions(M.get_sessions())
|
return M.decode_sessions(M.get_sessions(), opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue