@@ -7,36 +7,57 @@ local variants = require("cmake-tools.variants")
77local Presets = require (" cmake-tools.presets" )
88local kits = require (" cmake-tools.kits" )
99
10- local Config = {
11- build_directory = nil ,
12- query_directory = nil ,
13- reply_directory = nil ,
14- build_type = nil ,
15- variant = nil ,
16- build_target = nil ,
17- launch_target = nil ,
18- kit = nil ,
19- configure_preset = nil ,
20- build_preset = nil ,
21- base_settings = {
22- env = {},
23- build_dir = " " ,
24- working_dir = " ${dir.binary}" ,
25- use_preset = true ,
26- generate_options = {},
27- build_options = {},
28- show_disabled_build_presets = true ,
29- }, -- general config
30- target_settings = {}, -- target specific config
31- executor = nil ,
32- runner = nil ,
33- env_script = " " ,
34- cwd = vim .loop .cwd (),
35- }
36-
10+ --- @class Config.BaseSettings
11+ --- @field env table
12+ --- @field build_dir string
13+ --- @field working_dir string
14+ --- @field use_preset boolean
15+ --- @field generate_options string[]
16+ --- @field build_options string[]
17+ --- @field show_disabled_build_presets boolean
18+ --- @field ctest_show_labels boolean
19+
20+ --- @class Config
21+ --- @field build_directory Path ?
22+ --- @field query_directory Path ?
23+ --- @field reply_directory Path ?
24+ --- @field build_type string ?
25+ --- @field variant table ?
26+ --- @field build_target string | string[] | nil
27+ --- @field launch_target string ?
28+ --- @field kit string ?
29+ --- @field configure_preset string ?
30+ --- @field build_preset string ?
31+ --- @field test_preset string ?
32+ --- @field selected_test string ?
33+ --- @field base_settings Config.BaseSettings
34+ --- @field target_settings table
35+ --- @field executor table ?
36+ --- @field runner table ?
37+ --- @field env_script string
38+ --- @field cwd string
39+ local Config = {}
40+ Config .__index = Config
41+
42+ --- @param const Const
43+ --- @return Config
3744function Config :new (const )
38- local obj = {}
39- setmetatable (obj , { __index = self }) -- when obj cannot find key in its table, it will try to find it from its __index value
45+ local obj = {
46+ base_settings = {
47+ env = {},
48+ build_dir = " " ,
49+ working_dir = " ${dir.binary}" ,
50+ use_preset = true ,
51+ generate_options = {},
52+ build_options = {},
53+ show_disabled_build_presets = true ,
54+ ctest_show_labels = false ,
55+ }, -- general config
56+ target_settings = {}, -- target specific config
57+ env_script = " " ,
58+ cwd = vim .loop .cwd (),
59+ }
60+ setmetatable (obj , Config )
4061
4162 obj :update_build_dir (const .cmake_build_directory , const .cmake_build_directory )
4263
@@ -45,28 +66,30 @@ function Config:new(const)
4566 obj .base_settings .use_preset = const .cmake_use_preset
4667
4768 obj .base_settings .show_disabled_build_presets = const .cmake_show_disabled_build_presets
69+ obj .base_settings .ctest_show_labels = const .ctest_show_labels
4870
4971 obj .executor = const .cmake_executor
5072 obj .runner = const .cmake_runner
5173
5274 return obj
5375end
5476
77+ --- @return string
5578function Config :build_directory_path ()
5679 return self .build_directory .filename
5780end
5881
82+ --- @return boolean
5983function Config :has_build_directory ()
6084 return self .build_directory and self .build_directory :exists ()
6185end
6286
63- --- comment
6487--- The reason for storing no expand build directory is to make cwd selecting easier
88+ --- @return string
6589function Config :no_expand_build_directory_path ()
6690 return self .base_settings .build_dir
6791end
6892
69- --- comment
7093--- @param build_dir string | function string or a function returning string containing path to the build dir
7194--- @param no_expand_build_dir string | function
7295function Config :update_build_dir (build_dir , no_expand_build_dir )
@@ -103,8 +126,9 @@ function Config:update_build_dir(build_dir, no_expand_build_dir)
103126 self .base_settings .build_dir = Path :new (no_expand_build_dir ):absolute ()
104127end
105128
106- --- Prepare build directory. Which allows macro expansion.
129+ --- Prepare build directory with macro expansion
107130--- @param kit_list table all the kits
131+ --- @return string
108132function Config :prepare_build_directory (kit_list )
109133 -- macro expansion:
110134 -- ${kit}
@@ -136,18 +160,27 @@ function Config:prepare_build_directory(kit_list)
136160 return build_dir
137161end
138162
163+ --- @return string[]
139164function Config :generate_options ()
140165 return self .base_settings .generate_options and self .base_settings .generate_options or {}
141166end
142167
168+ --- @return string[]
143169function Config :build_options ()
144170 return self .base_settings .build_options and self .base_settings .build_options or {}
145171end
146172
173+ --- @return boolean
147174function Config :show_disabled_build_presets ()
148175 return self .base_settings .show_disabled_build_presets
149176end
150177
178+ --- @return boolean
179+ function Config :ctest_show_labels ()
180+ return self .base_settings .ctest_show_labels
181+ end
182+
183+ --- @return cmake.Result
151184function Config :generate_build_directory ()
152185 local build_directory = Path :new (self .build_directory )
153186
@@ -157,6 +190,7 @@ function Config:generate_build_directory()
157190 return self :generate_query_files ()
158191end
159192
193+ --- @return cmake.Result
160194function Config :generate_query_files ()
161195 local query_directory = Path :new (self .query_directory )
162196 if not query_directory :mkdir ({ parents = true }) then
@@ -188,6 +222,7 @@ function Config:generate_query_files()
188222 return Result :new (Types .SUCCESS , true , " yeah, that could be" )
189223end
190224
225+ --- @return cmake.Result
191226function Config :get_cmake_files ()
192227 -- if reply_directory exists
193228 local reply_directory = Path :new (self .reply_directory )
@@ -204,6 +239,7 @@ function Config:get_cmake_files()
204239 return Result :new (Types .SUCCESS , codemodel_json [" inputs" ], " find it" )
205240end
206241
242+ --- @return cmake.Result
207243function Config :get_codemodel_targets ()
208244 -- if reply_directory exists
209245 local reply_directory = Path :new (self .reply_directory )
@@ -225,12 +261,15 @@ function Config:get_codemodel_targets()
225261 return Result :new (Types .SUCCESS , codemodel_json [" configurations" ][1 ][" targets" ], " find it" ) -- Return the first else
226262end
227263
264+ --- @param codemodel_target table
265+ --- @return table
228266function Config :get_code_model_target_info (codemodel_target )
229267 local reply_directory = Path :new (self .reply_directory )
230268 return vim .json .decode ((reply_directory / codemodel_target [" jsonFile" ]):read ())
231269end
232270
233- -- Check if launch target is built
271+ --- Check if launch target is built
272+ --- @return cmake.Result
234273function Config :check_launch_target ()
235274 -- 1. not configured
236275 local build_directory = Path :new (self .build_directory )
@@ -268,6 +307,8 @@ function Config:check_launch_target()
268307 )
269308end
270309
310+ --- @param target_info table
311+ --- @return cmake.Result
271312function Config :get_launch_target_from_info (target_info )
272313 local target_path = target_info [" artifacts" ][1 ][" path" ]
273314 if require (" cmake-tools.osys" ).iswin32 then
@@ -292,8 +333,8 @@ function Config:get_launch_target_from_info(target_info)
292333 return Result :new (Types .SUCCESS , target_path .filename , " yeah, that's good" )
293334end
294335
295- -- Retrieve launch target path: self.launch_target
296- -- it will first check if this launch target is built
336+ --- Retrieve launch target path
337+ --- @return cmake.Result
297338function Config :get_launch_target ()
298339 local check_result = self :check_launch_target ()
299340 if check_result .code ~= Types .SUCCESS then
@@ -304,7 +345,8 @@ function Config:get_launch_target()
304345 return self :get_launch_target_from_info (target_info )
305346end
306347
307- -- Check if build target exists
348+ --- Check if build target exists
349+ --- @return cmake.Result
308350function Config :check_build_target ()
309351 -- 1. not configured
310352 local build_directory = Path :new (self .build_directory )
@@ -338,8 +380,8 @@ function Config:check_build_target()
338380 )
339381end
340382
341- -- Retrieve launch target path: self.launch_target
342- -- it will first check if this launch target is built
383+ --- Retrieve build target path
384+ --- @return cmake.Result
343385function Config :get_build_target ()
344386 local check_result = self :check_build_target ()
345387 if check_result .code ~= Types .SUCCESS then
@@ -366,8 +408,8 @@ function Config:get_build_target()
366408 return Result :new (Types .SUCCESS , target_path .filename , " yeah, that's good" )
367409end
368410
369- -- Check if this launch target is debuggable
370- -- use variants.debuggable
411+ --- Check if this launch target is debuggable using variants. debuggable
412+ --- @return cmake.Result
371413function Config :validate_for_debugging ()
372414 local build_type = self .build_type
373415
@@ -377,6 +419,9 @@ function Config:validate_for_debugging()
377419 return Result :new (Types .SUCCESS , true , " Yeah, it may be" )
378420end
379421
422+ --- @param config Config
423+ --- @param opt { has_all : boolean , only_executable : boolean , query_sources : boolean ? }
424+ --- @return cmake.Result
380425local function get_targets (config , opt )
381426 local targets , display_targets , paths , abs_paths = {}, {}, {}, {}
382427 local sources = {}
@@ -447,6 +492,7 @@ local function get_targets(config, opt)
447492 end
448493end
449494
495+ --- @return table
450496function Config :get_code_model_info ()
451497 local codemodel_targets = self :get_codemodel_targets ()
452498 if codemodel_targets .code ~= Types .SUCCESS then
@@ -464,31 +510,37 @@ function Config:get_code_model_info()
464510 return result
465511end
466512
513+ --- @return cmake.Result
467514function Config :launch_targets ()
468515 return get_targets (self , { has_all = false , only_executable = true })
469516end
470517
518+ --- @return cmake.Result
471519function Config :build_targets ()
472520 return get_targets (self , { has_all = true , only_executable = false })
473521end
474522
523+ --- @return cmake.Result
475524function Config :launch_targets_with_sources ()
476525 return get_targets (self , { has_all = false , only_executable = true , query_sources = true })
477526end
478527
479528local _virtual_targets = nil
529+
480530function Config :update_targets ()
481531 _virtual_targets =
482532 get_targets (self , { has_all = false , only_executable = false , query_sources = true })
483533end
484534
535+ --- @return cmake.Result ?
485536function Config :build_targets_with_sources ()
486537 if not _virtual_targets then
487538 self :update_targets ()
488539 end
489540 return _virtual_targets
490541end
491542
543+ --- Update build type from configure and build presets
492544function Config :update_build_type ()
493545 local presets_exists = self .base_settings .use_preset and Presets .exists (self .cwd )
494546 if not presets_exists then
@@ -538,6 +590,7 @@ function Config:update_build_type()
538590 end
539591end
540592
593+ --- Update build target from build preset
541594function Config :update_build_target ()
542595 local presets_exists = self .base_settings .use_preset and Presets .exists (self .cwd )
543596 if not presets_exists then
@@ -570,6 +623,7 @@ function Config:update_build_target()
570623 end
571624end
572625
626+ --- Update build directory from kits or configure preset
573627function Config :update_build_directory ()
574628 local kits_config = kits .parse (self .cmake_kits_path , self .cwd )
575629 if kits_config then
0 commit comments