From 9c5fc98a4cb64f66b6f33cae9d31ee8cd54d43ed Mon Sep 17 00:00:00 2001 From: Oli Date: Fri, 3 May 2024 10:09:50 +0100 Subject: [PATCH] fix: #120 session loading for branches with slashes --- lua/persisted/utils.lua | 26 +++++++++++--------------- tests/default_settings_spec.lua | 9 +++++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua index de7de04..58dbb07 100644 --- a/lua/persisted/utils.lua +++ b/lua/persisted/utils.lua @@ -29,32 +29,28 @@ end ---Form a table of session data ---@param session string ----@return table +---@return table|nil function M.make_session_data(session) local config = require("persisted.config").options - local home - if os.getenv("HOME") then - home = os.getenv("HOME") -- Unix-based systems (Linux, macOS) - elseif os.getenv("USERPROFILE") then - home = os.getenv("USERPROFILE") -- Windows - else - home = "" + local home = os.getenv("HOME") or os.getenv("USERPROFILE") or "" + + -- Split the session string into path and branch parts + local separator_index = session:find(config.branch_separator) + if not separator_index then + return nil end - -- Form the branch - local pattern = config.branch_separator .. "(.-)%.vim" - local branch = session:match(pattern) or "" + local branch = session:sub(separator_index + 2):gsub("%.vim$", ""):gsub("%%", "/") - -- Form the name + -- Removing the home directory from the path and cleaning leading `/` local name = session:gsub(config.save_dir, ""):gsub("%%", "/"):gsub(home, "") - name = name:sub(1, #name - 4) -- Remove the .vim extension - + -- Remove the .vim extension + name = name:sub(1, #name - 4) if name:sub(1, 1) == "/" then name = name:sub(2) end - -- Form the dir_path local dir_path = name:gsub(branch, ""):gsub(config.branch_separator, ""):gsub(home, "") return { diff --git a/tests/default_settings_spec.lua b/tests/default_settings_spec.lua index c5b31b5..09c8fd1 100644 --- a/tests/default_settings_spec.lua +++ b/tests/default_settings_spec.lua @@ -66,3 +66,12 @@ async.describe("With default settings:", function() assert.equals("0", vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", "")) end) end) + +describe("Utilities", function() + it("can make derive the session name", function() + local session = "%home%username%projects%front@@user%fix-analytics-export-null-values.vim" + local data = require("persisted.utils").make_session_data(session) + + assert.equals("home/username/projects/front@@user/fix-analytics-export-null-values", data.name) + end) +end)