Skip to content

Commit 2858a15

Browse files
committed
fix: vim.fn.getcwd actually takes a second argument at -1 as well
- Update `vim.fn.getcwd` implementation with additional argument - Implement and test `tcd` and `lcd` commands in `get_cwd_spec.lua` (thanks @cameronr)
1 parent 2349b46 commit 2858a15

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

lua/auto-session/init.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ local function suppress_session(session_dir)
173173

174174
-- If session_dir is set, use that otherwise use cwd
175175
-- session_dir will be set when loading a session from a directory at lauch (i.e. from argv)
176-
local cwd = session_dir or vim.fn.getcwd(-1)
176+
local cwd = session_dir or vim.fn.getcwd(-1, -1)
177177

178178
if Lib.find_matching_directory(cwd, dirs) then
179179
Lib.logger.debug "suppress_session found a match, suppressing"
@@ -190,7 +190,7 @@ local function is_allowed_dir()
190190
end
191191

192192
local dirs = Config.allowed_dirs or {}
193-
local cwd = vim.fn.getcwd(-1)
193+
local cwd = vim.fn.getcwd(-1, -1)
194194

195195
if Lib.find_matching_directory(cwd, dirs) then
196196
Lib.logger.debug "is_allowed_dir found a match, allowing"
@@ -209,7 +209,7 @@ end
209209
---@return string Returns the escaped version of the name with .vim appended.
210210
local function get_session_file_name(session_name, legacy)
211211
if not session_name or session_name == "" then
212-
session_name = vim.fn.getcwd(-1)
212+
session_name = vim.fn.getcwd(-1, -1)
213213
Lib.logger.debug("get_session_file_name no session_name, using cwd: " .. session_name)
214214

215215
local git_branch_name = get_git_branch_name()
@@ -279,13 +279,13 @@ end
279279
---unless a session for the current working directory exists.
280280
---@return boolean True if a session exists for the cwd
281281
function AutoSession.session_exists_for_cwd()
282-
local session_file = get_session_file_name(vim.fn.getcwd(-1))
282+
local session_file = get_session_file_name(vim.fn.getcwd(-1, -1))
283283
if vim.fn.filereadable(AutoSession.get_root_dir() .. session_file) ~= 0 then
284284
return true
285285
end
286286

287287
-- Check legacy sessions
288-
session_file = get_session_file_name(vim.fn.getcwd(-1), true)
288+
session_file = get_session_file_name(vim.fn.getcwd(-1, -1), true)
289289
return vim.fn.filereadable(AutoSession.get_root_dir() .. session_file) ~= 0
290290
end
291291

@@ -482,7 +482,7 @@ function AutoSession.auto_restore_session_at_vim_enter()
482482

483483
-- We failed to load a session for the other directory. Unless session name matches cwd, we don't
484484
-- want to enable autosaving since it might replace the session for the cwd
485-
if vim.fn.getcwd(-1) ~= session_name then
485+
if vim.fn.getcwd(-1, -1) ~= session_name then
486486
Lib.logger.debug "Not enabling autosave because launch argument didn't load session and doesn't match cwd"
487487
Config.auto_save = false
488488
end

tests/get_cwd_spec.lua

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---@diagnostic disable: undefined-field
2+
local TL = require "tests/test_lib"
3+
4+
describe("cwd lookup", function()
5+
local as = require "auto-session"
6+
7+
require("auto-session").setup {}
8+
9+
TL.clearSessionFilesAndBuffers()
10+
11+
it("works when tcd is used", function()
12+
assert.equals(0, vim.fn.filereadable(TL.default_session_path))
13+
vim.cmd("e " .. TL.test_file)
14+
vim.cmd "tabnew"
15+
vim.cmd "tcd tests"
16+
17+
as.SaveSession()
18+
19+
vim.cmd "tabclose"
20+
21+
-- Make sure the session was still created for the global directory
22+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
23+
end)
24+
25+
TL.clearSessionFilesAndBuffers()
26+
27+
it("works when lcd is used", function()
28+
assert.equals(0, vim.fn.filereadable(TL.default_session_path))
29+
vim.cmd("e " .. TL.test_file)
30+
vim.cmd "tabnew"
31+
vim.cmd "lcd tests"
32+
33+
as.SaveSession()
34+
35+
vim.cmd "tabclose"
36+
37+
-- Make sure the session was still created for the global directory
38+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
39+
end)
40+
end)

0 commit comments

Comments
 (0)