From eedb67c18d38299a7433f6d6cfd1f3917b8621d1 Mon Sep 17 00:00:00 2001 From: dlond Date: Thu, 29 May 2025 22:56:44 +1200 Subject: [PATCH] refactor --- lua/custom/plugins/lsp/clangd.lua | 105 +++++++++++++++--------------- 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/lua/custom/plugins/lsp/clangd.lua b/lua/custom/plugins/lsp/clangd.lua index dd093875..d2478c52 100644 --- a/lua/custom/plugins/lsp/clangd.lua +++ b/lua/custom/plugins/lsp/clangd.lua @@ -1,60 +1,57 @@ -local M = {} - -local pickers = require 'telescope.pickers' -local finders = require 'telescope.finders' -local conf = require('telescope.config').values - -local function build_cmd(commands_dir) - return { - 'clangd', - '--background-index', - '--clang-tidy', - '--header-insertion=never', - '--query-driver=' .. vim.vn.exepath 'clang++', - '--resource-dir=' .. vim.fn.trim(vim.fn.system 'clang++ --print-resource-dir'), - '--compile-commands-dir=' .. commands_dir, - } -end - -function M.reload_clangd(commands_dir) - local lspconfig = require 'lspconfig' - - commands_dir = commands_dir or './build/debug' - - lspconfig.clangd.setup { - cmd = build_cmd(commands_dir), - root_dir = lspconfig.util.root_pattern '.git', - single_file_support = true, - capabilities = require('blink.cmp').get_lsp_capabilities(), - } - - vim.cmd.edit() -end - -function M.pick_commands_dir() - pickers - .new({}, { - prompt_title = 'Choose compile_commands.json location', - finder = finders.new_oneshot_job { 'fd', '-u', 'compile_commands.json', '-x', 'dirname', '{}' }, - sorter = conf.generic_sorter {}, - attach_mappings = function(_, map) - map('i', '', function(prompt_bufnr) - local entry = require('telescope.actions.state').get_selected_entry() - local commands_dir = entry[1] - require('telescope.actions').close(prompt_bufnr) - M.reload_clangd(commands_dir) - end) - return true - end, - }) - :find() -end - return { 'neovim/nvim-lspconfig', ft = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' }, config = function() - M.reload_clangd() - vim.keymap.set('n', 'cc', M.pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' }) + local function build_cmd(commands_dir) + return { + 'clangd', + '--background-index', + '--clang-tidy', + '--header-insertion=never', + '--query-driver=' .. vim.vn.exepath 'clang++', + '--resource-dir=' .. vim.fn.trim(vim.fn.system 'clang++ --print-resource-dir'), + '--compile-commands-dir=' .. commands_dir, + } + end + + local function reload_clangd(commands_dir) + commands_dir = commands_dir or './build/debug' + local lspconfig = require 'lspconfig' + + lspconfig.clangd.setup { + cmd = build_cmd(commands_dir), + root_dir = lspconfig.util.root_pattern '.git', + single_file_support = true, + capabilities = require('blink.cmp').get_lsp_capabilities(), + } + + vim.cmd.edit() + end + + local function pick_commands_dir() + local pickers = require 'telescope.pickers' + local finders = require 'telescope.finders' + local conf = require('telescope.config').values + pickers + .new({}, { + prompt_title = 'Choose compile_commands.json location', + finder = finders.new_oneshot_job { 'fd', '-u', 'compile_commands.json', '-x', 'dirname', '{}' }, + sorter = conf.generic_sorter {}, + attach_mappings = function(_, map) + map('i', '', function(prompt_bufnr) + local entry = require('telescope.actions.state').get_selected_entry() + local commands_dir = entry[1] + require('telescope.actions').close(prompt_bufnr) + reload_clangd(commands_dir) + end) + return true + end, + }) + :find() + end + + reload_clangd() + + vim.keymap.set('n', 'cc', pick_commands_dir, { desc = 'Pick location of compile_commands.json for clangd' }) end, }