feat(nvim): setup overseer for C with cmake

This commit is contained in:
Kyren223
2024-08-05 16:26:54 +03:00
parent d296700cb3
commit 6abe2a79f7
5 changed files with 115 additions and 2 deletions

View File

@@ -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,
},
}

View File

@@ -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,
},
}

View File

@@ -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,
},
}

View File

@@ -1,3 +1,6 @@
return {
'custom.make_c',
'custom.make_c_file',
'custom.cmake_build',
'custom.cmake_run',
'custom.cmake_test',
}

View File

@@ -13,8 +13,21 @@ return {
}
end,
tags = { overseer.TAG.BUILD },
priority = 20,
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,
},
}