From 0660a9446d4515c9755573152ce944ebf2f019fc Mon Sep 17 00:00:00 2001 From: Daniil Rozanov Date: Fri, 26 Apr 2024 03:33:49 +0300 Subject: fix: better autocmds Correctly process case when neovim opened witn nvim CMakeLists.txt command. At the moment of first setup CMakeLists.txt does not exists, so we need to setup project on BufEnter if it was not inisialized before --- lua/cmake/actions.lua | 11 ++++++++++- lua/cmake/autocmds.lua | 25 ++++++++++++++++++++++--- lua/cmake/init.lua | 11 +++++------ lua/cmake/project.lua | 30 +++++++++++++++++++++++++++--- 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/lua/cmake/actions.lua b/lua/cmake/actions.lua index 4008f7c..8c5a300 100644 --- a/lua/cmake/actions.lua +++ b/lua/cmake/actions.lua @@ -27,8 +27,17 @@ local default_build_exe_opts = { }, } +M.reset_project = function(opts) + require("cmake.project").setup(opts) +end + M.generate = function(opts) - pr.create_fileapi_query({ idx = pr.current_generate_option_idx() }, function() + local idx = pr.current_generate_option_idx() + if not idx then + vim.notify("CMake: no project to generate") + return + end + pr.create_fileapi_query({ idx = idx }, function() vim.schedule(function() t.cmake_execute(pr.current_generate_option().generate_command, default_generate_exe_opts) end) diff --git a/lua/cmake/autocmds.lua b/lua/cmake/autocmds.lua index f430cf1..4b5b479 100644 --- a/lua/cmake/autocmds.lua +++ b/lua/cmake/autocmds.lua @@ -4,16 +4,20 @@ local constants = require("cmake.constants") local autocmds = {} -function autocmds.setup() - local cmake_nvim_augroup = vim.api.nvim_create_augroup("CMake", {}) +local cmake_nvim_augroup = vim.api.nvim_create_augroup("CMake", {}) + +function autocmds.set_on_variants() vim.api.nvim_create_autocmd({ "BufWritePost" }, { group = cmake_nvim_augroup, pattern = constants.variants_yaml_filename, callback = function(args) - require("cmake.project").setup() + actions.reset_project() end, desc = "Setup project after saving variants", }) +end + +function autocmds.setup() if config.generate_after_save then vim.api.nvim_create_autocmd({ "BufWritePost" }, { group = cmake_nvim_augroup, @@ -24,6 +28,21 @@ function autocmds.setup() desc = "Generate project after saving CMakeLists.txt", }) end + --NOTE: this autocmd was written only to handle very rarely case when inside directory + --without CMakeLists.txt neovim starts like `nvim CMakeLists.txt`. In this case initial + --setup will not make the affect and to correctry process the file save, we need to create + --this autocommand so it reinitializes the project if it has not been done before. IMHO this + --is not the best way to do this + if config.generate_after_save then + vim.api.nvim_create_autocmd({ "BufEnter" }, { + group = cmake_nvim_augroup, + pattern = constants.cmakelists, + callback = function(args) + actions.reset_project({ first_time_only = true }) + end, + desc = "Set up project on open CMakeLists.txt if not set before", + }) + end end return autocmds diff --git a/lua/cmake/init.lua b/lua/cmake/init.lua index febf307..adfa84e 100644 --- a/lua/cmake/init.lua +++ b/lua/cmake/init.lua @@ -12,15 +12,14 @@ function M.setup(opts) opts = opts or {} config.setup(opts) if vim.fn.executable(config.cmake.cmake_path) then + autocmds.setup() 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.setup() - commands.register_commands() - end) - require("cmake.project").setup() + vim.schedule(function() + autocmds.set_on_variants() + commands.register_commands() end) + require("cmake.project").setup({ first_time_only = true }) else end end) diff --git a/lua/cmake/project.lua b/lua/cmake/project.lua index 3b7ed04..6f5a21e 100644 --- a/lua/cmake/project.lua +++ b/lua/cmake/project.lua @@ -7,6 +7,8 @@ local uv = vim.uv or vim.loop local Project = {} +local initialised = false + local configs = {} local current_config = nil local fileapis = {} @@ -15,6 +17,7 @@ local reset_internals = function() configs = {} current_config = nil fileapis = {} + initialised = true end local append_after_success_actions = function() @@ -188,9 +191,7 @@ function Project.create_fileapi_query(opts, callback) end) end -function Project.setup(opts) - opts = opts or {} - reset_internals() +local do_setup = function(opts) local variants_path = vim.fs.joinpath(uv.cwd(), constants.variants_yaml_filename) utils.file_exists(variants_path, function(variants_exists) if variants_exists then @@ -204,4 +205,27 @@ function Project.setup(opts) end) end +function Project.setup(opts) + opts = opts or {} + vim.notify( + "Start setup. " .. vim.inspect(opts.first_time_only) .. " " .. tostring(initialised), + vim.log.levels.INFO + ) + if opts.first_time_only and initialised then + vim.notify( + "Setup abort. " .. vim.inspect(opts.first_time_only) .. " " .. tostring(initialised), + vim.log.levels.INFO + ) + return + end + reset_internals() + if not initialised then + require("cmake.capabilities").setup(function() + do_setup(opts) + end) + else + do_setup(opts) + end +end + return Project -- cgit v1.2.3