Skip to content

Commit

Permalink
fix: #120 session loading for branches with slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed May 3, 2024
1 parent 96bf597 commit 9c5fc98
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lua/persisted/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions tests/default_settings_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 9c5fc98

Please sign in to comment.