From a156dd33c3571cf34a4c672003b88c1931347485 Mon Sep 17 00:00:00 2001 From: olimorris Date: Tue, 19 Apr 2022 22:21:06 +0100 Subject: [PATCH] feat: #7 initial Telescope support --- lua/persisted/config.lua | 4 ++ lua/persisted/init.lua | 47 +++++++++++++++--- lua/persisted/utils.lua | 28 +++++++++++ lua/telescope/_extensions/persisted.lua | 64 +++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 lua/persisted/utils.lua create mode 100644 lua/telescope/_extensions/persisted.lua diff --git a/lua/persisted/config.lua b/lua/persisted/config.lua index eeede9a..57fc014 100644 --- a/lua/persisted/config.lua +++ b/lua/persisted/config.lua @@ -10,6 +10,10 @@ local defaults = { 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 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 diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 3b72463..bc55f5f 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -4,6 +4,7 @@ local M = {} local e = vim.fn.fnameescape local echo = vim.api.nvim_echo +local default_branch = "main" local echoerr = function(msg, error) echo({ @@ -90,17 +91,23 @@ local function get_branch() return lines[#lines]:gsub("/", "%%") 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 ---Get the current session for the current working directory and git branch ---@return string local function get_current() - local pattern = "/" - if vim.fn.has("win32") == 1 then - pattern = "[\\:]" - end - local name = vim.fn.getcwd():gsub(pattern, "%%") + local name = vim.fn.getcwd():gsub(get_dir_pattern(), "%%") return config.options.dir .. name .. get_branch() .. ".vim" end @@ -187,7 +194,33 @@ end ---List all of the sessions in the session directory ---@return table 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 ---Determines whether to load, start or stop a session diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua new file mode 100644 index 0000000..5de593d --- /dev/null +++ b/lua/persisted/utils.lua @@ -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 diff --git a/lua/telescope/_extensions/persisted.lua b/lua/telescope/_extensions/persisted.lua new file mode 100644 index 0000000..afde773 --- /dev/null +++ b/lua/telescope/_extensions/persisted.lua @@ -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, + }, +})