diff --git a/README.md b/README.md index 3aab81f..b593ebc 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,12 @@ Auto Session takes advantage of Neovim's existing session management capabilitie 1. When starting `nvim` with no arguments, auto-session will try to restore an existing session for the current `cwd` if one exists. 2. When starting `nvim .` with some argument, auto-session will do nothing. -3. Even after starting `nvim` with an argument, a session can still be manually restored by running `:RestoreSession`. +3. Even after starting `nvim` with an argument, a session can still be manually restored by running `:SessionRestore`. 4. Any session saving and restoration takes into consideration the current working directory `cwd`. 5. When piping to `nvim`, e.g: `cat myfile | nvim`, auto-session behaves like #2. :warning: Please note that if there are errors in your config, restoring the session might fail, if that happens, auto session will then disable auto saving for the current session. -Manually saving a session can still be done by calling `:SaveSession`. +Manually saving a session can still be done by calling `:SessionSave`. AutoSession now tracks `cwd` changes! By default, handling is as follows: @@ -164,7 +164,7 @@ set sessionoptions+=winpos,terminal,folds ### Last Session This optional feature enables the keeping track and loading of the last session. -This loading of a last session happens only when a `RestoreSession` could not find a session for the current dir. +This loading of a last session happens only when a `SessionRestore` could not find a session for the current dir. This feature can come in handy when starting Neovim from a GUI for example. :warning: This feature is still experimental and as of right now it interferes with the plugin's ability to auto create new sessions when opening Neovim in a new directory. @@ -181,13 +181,13 @@ require('auto-session').setup { Auto Session exposes two commands that can be used or mapped to any keybindings for manually saving and restoring sessions. ```viml -:SaveSession " saves or creates a session in the currently set `auto_session_root_dir`. -:SaveSession ~/my/custom/path " saves or creates a session in the specified directory path. -:RestoreSession " restores a previously saved session based on the `cwd`. -:RestoreSession ~/my/custom/path " restores a previously saved session based on the provided path. -:RestoreSessionFromFile ~/session/path " restores any currently saved session -:DeleteSession " deletes a session in the currently set `auto_session_root_dir`. -:DeleteSession ~/my/custom/path " deleetes a session based on the provided path. +:SessionSave " saves or creates a session in the currently set `auto_session_root_dir`. +:SessionSave ~/my/custom/path " saves or creates a session in the specified directory path. +:SessionRestore " restores a previously saved session based on the `cwd`. +:SessionRestore ~/my/custom/path " restores a previously saved session based on the provided path. +:SessionRestoreFromFile ~/session/path " restores any currently saved session +:SessionDelete " deletes a session in the currently set `auto_session_root_dir`. +:SessionDelete ~/my/custom/path " deleetes a session based on the provided path. :Autosession search :Autosession delete ``` @@ -276,20 +276,30 @@ Session Lens has been merged into Auto Session! This means all the functionality You still need to call the session-lens specific setup function for things to work properly since even though these plugins are now merged, they are effectily fully modular and auto-session does not depend on session-lens functionality. ```lua -require("session-lens").setup { - path_display = { "shorten" }, - theme_conf = { border = true }, - previewer = false, +require("auto-session").setup { + log_level = vim.log.levels.ERROR, + auto_session_suppress_dirs = { "~/", "~/Projects", "~/Downloads", "/" }, + auto_session_use_git_branch = false, + + auto_session_enable_last_session = false, + + session_lens = { + theme_conf = { border = true }, + previewer = false, + }, } --- Mapping to search sessions using the session-lens search_session function -vim.keymap.set("n", "", require("session-lens").search_session, { +-- Set mapping for searching a session. +vim.keymap.set("n", "", require("auto-session.session-lens").search_session, { noremap = true, }) ``` -Auto Session provides its own `:Autosession search` and `:Autosession delete` commands, but session-lens is a more complete version of those commands that is specificly built to be used with `telescope.nvim`. These commands make use of `vim.ui.select` which can itself be implemented by other plugins other than telescope. -Sometime after `telescope.nvim` has been started, you'll want to call `require("telescope").load_extension "session-lens"` so that auto completion can happen for the `:Telescope session-lens` commands. +*Note:* hitting `` on an open session-lens picker will automatically try to restore the previous session opened. This can give you a nice flow if you're constantly switching between two projects. + +Sometime after `telescope.nvim` has been started, you'll want to call ```lua require("telescope").load_extension "session-lens"```` so that command completion works for `:Telescope session-lens` commands. + +Auto Session provides its own `:Autosession search` and `:Autosession delete` commands, but session-lens is a more complete version of those commands that is specificly built to be used with `telescope.nvim`. These commands make use of `vim.ui.select` which can itself be implemented by other plugins other than telescope. ### Preview diff --git a/lua/auto-session.lua b/lua/auto-session/init.lua similarity index 89% rename from lua/auto-session.lua rename to lua/auto-session/init.lua index 3ea9d90..7b190b7 100644 --- a/lua/auto-session.lua +++ b/lua/auto-session/init.lua @@ -1,5 +1,6 @@ local Lib = require "auto-session.lib" -local autocmds = require "auto-session.autocmds" +local AutoCmds = require "auto-session.autocmds" +local SessionLens = require "auto-session.session-lens" ----------- Setup ---------- local AutoSession = { @@ -34,7 +35,7 @@ end ---table default config for auto session ---@class defaultConf ----@field log_level string debug, info, error +---@field log_level string|integer "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR ---@field auto_session_enable_last_session boolean ---@field auto_session_last_session_dir string ---@field auto_session_root_dir string root directory for session files, by default is `vim.fn.stdpath('data')/sessions/` @@ -87,10 +88,12 @@ local luaOnlyConf = { ---@field control_dir string ---@field control_filename string - ---@type session_control - 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 + ---@type session_lens_config + session_lens = { + 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 + }, }, } @@ -108,8 +111,10 @@ function AutoSession.setup(config) AutoSession.conf = vim.tbl_deep_extend("force", AutoSession.conf, config or {}) Lib.ROOT_DIR = AutoSession.conf.auto_session_root_dir Lib.setup(AutoSession.conf) + SessionLens.setup(AutoSession.conf, AutoSession) + AutoCmds.setup_autocmds(AutoSession.conf, AutoSession) - autocmds.setup_autocmds(AutoSession.conf, AutoSession) + SetupAutocmds() end local function is_enabled() @@ -502,8 +507,8 @@ vim.api.nvim_create_user_command("Autosession", handle_autosession_command, { na -- end local function write_to_session_control_json(session_file_name) - local control_dir = AutoSession.conf.session_control.control_dir - local control_file = AutoSession.conf.session_control.control_filename + local control_dir = AutoSession.conf.session_lens.session_control.control_dir + local control_file = AutoSession.conf.session_lens.session_control.control_filename session_file_name = Lib.expand(session_file_name) Lib.init_dir(control_dir) @@ -512,7 +517,7 @@ local function write_to_session_control_json(session_file_name) local log_ending_state = function() local content = vim.fn.readfile(session_control_file) - local session_control = vim.json.decode(content[1] or "{}") + local session_control = vim.json.decode(content[1] or "{}") or {} local sessions = { current = session_control.current, alternate = session_control.alternate } Lib.logger.debug("session control file contents", { sessions = sessions, content = content }) @@ -521,7 +526,7 @@ local function write_to_session_control_json(session_file_name) if vim.fn.filereadable(session_control_file) == 1 then local content = vim.fn.readfile(session_control_file) Lib.logger.debug { content = content } - local json = vim.json.decode(content[1] or "{}") + local json = vim.json.decode(content[1] or "{}") or {} local sessions = { current = session_file_name, @@ -809,4 +814,88 @@ function AutoSession.DeleteSession(...) run_hook_cmds(post_cmds, "post-delete") end +function SetupAutocmds() + Lib.logger.info "Setting up autocmds" + + -- Check if the auto-session plugin has already been loaded to prevent loading it twice + if vim.g.loaded_auto_session ~= nil then + return + end + + -- Initialize variables + vim.g.in_pager_mode = false + + local function SaveSession(args) + return AutoSession.SaveSession(args.args, false) + end + + local function SessionRestore(args) + return AutoSession.RestoreSession(args.args) + end + + local function SessionDelete(args) + return AutoSession.DeleteSession(args.args) + end + + local function DisableAutoSave() + return AutoSession.DisableAutoSave() + end + + vim.api.nvim_create_user_command( + "SessionSave", + SaveSession, + { bang = true, desc = "Save the current session. Based in cwd if no arguments are passed" } + ) + + vim.api.nvim_create_user_command("SessionRestore", SessionRestore, { bang = true, desc = "Restore Session" }) + + vim.api.nvim_create_user_command("DisableAutoSave", DisableAutoSave, { bang = true, desc = "Disable Auto Save" }) + + vim.api.nvim_create_user_command( + "SessionRestoreFromFile", + DisableAutoSave, + { complete = AutoSession.CompleteSessions, bang = true, nargs = "*", desc = "Restore Session from file" } + ) + + vim.api.nvim_create_user_command( + "SessionDelete", + SessionDelete, + { complete = AutoSession.CompleteSessions, bang = true, nargs = "*", desc = "Delete Session" } + ) + + local group = vim.api.nvim_create_augroup("auto_session_group", {}) + + vim.api.nvim_create_autocmd({ "StdinReadPre" }, { + group = group, + pattern = "*", + callback = function() + vim.g.in_pager_mode = true + end, + }) + + vim.api.nvim_create_autocmd({ "VimEnter" }, { + group = group, + pattern = "*", + nested = true, + callback = function() + if not vim.g.in_pager_mode then + AutoSession.AutoRestoreSession() + end + end, + }) + + vim.api.nvim_create_autocmd({ "VimLeave" }, { + group = group, + pattern = "*", + callback = function() + if not vim.g.in_pager_mode then + AutoSession.AutoSaveSession() + end + end, + }) + + -- Set a flag to indicate that the plugin has been loaded + vim.g.loaded_auto_session = true +end + return AutoSession diff --git a/lua/auto-session/session-lens/actions.lua b/lua/auto-session/session-lens/actions.lua index ed66316..ca0508e 100644 --- a/lua/auto-session/session-lens/actions.lua +++ b/lua/auto-session/session-lens/actions.lua @@ -1,9 +1,16 @@ -local AutoSession = require "auto-session" local action_state = require "telescope.actions.state" local actions = require "telescope.actions" local Lib = require "auto-session.lib" -local SessionLensActions = {} +local M = { + conf = {}, + functions = {}, +} + +function M.setup(config, functions) + M.conf = vim.tbl_deep_extend("force", config, M.conf) + M.functions = functions +end -- TODO: Either use this or actually store the latest sessions on load time, then just alternate between them here. -- local function get_second_to_latest_session_by_last_edited() @@ -71,12 +78,11 @@ local SessionLensActions = {} -- end local function get_alternate_session() - local filepath = AutoSession.conf.session_control.control_dir .. AutoSession.conf.session_control.control_filename + local filepath = M.conf.session_control.control_dir .. M.conf.session_control.control_filename if vim.fn.filereadable(filepath) == 1 then local content = vim.fn.readfile(filepath)[1] or "{}" - - local json = vim.json.decode(content) + local json = vim.json.decode(content) or {} -- should never hit the or clause since we're defaulting to a string for content local sessions = { current = json.current, @@ -107,13 +113,13 @@ local function source_session(selection, prompt_bufnr) then -- Take advatage of cwd_change_handling behaviour for switching sessions Lib.logger.debug "Triggering vim.fn.chdir since cwd_change_handling feature is enabled" - vim.fn.chdir(AutoSession.format_file_name(type(selection) == "table" and selection.filename or selection)) + vim.fn.chdir(M.functions.format_file_name(type(selection) == "table" and selection.filename or selection)) else Lib.logger.debug "Triggering session-lens behaviour since cwd_change_handling feature is disabled" - AutoSession.AutoSaveSession() + M.functions.AutoSaveSession() vim.cmd "%bd!" vim.cmd "clearjumps" - AutoSession.RestoreSession(type(selection) == "table" and selection.path or selection) + M.functions.RestoreSession(type(selection) == "table" and selection.path or selection) end end, 50) end @@ -121,7 +127,7 @@ end ---Source session action ---Source a selected session after doing proper current session saving and cleanup ---@param prompt_bufnr number the telescope prompt bufnr -SessionLensActions.source_session = function(prompt_bufnr) +M.source_session = function(prompt_bufnr) local selection = action_state.get_selected_entry() source_session(selection, prompt_bufnr) end @@ -129,14 +135,14 @@ end ---Delete session action ---Delete a selected session file ---@param prompt_bufnr number the telescope prompt bufnr -SessionLensActions.delete_session = function(prompt_bufnr) +M.delete_session = function(prompt_bufnr) local current_picker = action_state.get_current_picker(prompt_bufnr) current_picker:delete_selection(function(selection) - AutoSession.DeleteSession(selection.path) + M.functions.DeleteSession(selection.path) end) end -SessionLensActions.alternate_session = function(prompt_bufnr) +M.alternate_session = function(prompt_bufnr) local alternate_session = get_alternate_session() if not alternate_session then @@ -158,4 +164,4 @@ end ---"/Users/ronnieandrewmagatti/.local/share/nvim/sessions/%Users%ronnieandrewmagatti%Projects%auto-session.vim" ---"/Users/ronnieandrewmagatti/.local/share/nvim/sessions/\\%Users\\%ronnieandrewmagatti\\%Projects\\%auto-session.vim" -return SessionLensActions +return M diff --git a/lua/auto-session/session-lens/init.lua b/lua/auto-session/session-lens/init.lua index c5cdd72..feeb6ca 100644 --- a/lua/auto-session/session-lens/init.lua +++ b/lua/auto-session/session-lens/init.lua @@ -1,17 +1,23 @@ -local Lib = require "session-lens.library" -local AutoSession = require "auto-session" -local SessionLensActions = require "session-lens.actions" +local Lib = require "auto-session.session-lens.library" +local Actions = require "auto-session.session-lens.actions" + +local logger = require("auto-session.logger"):new { + log_level = vim.log.levels.INFO, +} ----------- Setup ---------- local SessionLens = { conf = {}, } ----@class DefaultConf ----@field theme_conf table telescope theme configuration ----@field previewer boolean|table telescope preview configs +---Session Lens Config +---@class session_lens_config +---@field shorten_path boolean +---@field theme_conf table +---@field previewer boolean +---@field session_control session_control ----@type DefaultConf +---@type session_lens_config local defaultConf = { theme_conf = { winblend = 10, border = true }, previewer = false, @@ -21,13 +27,18 @@ local defaultConf = { SessionLens.conf = defaultConf ---Session lens setup function ----@param config DefaultConf the optional config for the setup function -function SessionLens.setup(config) - SessionLens.conf = vim.tbl_deep_extend("force", config, SessionLens.conf) +---@param config luaOnlyConf|defaultConf the optional config for the setup function +function SessionLens.setup(config, functions) + SessionLens.conf = vim.tbl_deep_extend("force", config.session_lens, SessionLens.conf) + SessionLens.conf.functions = functions + + Lib.setup(SessionLens.conf, functions) + Actions.setup(SessionLens.conf, functions) + logger.log_level = config.log_level end local themes = require "telescope.themes" -local actions = require "telescope.actions" +local telescope_actions = require "telescope.actions" ---Search session ---Triggers the customized telescope picker for switching sessions @@ -36,10 +47,10 @@ SessionLens.search_session = function(custom_opts) custom_opts = (vim.tbl_isempty(custom_opts or {}) or custom_opts == nil) and SessionLens.conf or custom_opts -- Use auto_session_root_dir from the Auto Session plugin - local cwd = AutoSession.conf.auto_session_root_dir + local cwd = SessionLens.conf.functions.get_root_dir() if custom_opts.shorten_path ~= nil then - Lib.logger.error "`shorten_path` config is deprecated, use the new `path_display` config instead" + logger.warn "`shorten_path` config is deprecated, use the new `path_display` config instead" if custom_opts.shorten_path then custom_opts.path_display = { "shorten" } else @@ -72,9 +83,9 @@ SessionLens.search_session = function(custom_opts) cwd = cwd, -- TOOD: support custom mappings? attach_mappings = function(_, map) - actions.select_default:replace(SessionLensActions.source_session) - map("i", "", SessionLensActions.delete_session) - map("i", "", SessionLensActions.alternate_session) + telescope_actions.select_default:replace(Actions.source_session) + map("i", "", Actions.delete_session) + map("i", "", Actions.alternate_session) return true end, } diff --git a/lua/auto-session/session-lens/library.lua b/lua/auto-session/session-lens/library.lua index 1dc27f3..bdaa5ed 100644 --- a/lua/auto-session/session-lens/library.lua +++ b/lua/auto-session/session-lens/library.lua @@ -1,5 +1,4 @@ local path = require "plenary.path" -local AutoSession = require "auto-session" local AutoSessionLib = require "auto-session.lib" local Config = {} @@ -12,12 +11,13 @@ local Lib = { Config = Config, _VIM_FALSE = 0, _VIM_TRUE = 1, - ROOT_DIR = AutoSession.conf.auto_session_root_dir, + ROOT_DIR = nil, } --- Setup ====================================================== -function Lib.setup(config) +function Lib.setup(config, functions) Lib.conf = vim.tbl_deep_extend("force", Lib.conf, config) + Lib.functions = functions + Lib.ROOT_DIR = Lib.functions.get_root_dir() end function Lib.isEmpty(s) @@ -33,11 +33,8 @@ function Lib.appendSlash(str) return str end --- =================================================================================== - --- ==================== SessionLens ========================== function Lib.make_entry.gen_from_file(opts) - local root = AutoSession.get_root_dir() + local root = Lib.functions.get_root_dir() return function(line) return { ordinal = line, @@ -59,26 +56,4 @@ function Lib.make_entry.gen_from_file(opts) end end --- =================================================================================== - --- Logger ========================================================= -function Lib.logger.debug(...) - if Lib.conf.logLevel == "debug" then - print(...) - end -end - -function Lib.logger.info(...) - local valid_values = { "info", "debug" } - if vim.tbl_contains(valid_values, Lib.conf.logLevel) then - print(...) - end -end - -function Lib.logger.error(...) - error(...) -end - --- ========================================================= - return Lib diff --git a/lua/session-lens/actions.lua b/lua/session-lens/actions.lua deleted file mode 100644 index ed66316..0000000 --- a/lua/session-lens/actions.lua +++ /dev/null @@ -1,161 +0,0 @@ -local AutoSession = require "auto-session" -local action_state = require "telescope.actions.state" -local actions = require "telescope.actions" -local Lib = require "auto-session.lib" - -local SessionLensActions = {} - --- TODO: Either use this or actually store the latest sessions on load time, then just alternate between them here. --- local function get_second_to_latest_session_by_last_edited() --- local dir = Lib.expand(AutoSession.conf.auto_session_root_dir) --- local sessions = {} - --- for _, filename in ipairs(vim.fn.readdir(dir)) do --- local session = AutoSession.conf.auto_session_root_dir .. filename --- local last_edited = vim.fn.getftime(session) - --- -- This is a naive way if checking if the session in question is an "extra" x.vim file. --- -- This check has a bug with non-extra session file names ending in x, e.g ajax.vim, where the session will never be considered a candidate for alternating. --- -- TODO: fix this naiveness somehow --- table.insert(sessions, { --- session = session, --- last_edited = last_edited, --- match = (function() --- for match in string.gmatch(session, "x.vim") do --- return match --- end --- end)(), --- }) - --- table.sort(sessions, function(a, b) --- return a.last_edited > b.last_edited --- end) --- end - --- local function find_non_extra_vim(start_count) --- if sessions[start_count].match then --- return find_non_extra_vim(start_count + 1) --- end - --- return sessions[start_count] --- end - --- local latest_and_second_latest = { --- latest = find_non_extra_vim(1).session, --- second_latest = find_non_extra_vim(2).session, --- } - --- -- print(vim.inspect(latest_and_second_latest)) --- -- print(vim.inspect(sessions)) - --- return latest_and_second_latest.second_latest --- end - --- local function get_second_to_latest_session() --- local filepath = AutoSession.conf.session_control.control_dir .. AutoSession.conf.session_control.control_filename - --- if vim.fn.filereadable(filepath) == 1 then --- local content = vim.fn.readfile(filepath) - --- local sessions = { --- this = Lib.expand(vim.v.this_session), --- alternate = Lib.expand(content[#content - 1]), --- } - --- Lib.logger.debug("get_second_to_latest_session", { sessions = sessions, content = content }) - --- if sessions.this ~= sessions.alternate then --- return sessions.alternate --- end --- end --- end - -local function get_alternate_session() - local filepath = AutoSession.conf.session_control.control_dir .. AutoSession.conf.session_control.control_filename - - if vim.fn.filereadable(filepath) == 1 then - local content = vim.fn.readfile(filepath)[1] or "{}" - - local json = vim.json.decode(content) - - local sessions = { - current = json.current, - alternate = json.alternate, - } - - Lib.logger.debug("get_alternate_session", { sessions = sessions, content = content }) - - if sessions.current ~= sessions.alternate then - return sessions.alternate - end - - Lib.logger.info "Current session is the same as alternate!" - end -end - -local function source_session(selection, prompt_bufnr) - if prompt_bufnr then - actions.close(prompt_bufnr) - end - - vim.defer_fn(function() - if -- type(AutoSession.conf.cwd_change_handling) == "table" - -- and not vim.tbl_isempty(AutoSession.conf.cwd_change_handling or {}) - -- and AutoSession.conf.cwd_change_handling.restore_upcoming_session - -- FIXME: Trying to check if cwd_change_handling properties are set, but something is wrong here. - false - then - -- Take advatage of cwd_change_handling behaviour for switching sessions - Lib.logger.debug "Triggering vim.fn.chdir since cwd_change_handling feature is enabled" - vim.fn.chdir(AutoSession.format_file_name(type(selection) == "table" and selection.filename or selection)) - else - Lib.logger.debug "Triggering session-lens behaviour since cwd_change_handling feature is disabled" - AutoSession.AutoSaveSession() - vim.cmd "%bd!" - vim.cmd "clearjumps" - AutoSession.RestoreSession(type(selection) == "table" and selection.path or selection) - end - end, 50) -end - ----Source session action ----Source a selected session after doing proper current session saving and cleanup ----@param prompt_bufnr number the telescope prompt bufnr -SessionLensActions.source_session = function(prompt_bufnr) - local selection = action_state.get_selected_entry() - source_session(selection, prompt_bufnr) -end - ----Delete session action ----Delete a selected session file ----@param prompt_bufnr number the telescope prompt bufnr -SessionLensActions.delete_session = function(prompt_bufnr) - local current_picker = action_state.get_current_picker(prompt_bufnr) - current_picker:delete_selection(function(selection) - AutoSession.DeleteSession(selection.path) - end) -end - -SessionLensActions.alternate_session = function(prompt_bufnr) - local alternate_session = get_alternate_session() - - if not alternate_session then - Lib.logger.info "There is no alternate session to navigate to, aborting operation" - - if prompt_bufnr then - actions.close(prompt_bufnr) - end - - return - end - - source_session(alternate_session, prompt_bufnr) -end - ---TODO: figure out the whole file placeholder parsing, expanding, escaping issue!! ----ex: ----"/Users/ronnieandrewmagatti/.local/share/nvim/sessions//%Users%ronnieandrewmagatti%Projects%dotfiles.vim", ----"/Users/ronnieandrewmagatti/.local/share/nvim/sessions/%Users%ronnieandrewmagatti%Projects%auto-session.vim" ----"/Users/ronnieandrewmagatti/.local/share/nvim/sessions/\\%Users\\%ronnieandrewmagatti\\%Projects\\%auto-session.vim" - -return SessionLensActions diff --git a/lua/session-lens/init.lua b/lua/session-lens/init.lua deleted file mode 100644 index c5cdd72..0000000 --- a/lua/session-lens/init.lua +++ /dev/null @@ -1,86 +0,0 @@ -local Lib = require "session-lens.library" -local AutoSession = require "auto-session" -local SessionLensActions = require "session-lens.actions" - ------------ Setup ---------- -local SessionLens = { - conf = {}, -} - ----@class DefaultConf ----@field theme_conf table telescope theme configuration ----@field previewer boolean|table telescope preview configs - ----@type DefaultConf -local defaultConf = { - theme_conf = { winblend = 10, border = true }, - previewer = false, -} - --- Set default config on plugin load -SessionLens.conf = defaultConf - ----Session lens setup function ----@param config DefaultConf the optional config for the setup function -function SessionLens.setup(config) - SessionLens.conf = vim.tbl_deep_extend("force", config, SessionLens.conf) -end - -local themes = require "telescope.themes" -local actions = require "telescope.actions" - ----Search session ----Triggers the customized telescope picker for switching sessions ----@param custom_opts any -SessionLens.search_session = function(custom_opts) - custom_opts = (vim.tbl_isempty(custom_opts or {}) or custom_opts == nil) and SessionLens.conf or custom_opts - - -- Use auto_session_root_dir from the Auto Session plugin - local cwd = AutoSession.conf.auto_session_root_dir - - if custom_opts.shorten_path ~= nil then - Lib.logger.error "`shorten_path` config is deprecated, use the new `path_display` config instead" - if custom_opts.shorten_path then - custom_opts.path_display = { "shorten" } - else - custom_opts.path_display = nil - end - - custom_opts.shorten_path = nil - end - - local theme_opts = themes.get_dropdown(custom_opts.theme_conf) - - -- -- Ignore last session dir on finder if feature is enabled - -- if AutoSession.conf.auto_session_enable_last_session then - -- if AutoSession.conf.auto_session_last_session_dir then - -- local last_session_dir = AutoSession.conf.auto_session_last_session_dir:gsub(cwd, "") - -- custom_opts["file_ignore_patterns"] = { last_session_dir } - -- end - -- end - - -- Use default previewer config by setting the value to nil if some sets previewer to true in the custom config. - -- Passing in the boolean value errors out in the telescope code with the picker trying to index a boolean instead of a table. - -- This fixes it but also allows for someone to pass in a table with the actual preview configs if they want to. - if custom_opts.previewer ~= false and custom_opts.previewer == true then - custom_opts["previewer"] = nil - end - - local opts = { - prompt_title = "Sessions", - entry_maker = Lib.make_entry.gen_from_file(custom_opts), - cwd = cwd, - -- TOOD: support custom mappings? - attach_mappings = function(_, map) - actions.select_default:replace(SessionLensActions.source_session) - map("i", "", SessionLensActions.delete_session) - map("i", "", SessionLensActions.alternate_session) - return true - end, - } - - local find_files_conf = vim.tbl_deep_extend("force", opts, theme_opts, custom_opts or {}) - require("telescope.builtin").find_files(find_files_conf) -end - -return SessionLens diff --git a/lua/session-lens/library.lua b/lua/session-lens/library.lua deleted file mode 100644 index 1dc27f3..0000000 --- a/lua/session-lens/library.lua +++ /dev/null @@ -1,84 +0,0 @@ -local path = require "plenary.path" -local AutoSession = require "auto-session" -local AutoSessionLib = require "auto-session.lib" - -local Config = {} -local Lib = { - make_entry = {}, - logger = {}, - conf = { - logLevel = false, - }, - Config = Config, - _VIM_FALSE = 0, - _VIM_TRUE = 1, - ROOT_DIR = AutoSession.conf.auto_session_root_dir, -} - --- Setup ====================================================== -function Lib.setup(config) - Lib.conf = vim.tbl_deep_extend("force", Lib.conf, config) -end - -function Lib.isEmpty(s) - return s == nil or s == "" -end - -function Lib.appendSlash(str) - if not Lib.isEmpty(str) then - if not vim.endswith(str, "/") then - str = str .. "/" - end - end - return str -end - --- =================================================================================== - --- ==================== SessionLens ========================== -function Lib.make_entry.gen_from_file(opts) - local root = AutoSession.get_root_dir() - return function(line) - return { - ordinal = line, - value = line, - filename = line, - cwd = root, - display = function(_) - local out = AutoSessionLib.unescape_dir(line):match "(.+)%.vim" - if opts.path_display and vim.tbl_contains(opts.path_display, "shorten") then - out = path:new(out):shorten() - end - if out then - return out - end - return line - end, - path = path:new(root, line):absolute(), - } - end -end - --- =================================================================================== - --- Logger ========================================================= -function Lib.logger.debug(...) - if Lib.conf.logLevel == "debug" then - print(...) - end -end - -function Lib.logger.info(...) - local valid_values = { "info", "debug" } - if vim.tbl_contains(valid_values, Lib.conf.logLevel) then - print(...) - end -end - -function Lib.logger.error(...) - error(...) -end - --- ========================================================= - -return Lib diff --git a/lua/telescope/_extensions/session-lens.lua b/lua/telescope/_extensions/session-lens.lua index 5a40487..cd78010 100644 --- a/lua/telescope/_extensions/session-lens.lua +++ b/lua/telescope/_extensions/session-lens.lua @@ -2,7 +2,9 @@ local telescope = require "telescope" local SessionLens = require "auto-session.session-lens" return telescope.register_extension { - setup = SessionLens.setup, + setup = function() + -- Nothing here for now + end, exports = { search_session = SessionLens.search_session, }, diff --git a/plugin/auto-session.vim b/plugin/auto-session.vim index e621034..445373a 100644 --- a/plugin/auto-session.vim +++ b/plugin/auto-session.vim @@ -1,46 +1,46 @@ -if exists('g:loaded_auto_session') | finish | endif " prevent loading file twice +" if exists('g:loaded_auto_session') | finish | endif " prevent loading file twice -let s:save_cpo = &cpo " save user coptions -set cpo&vim " reset them to defaults +" let s:save_cpo = &cpo " save user coptions +" set cpo&vim " reset them to defaults -let g:in_pager_mode = 0 +" let g:in_pager_mode = 0 -let LuaSaveSession = luaeval('require("auto-session").SaveSession') -let LuaRestoreSession = luaeval('require("auto-session").RestoreSession') -let LuaRestoreSessionFromFile = luaeval('require("auto-session").RestoreSessionFromFile') -let LuaDeleteSessionByName = luaeval('require("auto-session").DeleteSessionByName') -let LuaDisableAutoSave = luaeval('require("auto-session").DisableAutoSave') +" let LuaSaveSession = luaeval('require("auto-session").SaveSession') +" let LuaRestoreSession = luaeval('require("auto-session").RestoreSession') +" let LuaRestoreSessionFromFile = luaeval('require("auto-session").RestoreSessionFromFile') +" let LuaDeleteSessionByName = luaeval('require("auto-session").DeleteSessionByName') +" let LuaDisableAutoSave = luaeval('require("auto-session").DisableAutoSave') -let LuaAutoSaveSession = luaeval('require("auto-session").AutoSaveSession') -let LuaAutoRestoreSession = luaeval('require("auto-session").AutoRestoreSession') +" let LuaAutoSaveSession = luaeval('require("auto-session").AutoSaveSession') +" let LuaAutoRestoreSession = luaeval('require("auto-session").AutoRestoreSession') -function! CompleteSessions(A,L,P) abort - return luaeval('require"auto-session".CompleteSessions()') -endfunction +" function! CompleteSessions(A,L,P) abort +" return luaeval('require"auto-session".CompleteSessions()') +" endfunction -" Available commands -command! -nargs=* SaveSession call LuaSaveSession(expand('')) -command! -nargs=* RestoreSession call LuaRestoreSession(expand('')) -command! -nargs=1 -complete=custom,CompleteSessions RestoreSessionFromFile call LuaRestoreSessionFromFile(expand('')) -command! -nargs=* -complete=custom,CompleteSessions DeleteSession call LuaDeleteSessionByName() -command! -nargs=* DisableAutoSave call LuaDisableAutoSave() +" " Available commands +" command! -nargs=* SaveSession call LuaSaveSession(expand('')) +" command! -nargs=* RestoreSession call LuaRestoreSession(expand('')) +" command! -nargs=1 -complete=custom,CompleteSessions RestoreSessionFromFile call LuaRestoreSessionFromFile(expand('')) +" command! -nargs=* -complete=custom,CompleteSessions DeleteSession call LuaDeleteSessionByName() +" command! -nargs=* DisableAutoSave call LuaDisableAutoSave() -aug StdIn - autocmd! - autocmd StdinReadPre * let g:in_pager_mode = 1 -aug END +" aug StdIn +" autocmd! +" autocmd StdinReadPre * let g:in_pager_mode = 1 +" aug END -augroup autosession - autocmd! - autocmd VimEnter * nested call LuaAutoRestoreSession() - autocmd VimLeave * call LuaAutoSaveSession() +" augroup autosession +" autocmd! +" autocmd VimEnter * nested call LuaAutoRestoreSession() +" autocmd VimLeave * call LuaAutoSaveSession() - " TODO: Experiment with saving session on more than just VimEnter and VimLeave - " autocmd BufWinEnter * if g:in_pager_mode == 0 | call LuaAutoSaveSession() | endif - " autocmd BufWinLeave * if g:in_pager_mode == 0 | call LuaAutoSaveSession() | endif -augroup end +" " TODO: Experiment with saving session on more than just VimEnter and VimLeave +" " autocmd BufWinEnter * if g:in_pager_mode == 0 | call LuaAutoSaveSession() | endif +" " autocmd BufWinLeave * if g:in_pager_mode == 0 | call LuaAutoSaveSession() | endif +" augroup end -let &cpo = s:save_cpo " and restore after -unlet s:save_cpo +" let &cpo = s:save_cpo " and restore after +" unlet s:save_cpo -let g:loaded_auto_session = 1 +" let g:loaded_auto_session = 1