diff options
-rw-r--r-- | lua/cmake/actions.lua | 6 | ||||
-rw-r--r-- | lua/cmake/autocmds.lua | 29 | ||||
-rw-r--r-- | lua/cmake/capabilities.lua | 34 | ||||
-rw-r--r-- | lua/cmake/config.lua | 5 | ||||
-rw-r--r-- | lua/cmake/constants.lua | 1 | ||||
-rw-r--r-- | lua/cmake/fileapi.lua | 2 | ||||
-rw-r--r-- | lua/cmake/init.lua | 24 | ||||
-rw-r--r-- | lua/cmake/project.lua | 10 | ||||
-rw-r--r-- | lua/cmake/utils.lua | 2 | ||||
-rw-r--r-- | lua/cmake/variants.lua | 6 |
10 files changed, 90 insertions, 29 deletions
diff --git a/lua/cmake/actions.lua b/lua/cmake/actions.lua index 3c09d93..4008f7c 100644 --- a/lua/cmake/actions.lua +++ b/lua/cmake/actions.lua @@ -5,6 +5,8 @@ local utils = require("cmake.utils") local constants = require("cmake.constants") local Path = require("plenary.path") +local uv = vim.uv or vim.loop + local M = {} local default_generate_exe_opts = { @@ -92,7 +94,7 @@ M.run_tagret = function(opts) M.run_tagret_select(opts) else local command = { - cmd = Path:new(pr.current_directory(), _curr_exe_cmd.path):make_relative(vim.loop.cwd()), + cmd = Path:new(pr.current_directory(), _curr_exe_cmd.path):make_relative(uv.cwd()), } t.target_execute(command) end @@ -113,7 +115,7 @@ M.run_tagret_select = function(opts) end pr.set_current_executable_target(idx) local command = { - cmd = Path:new(pr.current_directory(), choice.path):make_relative(vim.loop.cwd()), + cmd = Path:new(pr.current_directory(), choice.path):make_relative(uv.cwd()), } t.target_execute(command) end) diff --git a/lua/cmake/autocmds.lua b/lua/cmake/autocmds.lua new file mode 100644 index 0000000..e665182 --- /dev/null +++ b/lua/cmake/autocmds.lua @@ -0,0 +1,29 @@ +local config = require("cmake.config") +local actions = require("cmake.actions") +local constants = require("cmake.constants") + +local autocmds = {} + +function autocmds.setuo() + local cmake_nvim_augroup = vim.api.nvim_create_augroup("CMake", {}) + vim.api.nvim_create_autocmd({ "BufWritePost" }, { + group = cmake_nvim_augroup, + pattern = constants.variants_yaml_filename, + callback = function(args) + require("cmake.project").setup() + end, + desc = "Setup project after saving variants", + }) + if config.generate_after_save then + vim.api.nvim_create_autocmd({ "BufWritePost" }, { + group = cmake_nvim_augroup, + pattern = constants.cmakelists, + callback = function(args) + actions.generate() + end, + desc = "Generate project after saving CMakeLists.txt", + }) + end +end + +return autocmds diff --git a/lua/cmake/capabilities.lua b/lua/cmake/capabilities.lua index 6a70be2..36bc4b1 100644 --- a/lua/cmake/capabilities.lua +++ b/lua/cmake/capabilities.lua @@ -41,23 +41,25 @@ end -- TODO: make this async Capabilities.setup = function(callback) local lines = {} - vim.fn.jobstart({ config.cmake.cmake_path, "-E", "capabilities" }, { - on_stdout = function(_, data) - if data then - vim.list_extend(lines, data) - end - end, - on_exit = function(_, code, _) - if code == 0 then - Capabilities.json = vim.json.decode(table.concat(lines, "")) - if type(callback) == "function" then - callback() + vim.schedule(function() + vim.fn.jobstart({ config.cmake.cmake_path, "-E", "capabilities" }, { + on_stdout = function(_, data) + if data then + vim.list_extend(lines, data) end - else - vim.notify("error " .. tostring(code) .. ". 'cmake -E capabilities'", vim.log.levels.ERROR) - end - end, - }) + end, + on_exit = function(_, code, _) + if code == 0 then + Capabilities.json = vim.json.decode(table.concat(lines, "")) + if type(callback) == "function" then + callback() + end + else + vim.notify("error " .. tostring(code) .. ". 'cmake -E capabilities'", vim.log.levels.ERROR) + end + end, + }) + end) end return Capabilities diff --git a/lua/cmake/config.lua b/lua/cmake/config.lua index 5a4cb75..49dbc50 100644 --- a/lua/cmake/config.lua +++ b/lua/cmake/config.lua @@ -1,6 +1,8 @@ local default_config = { cmake = { cmake_path = "cmake", + ctest_path = "ctest", + cpack_path = "cpack", environment = {}, configure_environment = {}, build_directory = "${workspaceFolder}/build-${buildType}", @@ -21,9 +23,10 @@ local default_config = { }, }, parallel_jobs = 0, - save_before_build = true, source_directory = "${workspaceFolder}", }, + save_before_build = true, + generate_after_save = true, terminal = { direction = "vertical", display_name = "CMake", diff --git a/lua/cmake/constants.lua b/lua/cmake/constants.lua index fcbfea3..95f15b3 100644 --- a/lua/cmake/constants.lua +++ b/lua/cmake/constants.lua @@ -1,3 +1,4 @@ return { variants_yaml_filename = "cmake-variants.yaml", + cmakelists = "CMakeLists.txt", } diff --git a/lua/cmake/fileapi.lua b/lua/cmake/fileapi.lua index 47a7459..d33d477 100644 --- a/lua/cmake/fileapi.lua +++ b/lua/cmake/fileapi.lua @@ -2,7 +2,7 @@ local capabilities = require("cmake.capabilities") local Path = require("plenary.path") local scan = require("plenary.scandir") local utils = require("cmake.utils") -local uv = vim.loop +local uv = vim.uv or vim.loop local query_path_suffix = { ".cmake", "api", "v1", "query", "client-cmake", "query.json" } local reply_dir_suffix = { ".cmake", "api", "v1", "reply" } diff --git a/lua/cmake/init.lua b/lua/cmake/init.lua index d53e074..dcead08 100644 --- a/lua/cmake/init.lua +++ b/lua/cmake/init.lua @@ -1,5 +1,10 @@ local config = require("cmake.config") local commands = require("cmake.commands") +local autocmds = require("cmake.autocmds") +local utils = require("cmake.utils") +local constants = require("cmake.constants") + +local uv = vim.uv or vim.loop local M = {} @@ -7,12 +12,23 @@ function M.setup(opts) opts = opts or {} config.setup(opts) if vim.fn.executable(config.cmake.cmake_path) then - commands.register_commands() - require("cmake.capabilities").setup(function() - require("cmake.project").setup(opts) + utils.file_exists(vim.fs.joinpath(uv.cwd(), constants.cmakelists), function(cmake_lists_exists) + if cmake_lists_exists then + require("cmake.capabilities").setup(function() + vim.schedule(function() + autocmds.setuo() + commands.register_commands() + end) + require("cmake.project").setup() + end) + else + end end) else - vim.notify("CMake: " .. config.cmake.cmake_path .. " is not executable", vim.log.levels.WARN) + vim.notify( + "CMake: " .. config.cmake.cmake_path .. " is not executable. Plugin is unavailable", + vim.log.levels.WARN + ) end end diff --git a/lua/cmake/project.lua b/lua/cmake/project.lua index ad713e3..3b7ed04 100644 --- a/lua/cmake/project.lua +++ b/lua/cmake/project.lua @@ -11,11 +11,17 @@ local configs = {} local current_config = nil local fileapis = {} +local reset_internals = function() + configs = {} + current_config = nil + fileapis = {} +end + local append_after_success_actions = function() local read_reply = function(v, not_presented) if (not_presented and not fileapis[v.directory]) or not not_presented then --TODO: replace to vim.fs.joinpath after nvim 0.10 release - utils.symlink(v.directory .. "/compile_commands.json", vim.loop.cwd()) + utils.symlink(v.directory .. "/compile_commands.json", uv.cwd()) fileapis[v.directory] = { targets = {} } FileApi.read_reply(v.directory, function(target) table.insert(fileapis[v.directory].targets, target) @@ -58,7 +64,6 @@ function Project.from_variants(variants) list_variants[#list_variants]._name = k end table.sort(list_variants, function(a, b) - vim.notify(a._name .. " " .. b._name) return a._name < b._name end) for var, is_default in VariantConfig.cartesian_product(list_variants) do @@ -185,6 +190,7 @@ end function Project.setup(opts) opts = opts or {} + reset_internals() local variants_path = vim.fs.joinpath(uv.cwd(), constants.variants_yaml_filename) utils.file_exists(variants_path, function(variants_exists) if variants_exists then diff --git a/lua/cmake/utils.lua b/lua/cmake/utils.lua index 1c68fae..b9bcdf8 100644 --- a/lua/cmake/utils.lua +++ b/lua/cmake/utils.lua @@ -2,7 +2,7 @@ local config = require("cmake.config") local capabilities = require("cmake.capabilities") local scan = require("plenary.scandir") local Path = require("plenary.path") -local uv = vim.loop +local uv = vim.uv or vim.loop local utils = {} diff --git a/lua/cmake/variants.lua b/lua/cmake/variants.lua index 289d0e4..0d54a79 100644 --- a/lua/cmake/variants.lua +++ b/lua/cmake/variants.lua @@ -1,13 +1,15 @@ local config = require("cmake.config") local utils = require("cmake.utils") +local uv = vim.uv or vim.loop + local VariantConfig = {} VariantConfig.__index = VariantConfig local global_variant_subs = { - ["${workspaceFolder}"] = vim.loop.cwd(), - ["${userHome}"] = vim.loop.os_homedir(), + ["${workspaceFolder}"] = uv.cwd(), + ["${userHome}"] = uv.os_homedir(), } local _configure_args = function(obj, build_directory) |