From 416c31970c2f72fb83621f55334062a3d37bbbf9 Mon Sep 17 00:00:00 2001 From: Denzel <> Date: Sat, 9 Aug 2025 16:12:37 +0200 Subject: [PATCH] refactor(build-preset): make 'None' option an actual preset (#333) This commit changes the handling of the None option of the build-preset selection to be an actual preset. A new `is_valid` function checks if the preset was created from the presets file or if it was added "by hand" for the option to have no (real) build-preset selected --- lua/cmake-tools/build_preset.lua | 8 ++++++++ lua/cmake-tools/init.lua | 24 +++++++++++------------- lua/cmake-tools/presets.lua | 2 ++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lua/cmake-tools/build_preset.lua b/lua/cmake-tools/build_preset.lua index 1b8bf9c2..c1571684 100644 --- a/lua/cmake-tools/build_preset.lua +++ b/lua/cmake-tools/build_preset.lua @@ -9,6 +9,10 @@ function BuildPreset:new(cwd, obj) instance.environment = instance.environment or {} instance.cwd = cwd + if instance.valid == nil then + instance.valid = true + end + return instance end @@ -31,4 +35,8 @@ function BuildPreset:get_build_type() return self.configuration end +function BuildPreset:is_valid() + return self.valid +end + return BuildPreset diff --git a/lua/cmake-tools/init.lua b/lua/cmake-tools/init.lua index 9888aa85..042d767b 100644 --- a/lua/cmake-tools/init.lua +++ b/lua/cmake-tools/init.lua @@ -856,11 +856,7 @@ function cmake.select_build_preset(callback) local presets = Presets:parse(config.cwd) local build_preset_names = presets:get_build_preset_names({ include_disabled = config:show_disabled_build_presets() }) - build_preset_names = vim.list_extend(build_preset_names, { "None" }) local format_preset_name = function(p_name) - if p_name == "None" then - return p_name - end local p = presets:get_build_preset(p_name) return p.displayName or p.name end @@ -868,19 +864,18 @@ function cmake.select_build_preset(callback) build_preset_names, { prompt = "Select cmake build presets", format_item = format_preset_name }, vim.schedule_wrap(function(choice) - if not choice or choice == "None" then - if choice == "None" then - config.build_preset = nil - end + if not choice then callback(Result:new_error(Types.NOT_SELECT_PRESET, "No build preset selected")) return end if config.build_preset ~= choice then - config.build_preset = choice - local build_preset = presets:get_build_preset(choice) - if build_preset then - config:update_build_target() + if build_preset:is_valid() then + config.build_preset = choice + + if build_preset then + config:update_build_target() + end end end local associated_configure_preset = presets:get_configure_preset( @@ -892,7 +887,10 @@ function cmake.select_build_preset(callback) or nil local configure_preset_updated = false - if config.configure_preset ~= associated_configure_preset_name then + if + associated_configure_preset_name + and config.configure_preset ~= associated_configure_preset_name + then config.configure_preset = associated_configure_preset_name configure_preset_updated = true end diff --git a/lua/cmake-tools/presets.lua b/lua/cmake-tools/presets.lua index 077701e1..ea528bc8 100644 --- a/lua/cmake-tools/presets.lua +++ b/lua/cmake-tools/presets.lua @@ -129,6 +129,8 @@ function Presets:parse(cwd) build_preset = createBuildPreset(build_preset) end + table.insert(instance.buildPresets, createBuildPreset({ name = "None", valid = false })) + return instance end