try watch compile_config

This commit is contained in:
dlond 2025-05-30 04:10:53 +12:00
parent d523eeb957
commit b2038d3788
1 changed files with 48 additions and 67 deletions

View File

@ -14,8 +14,10 @@ end
function M.stop_clangd() function M.stop_clangd()
for _, client in ipairs(vim.lsp.get_clients()) do for _, client in ipairs(vim.lsp.get_clients()) do
if client.name == 'clangd' then if client.name == 'clangd' then
client.stop { force = true } pcall(function()
vim.notify '[clangd] stopped clangd' client.stop { force = true }
end)
vim.notify '[clangd] Stopped clangd'
end end
end end
end end
@ -32,11 +34,11 @@ function M.start_clangd(dir)
'--resource-dir=' .. vim.fn.systemlist({ 'clang++', '--print-resource-dir' })[1], '--resource-dir=' .. vim.fn.systemlist({ 'clang++', '--print-resource-dir' })[1],
} }
if dir and dir ~= '' then if dir and dir ~= '' then
table.insert(cmd, '--compile-commands-dir=' .. dir)
vim.notify('[clangd] Setting up with: ' .. dir) vim.notify('[clangd] Setting up with: ' .. dir)
table.insert(cmd, '--compile-commands-dir=' .. dir)
else else
table.insert(cmd, '--compile-commands-dir=.')
vim.notify '[clangd] No compile_commands.json found.\nUse <leader>cc to manually set location' vim.notify '[clangd] No compile_commands.json found.\nUse <leader>cc to manually set location'
table.insert(cmd, '--compile-commands-dir=.')
end end
lspconfig.clangd.setup { lspconfig.clangd.setup {
@ -56,18 +58,6 @@ function M.reload_clangd()
end, 100) end, 100)
end end
-- for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
-- local ft = vim.api.nvim_get_option_value('filetype', { buf = bufnr })
-- if vim.tbl_contains(M.clang_filetypes, ft) then
-- local client = vim.lsp.get_clients({ name = 'clangd' })[1]
-- if client then
-- vim.lsp.buf_attach_client(bufnr, client.id)
-- end
-- end
-- end
-- end, 100)
-- end
function M.pick_commands_dir() function M.pick_commands_dir()
local pickers = require 'telescope.pickers' local pickers = require 'telescope.pickers'
local finders = require 'telescope.finders' local finders = require 'telescope.finders'
@ -92,62 +82,53 @@ function M.pick_commands_dir()
:find() :find()
end end
-- function M.watch_compile_commands() function M.watch_compile_commands(dir)
-- vim.notify('clangd: Starting watcher for compile_commands.json', vim.log.levels.INFO) local uv = vim.uv or vim.loop
-- local uv = vim.uv or vim.loop local watcher = uv.new_fs_event()
-- local check_interval = 1000 -- ms local debounce_timer
--
-- local function try_attach() local watch_path = dir or vim.fn.getcwd()
-- local dir = find_compile_commands() local watch_file = watch_path .. '/compile_commands.json'
-- if dir then
-- vim.notify('[clangd] Found compile_commands at: ' .. dir) if not vim.fn.filereadable(watch_file) then
-- M.setup_clangd(dir) return
-- return true end
-- end
-- return false watcher:start(watch_path, { recursive = true }, function(err, fname, status)
-- end if err then
-- vim.schedule(function()
-- local timer = uv.new_timer() vim.notify('[clangd] Watcher error: ' .. err, vim.log.levels.ERROR)
-- timer:start(0, check_interval, function() end)
-- if try_attach() then return
-- timer:stop() end
-- timer:close() if fname and fname:match 'compile_commands%.json$' and status.change then
-- end if debounce_timer then
-- end) debounce_timer.stop()
-- debounce_timer.close()
-- local fs_watcher = uv.new_fs_event() end
-- fs_watcher:start(vim.fn.getcwd(), { recursive = true }, function(_, fname, status) debounce_timer = uv.new_timer()
-- if fname and fname:match 'compile_commands%.json$' and status.change then debounce_timer:start(200, 0, function()
-- fs_watcher:stop() vim.schedule(function()
-- vim.schedule(function() vim.notify '[clangd] Detected compile_commands.json change. Reloading ...'
-- vim.notify '[clangd] Detected compile_commands.json change, reloading ...' -- M.start_clangd(watch_path)
-- M.setup_clangd(vim.fn.fnamemodify(fname, ':h')) M.reload_clangd()
-- end) end)
-- end end)
-- end) end
-- end end)
vim.notify('[clangd] Watching: ' .. watch_file)
end
return { return {
'neovim/nvim-lspconfig', 'neovim/nvim-lspconfig',
ft = M.clang_filetypes, ft = M.clang_filetypes,
config = function() config = function()
-- vim.api.nvim_create_autocmd('FileType', { local dir = find_compile_commands()
-- pattern = M.clang_filetypes, M.start_clangd(dir)
-- group = vim.api.nvim_create_augroup('clangd-setup', { clear = true }), if dir ~= '' then
-- callback = function() M.watch_compile_commands(dir)
-- if not vim.lsp.get_clients({ name = 'clangd' })[1] then end
-- local dir = find_compile_commands()
-- if dir then
-- vim.notify('[clangd] Starting with compile_commands from: ' .. dir)
-- M.start_clangd(dir)
-- else
-- vim.notify '[clangd] No compile_commands found. Using fallback config.\nUse <leader>cc to manually set location.'
-- M.start_clangd(nil)
-- end
-- end
-- end,
-- })
M.start_clangd(find_compile_commands())
vim.keymap.set('n', '<leader>cc', M.pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' }) vim.keymap.set('n', '<leader>cc', M.pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' })
end, end,