diff --git a/README.md b/README.md index 682d666..6ce1237 100644 --- a/README.md +++ b/README.md @@ -248,7 +248,7 @@ require("persisted").setup({ Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`. -> **Note**: Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not not result in autoloading but `nvim some/existing/path` or `nvim .` will. +> **Note**: Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: `nvim some_file.rb` will not result in autoloading but `nvim some/existing/path` or `nvim .` will. ### Following current working directory diff --git a/doc/persisted.nvim.txt b/doc/persisted.nvim.txt index 728a496..f5b9b03 100644 --- a/doc/persisted.nvim.txt +++ b/doc/persisted.nvim.txt @@ -60,7 +60,7 @@ Install the plugin with your preferred package manager: >vim " Vim Script Plug 'olimorris/persisted.nvim' - + lua << EOF require("persisted").setup { -- your configuration comes here @@ -270,7 +270,7 @@ Autoloading can be further controlled for certain directories by specifying **Note**Autoloading will not occur if the plugin is lazy loaded or a user opens Neovim with arguments other than a single directory argument. For example: - `nvim some_file.rb` will not not result in autoloading but `nvim + `nvim some_file.rb` will not result in autoloading but `nvim some/existing/path` or `nvim .` will. FOLLOWING CURRENT WORKING DIRECTORY ~ @@ -379,14 +379,14 @@ session, saving the current session before clearing all of the open buffers: >lua local group = vim.api.nvim_create_augroup("PersistedHooks", {}) - + vim.api.nvim_create_autocmd({ "User" }, { pattern = "PersistedTelescopeLoadPre", group = group, callback = function(session) -- Save the currently loaded session using a global variable require("persisted").save({ session = vim.g.persisted_loaded_session }) - + -- Delete all of the open buffers vim.api.nvim_input(":%bd!") end, diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 9837bf1..6dbdb50 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -19,32 +19,38 @@ local function escape_pattern(str, pattern, replace, n) return string.gsub(str, pattern, replace, n) end +---Gets the directory from the file/path argument passed to Neovim if there's +---exactly one and it resolves to a valid directory +---@return string|nil +local function args_path() + if vim.fn.argc() ~= 1 then + return nil + end + + -- Use expand() to resolve '~' and use fs_realpath to resolve both '.' and + -- relative paths passed as arguments. Note that argv() will only ever return + -- paths/files passed as arguments and does not include other + -- parameters/arguments. fs_realpath() returns nil if the path doesn't exist. + -- Use isdirectory to validate it's a directory and not a file. + local dir = vim.loop.fs_realpath(vim.fn.expand(vim.fn.argv(0))) + if dir ~= nil and vim.fn.isdirectory(dir) ~= 0 then + return dir + end + return nil +end + ---Check any arguments passed to Neovim and verify if they're a directory ---@return boolean local function args_check() - if vim.fn.argc() == 1 then - return vim.fn.isdirectory(vim.fn.argv(0)) ~= 0 - end - - return vim.fn.argc() == 0 + -- Args are valid if a single directory was resolved or if no args were used. + return args_path() ~= nil or vim.fn.argc() == 0 end ---Get the directory to be used for the session ---@return string local function session_dir() - if vim.fn.argc() == 1 then - local dir = vim.fn.expand(vim.fn.argv(0)) - - if dir == "." then - return vim.fn.getcwd() - end - - if vim.fn.isdirectory(dir) ~= 0 then - return dir - end - end - - return vim.fn.getcwd() + -- Use specified directory from arguments or the working directory otherwise. + return args_path() or vim.fn.getcwd() end ---Does the current working directory allow for the auto-saving and loading? @@ -92,12 +98,12 @@ function M.get_branch(dir) dir = dir or session_dir() if config.options.use_git_branch then - vim.fn.system("git -C " .. dir .. " rev-parse 2>/dev/null") + vim.fn.system("git -C \"" .. dir .. "\" rev-parse 2>/dev/null") local git_enabled = (vim.v.shell_error == 0) if git_enabled then - local git_branch = vim.fn.systemlist("git -C " .. dir .. " rev-parse --abbrev-ref HEAD 2>/dev/null") + local git_branch = vim.fn.systemlist("git -C \"" .. dir .. "\" rev-parse --abbrev-ref HEAD 2>/dev/null") if vim.v.shell_error == 0 then local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")