@@ -19,6 +19,7 @@ local Path = require("plenary.path")
1919local ctest = require (" cmake-tools.test.ctest" )
2020
2121local config = Config :new (const )
22+ local cwd = vim .loop .cwd ()
2223
2324local 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
15881590local regenerate_id = nil
15891591local termclose_id = nil
15901592local vim_leave_pre_id = nil
1593+ local cwd_changed_id = nil
15911594
15921595local group = vim .api .nvim_create_augroup (" cmaketools" , { clear = true })
15931596
@@ -1665,6 +1668,36 @@ function cmake.create_regenerate_on_save_autocmd()
16651668end
16661669
16671670function 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 })
0 commit comments