Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lua/cmake-tools/build_preset.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ local osys = require("cmake-tools.osys")

local BuildPreset = {}

-- 'None' instance for when no build preset should be used.
BuildPreset.None = {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is not quite the effect you are trying to achieve. This is not an instance. It is something like a subclass within the BuildPreset class which re-uses the methods and fields of the BuildPreset class, yet some methods are overwritten/hidden (get_build_target and get_build_type)

See my comment on the createEmptyBuildPreset method on how to create individual instances of a None Preset

is_none = true,
get_build_target = function()
return ""
end,
get_build_type = function()
return nil
end,
}
setmetatable(BuildPreset.None, { __index = BuildPreset })

function BuildPreset:new(cwd, obj)
local instance = setmetatable(obj or {}, { __index = self })
instance.__index = self
Expand All @@ -18,7 +30,7 @@ function BuildPreset:get_build_target()
end
if type(self.targets) == "string" then
return self.targets
elseif type(self.targets == "table") then
elseif type(self.targets) == "table" then
return table.concat(self.targets, " ")
end
return ""
Expand Down
10 changes: 4 additions & 6 deletions lua/cmake-tools/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function cmake.generate(opt, callback)
-- try to determine the confiure preset based on the build preset
if config.build_preset then
local build_preset = presets:get_build_preset(config.build_preset)
if build_preset then
if build_preset and not build_preset.is_none then
local preset =
presets:get_configure_preset(build_preset.configurePreset, { include_hidden = true })
if preset then
Expand Down Expand Up @@ -387,7 +387,7 @@ function cmake.build(opt, callback)

local args

if presets_exists and config.build_preset then
if presets_exists and config.build_preset and not config.build_preset.is_none then
args = { "--build", "--preset", config.build_preset } -- preset don't need define build dir.
else
args = {
Expand Down Expand Up @@ -869,10 +869,8 @@ function cmake.select_build_preset(callback)
{ 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
callback(Result:new_error(Types.NOT_SELECT_PRESET, "No build preset selected"))
config.build_preset = Presets:createEmptyBuildPreset()
callback(Result:new(Types.SUCCESS, nil, nil))
return
end
if config.build_preset ~= choice then
Expand Down
4 changes: 4 additions & 0 deletions lua/cmake-tools/presets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,8 @@ function Presets.exists(cwd)
return Presets.find_preset_files(cwd) ~= nil
end

function Presets.createEmptyBuildPreset()
return BuildPreset.None
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not actually return a new instance. It returns the same table for each invocation. Modifications of that table would be shared across every "instance" you created that way. To properly create a new instance you should use
return setmetatable({}, <NonePreset-Table>)

end

return Presets