Skip to content

Commit 06e68ef

Browse files
DenzellceWolf
authored andcommitted
feat(session): save and load sesion on DirChanged
1 parent 6973167 commit 06e68ef

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

lua/cmake-tools/init.lua

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local Path = require("plenary.path")
1919
local ctest = require("cmake-tools.test.ctest")
2020

2121
local config = Config:new(const)
22+
local cwd = vim.loop.cwd()
2223

2324
local cmake = {}
2425

@@ -39,10 +40,11 @@ function cmake.setup(values)
3940
require("cmake-tools.notification").setup(const.cmake_notifications)
4041

4142
config = Config:new(const)
43+
cwd = vim.loop.cwd()
4244

4345
-- auto reload previous session
44-
local old_config = _session.load()
45-
_session.update(config, old_config)
46+
local old_config = _session.load(cwd)
47+
config = _session.update(config, old_config)
4648

4749
local is_executor_installed = utils.get_executor(config.executor.name).is_installed()
4850
local is_runner_installed = utils.get_runner(config.runner.name).is_installed()
@@ -1588,6 +1590,7 @@ end
15881590
local regenerate_id = nil
15891591
local termclose_id = nil
15901592
local vim_leave_pre_id = nil
1593+
local cwd_changed_id = nil
15911594

15921595
local group = vim.api.nvim_create_augroup("cmaketools", { clear = true })
15931596

@@ -1665,6 +1668,36 @@ function cmake.create_regenerate_on_save_autocmd()
16651668
end
16661669

16671670
function cmake.register_autocmd()
1671+
if cwd_changed_id then
1672+
vim.api.nvim_del_autocmd(cwd_changed_id)
1673+
end
1674+
cwd_changed_id = vim.api.nvim_create_autocmd("DirChanged", {
1675+
group = group,
1676+
callback = function()
1677+
local current_cwd = vim.loop.cwd()
1678+
if current_cwd == cwd then
1679+
return
1680+
end
1681+
1682+
-- Save session for the project we're leaving
1683+
_session.save(cwd, config)
1684+
cwd = current_cwd
1685+
config = Config:new(const)
1686+
1687+
if not cmake.is_cmake_project() then
1688+
return
1689+
end
1690+
1691+
-- Load and apply session for the new cwd
1692+
config = _session.update(config, _session.load(cwd))
1693+
1694+
-- Re-register cwd-dependent autocmds for the new project
1695+
cmake.create_regenerate_on_save_autocmd()
1696+
cmake.register_autocmd_provided_by_users()
1697+
cmake.register_scratch_buffer(config.executor.name, config.runner.name)
1698+
end,
1699+
})
1700+
16681701
-- preload the autocmd if the following option is true. only saves cmakelists.txt files
16691702
if cmake.is_cmake_project() then
16701703
if termclose_id then
@@ -1679,7 +1712,7 @@ function cmake.register_autocmd()
16791712
vim_leave_pre_id = vim.api.nvim_create_autocmd("VimLeavePre", {
16801713
group = group,
16811714
callback = function()
1682-
_session.save(config)
1715+
_session.save(cwd, config)
16831716
vim.api.nvim_del_augroup_by_id(group)
16841717
end,
16851718
})

lua/cmake-tools/session.lua

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ local function get_cache_path()
2323
end
2424
end
2525

26-
local function get_current_path()
27-
local current_path = vim.loop.cwd()
28-
local clean_path = current_path:gsub("/", "")
26+
local function get_current_path(cwd)
27+
local clean_path = cwd:gsub("/", "")
2928
clean_path = clean_path:gsub("\\", "")
3029
clean_path = clean_path:gsub(":", "")
3130
return get_cache_path() .. clean_path .. ".lua"
@@ -38,10 +37,10 @@ local function init_cache()
3837
end
3938
end
4039

41-
local function init_session()
40+
local function init_session(cwd)
4241
init_cache()
4342

44-
local path = get_current_path()
43+
local path = get_current_path(cwd)
4544
if not utils.file_exists(path) then
4645
local file = io.open(path, "w")
4746
if file then
@@ -50,8 +49,8 @@ local function init_session()
5049
end
5150
end
5251

53-
function session.load()
54-
local path = get_current_path()
52+
function session.load(cwd)
53+
local path = get_current_path(cwd)
5554

5655
if utils.file_exists(path) then
5756
local config = dofile(path)
@@ -113,10 +112,10 @@ function session.update(config, old_config)
113112
end
114113
end
115114

116-
function session.save(config)
117-
init_session()
115+
function session.save(cwd, config)
116+
init_session(cwd)
118117

119-
local path = get_current_path()
118+
local path = get_current_path(cwd)
120119
local file = io.open(path, "w")
121120

122121
local serialized_object = {

0 commit comments

Comments
 (0)