Skip to content

Commit 8a5a7ee

Browse files
DenzellceWolf
authored andcommitted
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
1 parent 447fe76 commit 8a5a7ee

3 files changed

Lines changed: 21 additions & 13 deletions

File tree

lua/cmake-tools/build_preset.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ function BuildPreset:new(cwd, obj)
99
instance.environment = instance.environment or {}
1010
instance.cwd = cwd
1111

12+
if instance.valid == nil then
13+
instance.valid = true
14+
end
15+
1216
return instance
1317
end
1418

@@ -31,4 +35,8 @@ function BuildPreset:get_build_type()
3135
return self.configuration
3236
end
3337

38+
function BuildPreset:is_valid()
39+
return self.valid
40+
end
41+
3442
return BuildPreset

lua/cmake-tools/init.lua

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -856,31 +856,26 @@ function cmake.select_build_preset(callback)
856856
local presets = Presets:parse(config.cwd)
857857
local build_preset_names =
858858
presets:get_build_preset_names({ include_disabled = config:show_disabled_build_presets() })
859-
build_preset_names = vim.list_extend(build_preset_names, { "None" })
860859
local format_preset_name = function(p_name)
861-
if p_name == "None" then
862-
return p_name
863-
end
864860
local p = presets:get_build_preset(p_name)
865861
return p.displayName or p.name
866862
end
867863
vim.ui.select(
868864
build_preset_names,
869865
{ prompt = "Select cmake build presets", format_item = format_preset_name },
870866
vim.schedule_wrap(function(choice)
871-
if not choice or choice == "None" then
872-
if choice == "None" then
873-
config.build_preset = nil
874-
end
867+
if not choice then
875868
callback(Result:new_error(Types.NOT_SELECT_PRESET, "No build preset selected"))
876869
return
877870
end
878871
if config.build_preset ~= choice then
879-
config.build_preset = choice
880-
881872
local build_preset = presets:get_build_preset(choice)
882-
if build_preset then
883-
config:update_build_target()
873+
if build_preset:is_valid() then
874+
config.build_preset = choice
875+
876+
if build_preset then
877+
config:update_build_target()
878+
end
884879
end
885880
end
886881
local associated_configure_preset = presets:get_configure_preset(
@@ -892,7 +887,10 @@ function cmake.select_build_preset(callback)
892887
or nil
893888
local configure_preset_updated = false
894889

895-
if config.configure_preset ~= associated_configure_preset_name then
890+
if
891+
associated_configure_preset_name
892+
and config.configure_preset ~= associated_configure_preset_name
893+
then
896894
config.configure_preset = associated_configure_preset_name
897895
configure_preset_updated = true
898896
end

lua/cmake-tools/presets.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ function Presets:parse(cwd)
129129
build_preset = createBuildPreset(build_preset)
130130
end
131131

132+
table.insert(instance.buildPresets, createBuildPreset({ name = "None", valid = false }))
133+
132134
return instance
133135
end
134136

0 commit comments

Comments
 (0)