Skip to content

Commit 75a3589

Browse files
mous16lceWolf
authored andcommitted
fix: avoid ambiguous file and preset type resolution
1 parent 3beee51 commit 75a3589

1 file changed

Lines changed: 30 additions & 26 deletions

File tree

lua/cmake-tools/presets.lua

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ local function merge_table_list_by_key(dst, src, key)
1313
vim.list_extend(dst[key], src[key])
1414
end
1515

16+
local KNOWN_PRESET_KEYS = {
17+
configurePresets = true,
18+
buildPresets = true,
19+
testPresets = true,
20+
packagePresets = true,
21+
workflowPresets = true,
22+
}
23+
1624
-- Decodes a Cmake[User]Presets.json and its "includes", if any
1725
-- CMakeUserPresets.json implicitly includes CMakePresets.json if it exists
1826
local function decode(file, visited)
@@ -35,47 +43,43 @@ local function decode(file, visited)
3543
error(string.format("Could not parse %s", abs_file_path))
3644
end
3745
local includes = data.include or {}
38-
local includes_is_empty = #includes == 0
39-
local isUserPreset = string.find(abs_file_path:lower(), "user")
4046
local parentDir = vim.fs.dirname(abs_file_path)
4147

42-
if includes_is_empty and isUserPreset then
43-
local preset = "CMakePresets.json"
44-
local presetKebapCase = "cmake-presets.json"
45-
local presetPath = parentDir .. "/" .. preset
46-
local presetKebapCasePath = parentDir .. "/" .. presetKebapCase
48+
local filename_lower = vim.fn.fnamemodify(abs_file_path, ":t"):lower()
49+
local is_user_preset = filename_lower == "cmakeuserpresets.json"
50+
or filename_lower == "cmake-user-presets.json"
51+
52+
if #includes == 0 and is_user_preset then
53+
local preset_pascal_case = "CMakePresets.json"
54+
local preset_kebab_case = "cmake-presets.json"
55+
local preset_pascal_case_path = tostring(Path:new(parentDir) / preset_pascal_case)
56+
local preset_kebab_case_path = tostring(Path:new(parentDir) / preset_kebab_case)
4757

48-
if vim.fn.filereadable(presetPath) then
49-
includes[#includes + 1] = preset
50-
elseif vim.fn.filereadable(presetKebapCasePath) then
51-
includes[#includes + 1] = presetKebapCase
58+
if vim.fn.filereadable(preset_pascal_case_path) > 0 then
59+
includes[#includes + 1] = preset_pascal_case
60+
elseif vim.fn.filereadable(preset_kebab_case_path) > 0 then
61+
includes[#includes + 1] = preset_kebab_case
5262
end
5363
end
5464

5565
if #includes == 0 then
5666
return data
5767
end
5868

59-
for _, f in ipairs(includes) do
60-
local f_path_str
61-
local f_path = Path:new(f)
69+
for _, include_path in ipairs(includes) do
70+
local included_file_str
71+
local f_path = Path:new(include_path)
6272
if f_path:is_absolute() then
63-
f_path_str = f
73+
included_file_str = include_path
6474
else
65-
f_path_str = tostring(Path:new(parentDir) / f)
75+
included_file_str = tostring(Path:new(parentDir) / include_path)
6676
end
6777

68-
local fdata = decode(f_path_str, visited)
69-
local thisFilePresetKeys = vim.tbl_filter(function(key)
70-
if string.find(key, "Presets") then
71-
return true
72-
else
73-
return false
78+
local included_data = decode(included_file_str, visited)
79+
for key, _ in pairs(included_data) do
80+
if KNOWN_PRESET_KEYS[key] then
81+
merge_table_list_by_key(data, included_data, key)
7482
end
75-
end, vim.tbl_keys(fdata))
76-
77-
for _, eachPreset in ipairs(thisFilePresetKeys) do
78-
merge_table_list_by_key(data, fdata, eachPreset)
7983
end
8084
end
8185

0 commit comments

Comments
 (0)