diff options
author | Daniil Rozanov <daniilrozzanov@gmail.com> | 2024-04-26 03:33:49 +0300 |
---|---|---|
committer | Daniil Rozanov <daniilrozzanov@gmail.com> | 2024-04-26 03:33:49 +0300 |
commit | 0660a9446d4515c9755573152ce944ebf2f019fc (patch) | |
tree | b587c6de58d6475feadade022fa918d15a3eba74 /lua/cmake/autocmds.lua | |
parent | a21a7207041754efdc811493ea131b6dd2d0f944 (diff) |
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
Diffstat (limited to 'lua/cmake/autocmds.lua')
-rw-r--r-- | lua/cmake/autocmds.lua | 25 |
1 files changed, 22 insertions, 3 deletions
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 |