Skip to content

Commit 55ff61d

Browse files
DenzellceWolf
authored andcommitted
refactor(session): update session via deep extend
1 parent 06e68ef commit 55ff61d

1 file changed

Lines changed: 26 additions & 46 deletions

File tree

lua/cmake-tools/session.lua

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local session = {
99
},
1010
}
1111

12+
---@return string
1213
local function get_cache_path()
1314
if osys.islinux then
1415
return session.dir.unix
@@ -23,6 +24,8 @@ local function get_cache_path()
2324
end
2425
end
2526

27+
---@param cwd string neovim working directory
28+
---@return string
2629
local function get_current_path(cwd)
2730
local clean_path = cwd:gsub("/", "")
2831
clean_path = clean_path:gsub("\\", "")
@@ -37,6 +40,7 @@ local function init_cache()
3740
end
3841
end
3942

43+
---@param cwd string neovim working directory
4044
local function init_session(cwd)
4145
init_cache()
4246

@@ -49,6 +53,8 @@ local function init_session(cwd)
4953
end
5054
end
5155

56+
---@param cwd string neovim working directory (used as cache key)
57+
---@return SerializedConfig raw session data, or empty table if none exists
5258
function session.load(cwd)
5359
local path = get_current_path(cwd)
5460

@@ -60,64 +66,38 @@ function session.load(cwd)
6066
return {}
6167
end
6268

69+
---@param config Config
70+
---@param old_config SerializedConfig
71+
---@return Config merged config with session state applied
6372
function session.update(config, old_config)
64-
if next(old_config) ~= nil then
65-
if old_config.build_directory and old_config.base_settings.build_dir then
66-
config:update_build_dir(old_config.build_directory, old_config.base_settings.build_dir)
67-
end
68-
if old_config.build_type then
69-
config.build_type = old_config.build_type
70-
end
71-
if old_config.variant then
72-
config.variant = old_config.variant
73-
end
74-
if old_config.build_target then
75-
config.build_target = old_config.build_target
76-
if type(config.build_target) ~= "table" then -- Backwards compatibility (could be removed after a grace period) see PR!332
77-
config.build_target = { config.build_target }
78-
end
79-
end
80-
if old_config.launch_target then
81-
config.launch_target = old_config.launch_target
82-
end
83-
if old_config.kit then
84-
config.kit = old_config.kit
85-
end
86-
if old_config.configure_preset then
87-
config.configure_preset = old_config.configure_preset
88-
end
89-
if old_config.build_preset then
90-
config.build_preset = old_config.build_preset
91-
end
92-
if old_config.selected_test then
93-
config.selected_test = old_config.selected_test
94-
end
95-
if old_config.env_script then
96-
config.env_script = old_config.env_script
97-
end
98-
if old_config.cwd then
99-
config.cwd = old_config.cwd
100-
end
73+
if next(old_config) == nil then
74+
return config
75+
end
10176

102-
config.base_settings =
103-
vim.tbl_deep_extend("keep", old_config.base_settings, config.base_settings)
104-
config.target_settings = old_config.target_settings or {}
77+
local mt = getmetatable(config)
78+
local build_directory = old_config.build_directory
79+
local old_build_dir = old_config.base_settings and old_config.base_settings.build_dir
80+
old_config.build_directory = nil
10581

106-
-- migrate old launch args to new config
107-
if old_config.launch_args then
108-
for k, v in pairs(old_config.launch_args) do
109-
config.target_settings[k].args = v
110-
end
111-
end
82+
config = vim.tbl_deep_extend("force", config, old_config)
83+
setmetatable(config, mt)
84+
85+
if build_directory and old_build_dir then
86+
config:update_build_dir(build_directory, old_build_dir)
11287
end
88+
89+
return config
11390
end
11491

92+
---@param cwd string neovim working directory (used as cache key)
93+
---@param config Config current config to persist
11594
function session.save(cwd, config)
11695
init_session(cwd)
11796

11897
local path = get_current_path(cwd)
11998
local file = io.open(path, "w")
12099

100+
---@class SerializedConfig
121101
local serialized_object = {
122102
build_directory = config:build_directory_path(),
123103
build_type = config.build_type,

0 commit comments

Comments
 (0)