1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
local pr = require("cmake.project")
local config = require("cmake.config")
local t = require("cmake.terminal")
local utils = require("cmake.utils")
local constants = require("cmake.constants")
local Path = require("plenary.path")
local uv = vim.uv
local M = {}
local default_generate_exe_opts = {
notify = {
ok_message = "CMake generate finished",
err_message = function(code)
return "CMake generate failed with code " .. tostring(code)
end,
},
}
local default_build_exe_opts = {
notify = {
ok_message = "CMake build finished",
err_message = function(code)
return "CMake build failed with code " .. tostring(code)
end,
},
}
local _explain = function(command)
vim.notify(
table.concat({
table.concat(
vim
.iter(command.env or {})
:map(function(k, v)
if v:find(" ") then
return k .. '="' .. v .. '"'
end
return k .. "=" .. v
end)
:totable(),
" "
),
command.cmd,
command.args,
}, " "),
vim.log.levels.INFO
)
end
--- Extends generate command by given options
---@param command table
---@param opts GenerateOpts
---@return table
local _extend_generate_command = function(command, opts)
opts = opts or {}
local new = vim.deepcopy(command)
return new
end
--- Extends build command by given options
---@param command table
---@param opts BuildOpts
---@return table
local _extend_build_command = function(command, opts)
local new = vim.deepcopy(command)
if opts.j then
new.args = new.args .. " -j " .. tostring(opts.j)
end
if opts.clean then
new.args = new.args .. " --clean-first"
end
if opts.target and #opts.target ~= 0 then
new.args = new.args .. " --target " .. table.concat(opts.target, " ")
end
return new
end
local _generate = function(option, opts)
opts = opts or {}
local main_path = function()
pr.create_fileapi_query({}, function()
vim.schedule(function()
t.cmake_execute(_extend_generate_command(option.generate_command, opts), default_generate_exe_opts)
end)
end)
end
if opts.fresh then
pr.clear_cache()
end
main_path()
end
local _for_current_generate_option = function(func)
local idx = pr.current_generate_option_idx()
if not idx then
vim.notify("CMake: no configuration to generate", vim.log.levels.WARN)
else
func(pr.current_generate_option())
end
end
---@class GenerateOpts
---@field fresh boolean|nil
--- Generate project with current generate option
--- @param opts GenerateOpts
M.generate = function(opts)
opts = opts or {}
_for_current_generate_option(function(option)
_generate(option, opts)
end)
end
--- Generate project with current generate option
--- @param opts GenerateOpts
M.generate_explain = function(opts)
opts = opts or {}
_for_current_generate_option(function(option)
_explain(_extend_generate_command(option.generate_command, opts))
end)
end
--- Generate project with current generate option
--- @param opts table|nil
M.generate_select = function(opts)
opts = opts or {}
local items = pr.generate_options(opts)
vim.ui.select(items, {
prompt = "Select configuration to generate:",
format_item = function(item)
return table.concat(item.name, config.variants_display.short.sep)
end,
}, function(_, idx)
if not idx then
return
end
pr.set_current_generate_option(idx)
end)
end
local _for_current_build_option = function(func)
local idx = pr.current_build_option()
if not idx then
vim.notify("CMake: no build configuration to generate", vim.log.levels.WARN)
else
func(pr.current_build_option())
end
end
local _build = function(option, opts)
opts = opts or {}
pr.create_fileapi_query({}, function()
vim.schedule(function()
t.cmake_execute(_extend_build_command(option.command, opts), default_build_exe_opts)
end)
end)
end
---@class BuildOpts
---@field clean boolean|nil
---@field j number|nil
---@field target string[]|nil
--- Build project with current build option
--- @param opts BuildOpts
M.build = function(opts)
opts = opts or {}
_for_current_build_option(function(option)
_build(option, opts)
end)
end
--- Build project with current build option
--- @param opts BuildOpts
M.build_explain = function(opts)
opts = opts or {}
_for_current_build_option(function(option)
_explain(_extend_build_command(option.command, opts))
end)
end
---Change current build option
---@param opts any|nil
M.build_select = function(opts)
local items = pr.current_generate_option().build_options
vim.ui.select(items, {
prompt = "Select build option to generate:",
format_item = function(item)
return table.concat(item.name, config.variants_display.short.sep)
end,
}, function(_, idx)
if not idx then
return
end
pr.set_current_build_option(idx)
end)
end
local _run_target = function(opts)
local command = {
cmd = opts.path,
cwd = pr.current_directory(),
}
t.target_execute(command)
end
---@class RunTargetOpts
---@field explain boolean|nil
--- Run target
--- @param opts RunTargetOpts
M.run_target = function(opts)
opts = opts or {}
local _curr_exe_cmd = pr.current_executable_target()
if not _curr_exe_cmd then
M.run_target_select(opts)
else
_run_target({ path = _curr_exe_cmd.path })
end
end
--- Select target to run
M.run_target_select = function(opts)
opts = opts or {}
opts.type = "EXECUTABLE"
local items = pr.current_targets(opts)
vim.ui.select(items, {
prompt = "Select tagret to run:",
format_item = function(item)
return item.name
end,
}, function(_, idx)
if not idx then
return
end
pr.set_current_executable_target(idx)
end)
end
---Toggle CMake terminal window
M.toggle = function()
t.cmake_toggle()
end
---Edit `.cmake-variants.yaml` file
M.edit_variants = function()
utils.file_exists(constants.variants_yaml_filename, function(variants_exists)
if variants_exists then
vim.schedule(function()
vim.cmd(string.format("e %s", constants.variants_yaml_filename))
end)
else
local default_yaml = require("cmake.lyaml").dump(config.variants)
utils.write_file(constants.variants_yaml_filename, default_yaml, function()
vim.schedule(function()
vim.cmd(string.format("e %s", constants.variants_yaml_filename))
end)
end)
end
end)
end
M.reset_project = function(opts)
require("cmake.project").setup(opts)
end
return M
|