From 6abe2a79f71401003134d967327d4ee526ed12ae Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Mon, 5 Aug 2024 16:26:54 +0300 Subject: feat(nvim): setup overseer for C with cmake --- .../lua/overseer/template/custom/cmake_build.lua | 24 ++++++++++++ .../lua/overseer/template/custom/cmake_run.lua | 45 ++++++++++++++++++++++ .../lua/overseer/template/custom/cmake_test.lua | 28 ++++++++++++++ .config/nvim/lua/overseer/template/custom/init.lua | 5 ++- .../nvim/lua/overseer/template/custom/make_c.lua | 20 ---------- .../lua/overseer/template/custom/make_c_file.lua | 33 ++++++++++++++++ 6 files changed, 134 insertions(+), 21 deletions(-) create mode 100644 .config/nvim/lua/overseer/template/custom/cmake_build.lua create mode 100644 .config/nvim/lua/overseer/template/custom/cmake_run.lua create mode 100644 .config/nvim/lua/overseer/template/custom/cmake_test.lua delete mode 100644 .config/nvim/lua/overseer/template/custom/make_c.lua create mode 100644 .config/nvim/lua/overseer/template/custom/make_c_file.lua diff --git a/.config/nvim/lua/overseer/template/custom/cmake_build.lua b/.config/nvim/lua/overseer/template/custom/cmake_build.lua new file mode 100644 index 0000000..cb321a0 --- /dev/null +++ b/.config/nvim/lua/overseer/template/custom/cmake_build.lua @@ -0,0 +1,24 @@ +local overseer = require('overseer') +local root_pattern = require('lspconfig/util').root_pattern('CMakePresets.json', 'CTestConfig.cmake', 'build', 'cmake') +return { + name = 'Build Cmake Project', + builder = function(_) + local filename = vim.fn.expand('%:p') + local root_dir = root_pattern(filename) or vim.fn.getcwd() + + return { + name = 'Cmake', + cmd = 'cmake .. && cmake --build .', + cwd = root_dir .. '/build', + } + end, + tags = { overseer.TAG.BUILD }, + priority = 40, + condition = { + callback = function() + local filename = vim.fn.expand('%:p') + local root_dir = root_pattern(filename) + return root_dir ~= nil + end, + }, +} diff --git a/.config/nvim/lua/overseer/template/custom/cmake_run.lua b/.config/nvim/lua/overseer/template/custom/cmake_run.lua new file mode 100644 index 0000000..cdb4d5c --- /dev/null +++ b/.config/nvim/lua/overseer/template/custom/cmake_run.lua @@ -0,0 +1,45 @@ +local overseer = require('overseer') +local root_pattern = require('lspconfig/util').root_pattern('CMakePresets.json', 'CTestConfig.cmake', 'build', 'cmake') +return { + name = 'Run Cmake Project', + params = { + args = { + optional = true, + type = 'string', + }, + }, + builder = function(params) + local filename = vim.fn.expand('%:p') + local root_dir = root_pattern(filename) + local exe = vim.g.overseer_cmake_main_exe + + -- Make sure params.args is a string + if not params.args or type(params.args) ~= 'string' then + params.args = '' + end + + return { + name = 'Run ' .. exe, + cmd = './' .. 'Kalc ' .. params.args, + cwd = root_dir .. '/build', + } + end, + tags = { overseer.TAG.RUN }, + priority = 41, + condition = { + callback = function() + local filename = vim.fn.expand('%:p') + local root_dir = root_pattern(filename) + if not root_dir then + return false, 'Unable to detect root directory of cmake project' + end + + if not vim.g.overseer_cmake_main_exe then + return false, + 'Global "overseer_cmake_main_exe" must be defined. use `vim.g.overseer_cmake_main_exe = "Value"` to define it' + end + + return true + end, + }, +} diff --git a/.config/nvim/lua/overseer/template/custom/cmake_test.lua b/.config/nvim/lua/overseer/template/custom/cmake_test.lua new file mode 100644 index 0000000..7591cc9 --- /dev/null +++ b/.config/nvim/lua/overseer/template/custom/cmake_test.lua @@ -0,0 +1,28 @@ +local overseer = require('overseer') +local root_pattern = require('lspconfig/util').root_pattern('CMakePresets.json', 'CTestConfig.cmake', 'build', 'cmake') +return { + name = 'Test Cmake Project', + builder = function(_) + local filename = vim.fn.expand('%:p') + local root_dir = root_pattern(filename) + + return { + name = 'Cmake Tests', + cmd = 'ctest --output-on-failure', + cwd = root_dir .. '/build', + } + end, + tags = { overseer.TAG.TEST }, + priority = 42, + condition = { + callback = function() + local filename = vim.fn.expand('%:p') + local root_dir = root_pattern(filename) + if not root_dir then + return false, 'Unable to detect root directory of cmake project' + end + + return true + end, + }, +} diff --git a/.config/nvim/lua/overseer/template/custom/init.lua b/.config/nvim/lua/overseer/template/custom/init.lua index be9e689..796ee39 100644 --- a/.config/nvim/lua/overseer/template/custom/init.lua +++ b/.config/nvim/lua/overseer/template/custom/init.lua @@ -1,3 +1,6 @@ return { - 'custom.make_c', + 'custom.make_c_file', + 'custom.cmake_build', + 'custom.cmake_run', + 'custom.cmake_test', } diff --git a/.config/nvim/lua/overseer/template/custom/make_c.lua b/.config/nvim/lua/overseer/template/custom/make_c.lua deleted file mode 100644 index 358047f..0000000 --- a/.config/nvim/lua/overseer/template/custom/make_c.lua +++ /dev/null @@ -1,20 +0,0 @@ -local overseer = require('overseer') -return { - name = 'Make Current C file', - builder = function(_) - local path = vim.fn.expand('%:p') - local dir, file = path:gmatch('(.*)[/\\](.*)')() - file = string.sub(file, 1, -3) -- remove .c extension - - return { - cmd = { 'make' }, - args = { file }, - cwd = dir, - } - end, - tags = { overseer.TAG.BUILD }, - priority = 20, - condition = { - filetype = { 'c' }, - }, -} diff --git a/.config/nvim/lua/overseer/template/custom/make_c_file.lua b/.config/nvim/lua/overseer/template/custom/make_c_file.lua new file mode 100644 index 0000000..75f89d0 --- /dev/null +++ b/.config/nvim/lua/overseer/template/custom/make_c_file.lua @@ -0,0 +1,33 @@ +local overseer = require('overseer') +return { + name = 'Make Current C file', + builder = function(_) + local path = vim.fn.expand('%:p') + local dir, file = path:gmatch('(.*)[/\\](.*)')() + file = string.sub(file, 1, -3) -- remove .c extension + + return { + cmd = { 'make' }, + args = { file }, + cwd = dir, + } + end, + tags = { overseer.TAG.BUILD }, + priority = 50, + condition = { + filetype = { 'c' }, + callback = function() + -- Disable if already using make/cmake + local filename = vim.fn.expand('%:p') + local root_dir = require('lspconfig/util').root_pattern( + 'Makefile', + 'build/Makefile', + 'CMakePresets.json', + 'CTestConfig.cmake', + 'build', + 'cmake' + )(filename) + return root_dir == nil + end, + }, +} -- cgit v1.3.1