feat: #7 initial Telescope support

main
olimorris 2022-04-19 22:21:06 +01:00
parent b6201178ff
commit a156dd33c3
4 changed files with 136 additions and 7 deletions

View File

@ -10,6 +10,10 @@ local defaults = {
ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading
before_save = function() end, -- function to run before the session is saved to disk before_save = function() end, -- function to run before the session is saved to disk
after_save = function() end, -- function to run after the session is saved to disk after_save = function() end, -- function to run after the session is saved to disk
telescope = { -- options for the telescope extension
before_source = function(session) end, -- function to run before the session is sourced via telescope
after_source = function(session) end, -- function to run after the session is sourced via telescope
},
} }
---@type PersistedOptions ---@type PersistedOptions

View File

@ -4,6 +4,7 @@ local M = {}
local e = vim.fn.fnameescape local e = vim.fn.fnameescape
local echo = vim.api.nvim_echo local echo = vim.api.nvim_echo
local default_branch = "main"
local echoerr = function(msg, error) local echoerr = function(msg, error)
echo({ echo({
@ -90,17 +91,23 @@ local function get_branch()
return lines[#lines]:gsub("/", "%%") return lines[#lines]:gsub("/", "%%")
end end
return "" return "_" .. default_branch
end
---Get the directory pattern based on OS
---@return string
local function get_dir_pattern()
local pattern = "/"
if vim.fn.has("win32") == 1 then
pattern = "[\\:]"
end
return pattern
end end
---Get the current session for the current working directory and git branch ---Get the current session for the current working directory and git branch
---@return string ---@return string
local function get_current() local function get_current()
local pattern = "/" local name = vim.fn.getcwd():gsub(get_dir_pattern(), "%%")
if vim.fn.has("win32") == 1 then
pattern = "[\\:]"
end
local name = vim.fn.getcwd():gsub(pattern, "%%")
return config.options.dir .. name .. get_branch() .. ".vim" return config.options.dir .. name .. get_branch() .. ".vim"
end end
@ -187,7 +194,33 @@ end
---List all of the sessions in the session directory ---List all of the sessions in the session directory
---@return table ---@return table
function M.list() function M.list()
return vim.fn.glob(config.options.dir .. "*.vim", true, true) local utils = require("persisted.utils")
local session_files = vim.fn.glob(config.options.dir .. "*.vim", true, true)
local sessions = {}
for _, session in pairs(session_files) do
local session_name = session
:gsub(config.options.dir, "")
:gsub("%%", get_dir_pattern())
:gsub(vim.fn.expand("~"), get_dir_pattern())
:gsub("//", "")
local branch = utils.get_last_item(utils.split_str(session_name, "_")):gsub(".vim", "")
local pwd = vim.fn.expand("~") .. get_dir_pattern() .. session_name
local branch_name = "_" .. utils.get_last_item(utils.split_str(pwd, "_"))
pwd = pwd:gsub(branch_name, "")
table.insert(sessions, {
["name"] = session_name,
["file_path"] = session,
["branch"] = branch,
["pwd"] = pwd,
})
end
return sessions
end end
---Determines whether to load, start or stop a session ---Determines whether to load, start or stop a session

28
lua/persisted/utils.lua Normal file
View File

@ -0,0 +1,28 @@
local M = {}
--- Split a string into a table
---@param input string
---@param sep string
---@return table
function M.split_str(input, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(input, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
--- Get the last element in a table
---@param table table
---@return string
function M.get_last_item(table)
for i, v in pairs(table) do
last = #table - 0
end
return table[last]
end
return M

View File

@ -0,0 +1,64 @@
local actions = require("telescope.actions")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values
local entry_display = require("telescope.pickers.entry_display")
local action_state = require("telescope.actions.state")
local config = require("persisted.config").options
local sessions = require("persisted").list()
local function search_sessions(opts)
local displayer = entry_display.create({
separator = "",
items = {
{ width = 50 },
{ width = 10 },
{ remaining = true },
},
})
local make_display = function(entry)
return displayer({
entry.name,
entry.branch,
})
end
local make_entry = function(item)
return {
ordinal = item.name,
display = make_display,
name = item.name,
branch = item.branch,
pwd = item.pwd,
file_path = item.file_path,
}
end
pickers.new(opts, {
prompt_title = "Sessions",
sorter = conf.generic_sorter(opts),
finder = finders.new_table({
results = sessions,
entry_maker = make_entry,
}),
attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function()
local session = action_state.get_selected_entry()
actions.close(prompt_bufnr)
pcall(require("persisted").stop(), "")
config.telescope.before_source(session)
pcall(vim.cmd, "source " .. vim.fn.fnameescape(session.file_path))
config.telescope.after_source(session)
end)
return true
end,
}):find()
end
return require("telescope").register_extension({
exports = {
persisted = search_sessions,
},
})