Skip to content

Commit fc97975

Browse files
committed
fix: avoid ambiguous file and preset type resolution
1 parent 350e088 commit fc97975

1 file changed

Lines changed: 29 additions & 26 deletions

File tree

lua/cmake-tools/presets.lua

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,47 +36,50 @@ local function decode(file, visited)
3636
end
3737
data.include = data.include or {}
3838
local includes = data.include
39-
local includes_is_empty = #includes == 0
40-
local isUserPreset = string.find(abs_file_path:lower(), "user")
4139
local parentDir = vim.fs.dirname(abs_file_path)
4240

43-
if includes_is_empty and isUserPreset then
44-
local preset = "CMakePresets.json"
45-
local presetKebapCase = "cmake-presets.json"
46-
local presetPath = parentDir .. "/" .. preset
47-
local presetKebapCasePath = parentDir .. "/" .. presetKebapCase
41+
local filename_lower = vim.fn.fnamemodify(abs_file_path, ":t"):lower()
42+
local is_user_preset = filename_lower == "cmakeuserpresets.json" or filename_lower == "cmake-user-presets.json"
4843

49-
if vim.fn.filereadable(presetPath) then
50-
includes[#includes + 1] = preset
51-
elseif vim.fn.filereadable(presetKebapCasePath) then
52-
includes[#includes + 1] = presetKebapCase
44+
if #includes == 0 and is_user_preset then
45+
local preset_pascal_case = "CMakePresets.json"
46+
local preset_kebab_case = "cmake-presets.json"
47+
local preset_pascal_case_path = tostring(Path:new(parentDir) / preset_pascal_case)
48+
local preset_kebab_case_path = tostring(Path:new(parentDir) / preset_kebab_case)
49+
50+
if vim.fn.filereadable(preset_pascal_case_path) > 0 then
51+
includes[#includes + 1] = preset_pascal_case
52+
elseif vim.fn.filereadable(preset_kebab_case_path) > 0 then
53+
includes[#includes + 1] = preset_kebab_case
5354
end
5455
end
5556

5657
if #includes == 0 then
5758
return data
5859
end
5960

60-
for _, f in ipairs(includes) do
61-
local f_path_str
62-
local f_path = Path:new(f)
61+
local KNOWN_PRESET_KEYS = {
62+
configurePresets = true,
63+
buildPresets = true,
64+
testPresets = true,
65+
packagePresets = true,
66+
workflowPresets = true,
67+
}
68+
69+
for _, include_path in ipairs(includes) do
70+
local included_file_str
71+
local f_path = Path:new(include_path)
6372
if f_path:is_absolute() then
64-
f_path_str = f
73+
included_file_str = include_path
6574
else
66-
f_path_str = tostring(Path:new(parentDir) / f)
75+
included_file_str = tostring(Path:new(parentDir) / include_path)
6776
end
6877

69-
local fdata = decode(f_path_str, visited)
70-
local thisFilePresetKeys = vim.tbl_filter(function(key)
71-
if string.find(key, "Presets") then
72-
return true
73-
else
74-
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)
7582
end
76-
end, vim.tbl_keys(fdata))
77-
78-
for _, eachPreset in ipairs(thisFilePresetKeys) do
79-
merge_table_list_by_key(data, fdata, eachPreset)
8083
end
8184
end
8285

0 commit comments

Comments
 (0)