From 2e1386a781df8957940435749204daff21855300 Mon Sep 17 00:00:00 2001 From: Oliver Hartmann Date: Wed, 16 Apr 2025 23:32:31 +0200 Subject: [PATCH] switched to new lsp config --- lua/plugins/lsp.lua | 169 +++++++++++++++++++++++++++++++++++++ lua/plugins/lsp/server.lua | 141 ------------------------------- lua/plugins/lspconfig.lua | 74 ---------------- 3 files changed, 169 insertions(+), 215 deletions(-) create mode 100644 lua/plugins/lsp.lua delete mode 100644 lua/plugins/lsp/server.lua delete mode 100644 lua/plugins/lspconfig.lua diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua new file mode 100644 index 0000000..dc4dc95 --- /dev/null +++ b/lua/plugins/lsp.lua @@ -0,0 +1,169 @@ +local lspKeys = function(client, bufnr) + local options = { noremap = true, silent = false, buffer = bufnr } + vim.keymap.set({ 'n', 'x' }, 'a', vim.lsp.buf.code_action, vim.tbl_extend('error', options, { desc = 'Code action' })) + vim.keymap.set('n', 'e', vim.lsp.buf.declaration, vim.tbl_extend('error', options, { desc = 'Declaration' })) + vim.keymap.set('n', 'h', function() + vim.lsp.buf.hover({ border = 'none' }) + end, vim.tbl_extend('error', options, { desc = 'Hover' })) + vim.keymap.set('n', 'c', vim.lsp.buf.outgoing_calls, vim.tbl_extend('error', options, { desc = 'Outgoing calls' })) + vim.keymap.set('n', 'C', vim.lsp.buf.incoming_calls, vim.tbl_extend('error', options, { desc = 'Incoming calls' })) + vim.keymap.set('n', 'm', vim.lsp.buf.rename, vim.tbl_extend('error', options, { desc = 'Rename' })) + vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, vim.tbl_extend('error', options, { desc = 'Type definition' })) + vim.keymap.set({ 'n', 'i', 'x' }, '', vim.lsp.buf.signature_help, vim.tbl_extend('error', options, { desc = 'Signature help' })) + vim.keymap.set('n', 'v', function() + vim.diagnostic.open_float({ border = 'rounded' }) + end, vim.tbl_extend('error', options, { desc = 'Diagnostics Float' })) + vim.keymap.set('n', '', 'ClangdSwitchSourceHeader', vim.tbl_extend('error', options, { desc = 'Switch Source/Header' })) + + if client.supports_method('inlayHintProvider') then + vim.keymap.set('n', 'i', function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }), { bufnr = bufnr }) + end, { + noremap = true, + silent = false, + desc = 'Toggle inlay hints', + buffer = bufnr, + }) + end +end +return { + { + 'neovim/nvim-lspconfig', + dependencies = { + 'williamboman/mason.nvim', + 'williamboman/mason-lspconfig.nvim', + 'p00f/clangd_extensions.nvim', + 'Fildo7525/pretty_hover', + { + 'creativenull/efmls-configs-nvim', + branch = 'main', + }, + 'SmiteshP/nvim-navic', + }, + lazy = false, + config = function() + local servers = { + basedpyright = { + settings = { + basedpyright = { + typeCheckingMode = 'standard', + }, + }, + }, + ruff = {}, + clangd = { + cmd = { + 'clangd', + '--compile-commands-dir=build_nvim', + '--query-driver', + '/opt/cortex-a78-2022.08-gcc12.1-linux5.15/bin/aarch64-linux-gnu-g*', + '--clang-tidy', + '--background-index', + '--use-dirty-headers', + '--completion-style=detailed', + }, + root_markers = { + '.clangd', + '.clang-tidy', + '.clang-format', + 'compile_flags.txt', + 'configure.ac', + '.git', + 'build_nvim', + }, + }, + lua_ls = { + settings = { + Lua = { + workspace = { + checkThirdParty = false, + }, + completion = { + callSnippet = 'Replace', + }, + -- Do not send telemetry data containing a randomized but unique identifier + telemetry = { + enable = false, + }, + diagnostics = { + disable = { 'missing-fields' }, + }, + format = { + enable = true, + defaultConfig = { + indent_style = 'space', + indent_size = '2', + quote_style = 'single', + }, + }, + }, + }, + }, + jsonls = {}, + dockerls = {}, + yamlls = { + settings = { + yaml = { + validate = true, + }, + }, + }, + neocmake = {}, + markdown_oxide = {}, + } + + local ensure_installed = vim.tbl_keys(servers or {}) + + require('mason').setup() + require('mason-lspconfig').setup({ + ensure_installed = ensure_installed, + }) + + local capabilities = vim.lsp.protocol.make_client_capabilities() + capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) + vim.lsp.config('*', { + capabilities = capabilities, + }) + + for server, settings in pairs(servers) do + vim.lsp.config(server, settings) + vim.lsp.enable(server) + end + + local lsp_group = vim.api.nvim_create_augroup('UserLspAttach', { clear = true }) + vim.api.nvim_create_autocmd('LspAttach', { + group = lsp_group, + desc = 'Set buffer‑local keymaps and options after an LSP client attaches', + callback = function(args) + local bufnr = args.buf + local client = vim.lsp.get_client_by_id(args.data.client_id) + if not client then + return + end + lspKeys(client, bufnr) + + if client.server_capabilities.completionProvider then + vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' + vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr()' + end + + if client.server_capabilities.inlayHintProvider then + vim.keymap.set('n', 'i', function() + vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }), { bufnr = bufnr }) + end, { + noremap = true, + silent = false, + desc = 'Toggle inlay hints', + buffer = bufnr, + }) + end + + if client.server_capabilities.documentSymbolProvider then + local navic = require('nvim-navic') + navic.attach(client, bufnr) + end + end, + }) + end, + }, +} diff --git a/lua/plugins/lsp/server.lua b/lua/plugins/lsp/server.lua deleted file mode 100644 index 0d7b773..0000000 --- a/lua/plugins/lsp/server.lua +++ /dev/null @@ -1,141 +0,0 @@ -local M = {} - -M.setup_server = function(lspconfig, capabilities, on_attach) - -- lspconfig['pyright'].setup { - -- capabilities = capabilities, - -- on_attach = on_attach, - -- } - - lspconfig['basedpyright'].setup({ - capabilities = capabilities, - on_attach = on_attach, - settings = { - basedpyright = { - typeCheckingMode = 'standard', - }, - }, - }) - - lspconfig['ruff'].setup({ - capabilities = capabilities, - on_attach = on_attach, - }) - - lspconfig['groovyls'].setup({ - capabilities = capabilities, - on_attach = on_attach, - }) - - -- lspconfig['cmake'].setup { - -- capabilities = capabilities, - -- on_attach = on_attach, - -- } - - local clangd_capabilities = capabilities - clangd_capabilities.textDocument.semanticHighlighting = true - clangd_capabilities.offsetEncoding = { 'utf-16' } - lspconfig['clangd'].setup({ - capabilities = clangd_capabilities, - on_attach = on_attach, - cmd = { - 'clangd', - '--compile-commands-dir=build_nvim', - '--query-driver', - '/opt/cortex-a78-2022.08-gcc12.1-linux5.15/bin/aarch64-linux-gnu-g*', - '--clang-tidy', - '--background-index', - '--use-dirty-headers', - '--completion-style=detailed', - }, - root_dir = lspconfig.util.root_pattern('.clangd', '.clang-tidy', '.clang-format', 'compile_flags.txt', 'configure.ac', '.git', 'build_nvim'), - }) - - require('clangd_extensions').setup({ - extensions = { - inlay_hints = { - -- Only show inlay hints for the current line - only_current_line = true, - }, - }, - }) - - lspconfig['jsonls'].setup({ - capabilities = capabilities, - on_attach = on_attach, - }) - - -- lspconfig['rust_analyzer'].setup { - -- capabilities = capabilities, - -- on_attach = on_attach, - -- } - - lspconfig['lua_ls'].setup({ - capabilities = capabilities, - on_attach = on_attach, - settings = { - Lua = { - workspace = { - checkThirdParty = false, - }, - completion = { - callSnippet = 'Replace', - }, - -- Do not send telemetry data containing a randomized but unique identifier - telemetry = { - enable = false, - }, - format = { - enable = true, - defaultConfig = { - indent_style = 'space', - indent_size = '2', - quote_style = 'single', - }, - }, - }, - }, - }) - - lspconfig['dockerls'].setup({ - capabilities = capabilities, - on_attach = on_attach, - }) - - lspconfig['markdown_oxide'].setup({ - capabilities = capabilities, - on_attach = on_attach, - }) - - -- lspconfig['marksman'].setup { - -- capabilities = capabilities, - -- on_attach = on_attach, - -- } - - lspconfig['yamlls'].setup({ - capabilities = capabilities, - on_attach = on_attach, - settings = { - yaml = { - validate = true, - }, - }, - }) - - local configs = require('lspconfig.configs') - if not configs.neocmake then - configs.neocmake = { - default_config = { - cmd = { 'neocmakelsp', '--stdio' }, - filetypes = { 'cmake' }, - root_dir = function(fname) - return lspconfig.util.find_git_ancestor(fname) - end, - single_file_support = true, -- suggested - on_attach = on_attach, -- on_attach is the on_attach function you defined - }, - } - lspconfig.neocmake.setup({}) - end -end - -return M diff --git a/lua/plugins/lspconfig.lua b/lua/plugins/lspconfig.lua deleted file mode 100644 index 769e214..0000000 --- a/lua/plugins/lspconfig.lua +++ /dev/null @@ -1,74 +0,0 @@ -local lspKeys = function(client, bufnr) - local options = { noremap = true, silent = false, buffer = bufnr } - vim.keymap.set('n', ',', vim.diagnostic.goto_prev, vim.tbl_extend('error', options, { desc = 'Diag prev' })) - vim.keymap.set('n', ';', vim.diagnostic.goto_next, vim.tbl_extend('error', options, { desc = 'Diag next' })) - vim.keymap.set({ 'n', 'x' }, 'a', vim.lsp.buf.code_action, vim.tbl_extend('error', options, { desc = 'Code action' })) - vim.keymap.set('n', 'e', vim.lsp.buf.declaration, vim.tbl_extend('error', options, { desc = 'Declaration' })) - vim.keymap.set('n', 'h', function() - vim.lsp.buf.hover({ border = 'none' }) - end, vim.tbl_extend('error', options, { desc = 'Hover' })) - vim.keymap.set('n', 'c', vim.lsp.buf.outgoing_calls, vim.tbl_extend('error', options, { desc = 'Outgoing calls' })) - vim.keymap.set('n', 'C', vim.lsp.buf.incoming_calls, vim.tbl_extend('error', options, { desc = 'Incoming calls' })) - vim.keymap.set('n', 'm', vim.lsp.buf.rename, vim.tbl_extend('error', options, { desc = 'Rename' })) - vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, vim.tbl_extend('error', options, { desc = 'Type definition' })) - vim.keymap.set({ 'n', 'i', 'x' }, '', vim.lsp.buf.signature_help, vim.tbl_extend('error', options, { desc = 'Signature help' })) - vim.keymap.set('n', 'v', function() - vim.diagnostic.open_float({ border = 'rounded' }) - end, vim.tbl_extend('error', options, { desc = 'Diagnostics Float' })) - vim.keymap.set('n', '', 'ClangdSwitchSourceHeader', vim.tbl_extend('error', options, { desc = 'Switch Source/Header' })) - - if client.supports_method('inlayHintProvider') then - vim.keymap.set('n', 'i', function() - vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }), { bufnr = bufnr }) - end, { - noremap = true, - silent = false, - desc = 'Toggle inlay hints', - buffer = bufnr, - }) - end -end - -local on_attach = function(client, bufnr) - vim.api.nvim_set_option_value('omnifunc', 'v:lua.vim.lsp.omnifunc', { buf = 0 }) - vim.api.nvim_set_option_value('formatexpr', 'v:lua.vim.lsp.formatexpr()', { buf = 0 }) - - lspKeys(client, bufnr) - - if client.server_capabilities.documentSymbolProvider then - local navic = require('nvim-navic') - navic.attach(client, bufnr) - end -end - -return { - 'neovim/nvim-lspconfig', - dependencies = { - 'williamboman/mason.nvim', - 'p00f/clangd_extensions.nvim', - 'Fildo7525/pretty_hover', - { - 'creativenull/efmls-configs-nvim', - branch = 'main', - }, - 'SmiteshP/nvim-navic', - }, - version = nil, - branch = 'master', - config = function() - local lspconfig = require('lspconfig') - require('nvim-navic').setup({}) - - local capabilities = vim.lsp.protocol.make_client_capabilities() - capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) - -- capabilities = require('blink.cmp').get_lsp_capabilities(capabilities) - capabilities.workspace = { - didChangeWatchedFiles = { - dynamicRegistration = true, - }, - } - - require('plugins.lsp.server').setup_server(lspconfig, capabilities, on_attach) - end, - event = 'VeryLazy', -}