diff options
Diffstat (limited to '.config/nvim/lua/custom/configs')
| -rw-r--r-- | .config/nvim/lua/custom/configs/language-servers.lua | 16 | ||||
| -rw-r--r-- | .config/nvim/lua/custom/configs/lsp-keymaps.lua | 58 | ||||
| -rw-r--r-- | .config/nvim/lua/custom/configs/lsp.lua | 63 | ||||
| -rw-r--r-- | .config/nvim/lua/custom/configs/mason.lua | 33 |
4 files changed, 170 insertions, 0 deletions
diff --git a/.config/nvim/lua/custom/configs/language-servers.lua b/.config/nvim/lua/custom/configs/language-servers.lua new file mode 100644 index 0000000..78dd6e5 --- /dev/null +++ b/.config/nvim/lua/custom/configs/language-servers.lua @@ -0,0 +1,16 @@ +-- NOTE: A list of language servers (and potentially their config) +-- Accepts either true to enable, false to disable or a table with settings +return { + lua_ls = true, -- lua + clangd = true, -- C/C++ + gradle_ls = true, -- java package manager + jdtls = true, -- java eclipse lsp + kotlin_language_server = true, -- kotlin + pyright = true, -- python + rust_analyzer = true, -- rust + bashls = true, -- bash + taplo = true, -- toml + lemminx = true, -- xml + yamlls = true, -- yaml + jsonls = true, -- json +} diff --git a/.config/nvim/lua/custom/configs/lsp-keymaps.lua b/.config/nvim/lua/custom/configs/lsp-keymaps.lua new file mode 100644 index 0000000..f43a03e --- /dev/null +++ b/.config/nvim/lua/custom/configs/lsp-keymaps.lua @@ -0,0 +1,58 @@ +local severity = vim.diagnostic.severity +local function next_diagnostic(diagnostic_severity) + return function() + require('lspsaga.diagnostic'):goto_next({ severity = diagnostic_severity }) + end +end +local function prev_diagnostic(diagnostic_severity) + return function() + require('lspsaga.diagnostic'):goto_prev({ severity = diagnostic_severity }) + end +end +local function cursor_diagnostics() + vim.diagnostic.open_float({ + scope = 'cursor', + border = 'single', + }) +end + +local keymaps = { + { 'n', 'gd', '<cmd>Telescope lsp_definitions<cr>', { desc = '[G]oto [D]efinition' } }, + { 'n', 'gu', '<cmd>Telescope lsp_references<cr>', { desc = '[G]oto [U]sages' } }, + { 'n', 'gi', '<cmd>Telescope lsp_implementations<cr>', { desc = '[G]oto [I]mplementations' } }, + { 'n', 'gt', '<cmd>Telescope lsp_type_definitions<cr>', { desc = '[G]oto [T]ype Definitions' } }, + { 'n', '<leader>ds', '<cmd>Telescope lsp_document_symbols<cr>', { desc = '[D]ocument [S]symbols' } }, + { 'n', '<leader>ps', '<cmd>Telescope lsp_workspace_symbols<cr>', { desc = '[P]roject [S]ymbols' } }, + { { 'n', 'i' }, '<C-p>', vim.lsp.buf.signature_help, { desc = 'Show [P]arameters' } }, + { 'n', 'K', '<cmd>Lspsaga hover_doc<cr>', { desc = 'Documentation' } }, + { 'n', 'R', '<cmd>Lspsaga rename<cr>', { desc = '[R]ename' } }, + { { 'n', 'i' }, '<M-Enter>', '<cmd>Lspsaga code_action<cr>', { desc = 'Code Actions' } }, + { 'n', '<leader>ca', '<cmd>Lspsaga code_action<cr><Esc>', { desc = '[C]ode [A]ctions' } }, + { 'n', 'ge', next_diagnostic(severity.ERROR), { desc = '[G]oto [E]rror' } }, + { 'n', 'gE', prev_diagnostic(severity.ERROR), { desc = '[G]oto [E]rror (prev)' } }, + { 'n', 'gw', next_diagnostic(severity.ERROR), { desc = '[G]oto [W]arning' } }, + { 'n', 'gW', prev_diagnostic(severity.ERROR), { desc = '[G]oto [W]arning (prev)' } }, + { 'n', '<leader>D', cursor_diagnostics, { desc = '[D]iagnostics under cursor' } }, +} + +local M = {} + +--- Registers LSP keymaps for the given buffer +---@param bufnr number: the id of the buffer +M.setup = function(bufnr) + for _, keymap in ipairs(keymaps) do + local mode = keymap[1] + local lhs = keymap[2] + local rhs = keymap[3] + local opts = keymap[4] + + if not opts then + opts = {} + end + + opts.buffer = bufnr + vim.keymap.set(mode, lhs, rhs, opts) + end +end + +return M diff --git a/.config/nvim/lua/custom/configs/lsp.lua b/.config/nvim/lua/custom/configs/lsp.lua new file mode 100644 index 0000000..afae5af --- /dev/null +++ b/.config/nvim/lua/custom/configs/lsp.lua @@ -0,0 +1,63 @@ +local capabilities = require('cmp_nvim_lsp').default_capabilities() +local servers = require('custom.configs.language-servers') + +local lspconfig = require('lspconfig') +for name, config in pairs(servers) do + if config == true then + ---@diagnostic disable-next-line: cast-local-type + config = {} + end + ---@diagnostic disable-next-line: cast-local-type + config = vim.tbl_deep_extend('force', {}, { + capabilities = capabilities, + }, config) + + lspconfig[name].setup(config) +end + +vim.api.nvim_create_autocmd('LspAttach', { + callback = function(args) + local bufnr = args.buf + local client = assert(vim.lsp.get_client_by_id(args.data.client_id), 'must have valid client') + + local settings = servers[client.name] + if type(settings) ~= 'table' then + ---@diagnostic disable-next-line: cast-local-type + settings = {} + end + + -- Override server capabilities + if settings.server_capabilities then + for k, v in pairs(settings.server_capabilities) do + if v == vim.NIL then + v = nil + end + client.server_capabilities[k] = v + end + end + + -- Register LSP keymaps local to buffer + require('custom.configs.lsp-keymaps').setup(bufnr) + end, +}) + +-- TODO: Decide if you want to use conform or not +-- +-- -- Autoformatting Setup +-- require('conform').setup({ +-- formatters_by_ft = { +-- lua = { 'stylua' }, +-- }, +-- }) +-- +-- vim.api.nvim_create_autocmd('BufWritePre', { +-- callback = function(args) +-- require('conform').format({ +-- bufnr = args.buf, +-- lsp_fallback = true, +-- quiet = true, +-- }) +-- end, +-- }) + +vim.diagnostic.config({ virtual_text = false }) diff --git a/.config/nvim/lua/custom/configs/mason.lua b/.config/nvim/lua/custom/configs/mason.lua new file mode 100644 index 0000000..d7a537b --- /dev/null +++ b/.config/nvim/lua/custom/configs/mason.lua @@ -0,0 +1,33 @@ +-- NOTE: Not language servers but still make sure mason installs them +local ensure_installed = { + stylua = true, -- lua formatting +} +local servers = require('custom.configs.language-servers') + +require('mason').setup({ + ui = { + icons = { + package_installed = '✓', + package_pending = '➜', + package_uninstalled = '✗', + }, + }, +}) + +local servers_to_install = vim.tbl_filter(function(key) + local server = servers[key] + if type(server) == 'table' then + return true + else + return server + end +end, vim.tbl_keys(servers)) + +vim.list_extend(ensure_installed, servers_to_install) +require('mason-lspconfig').setup({ ensure_installed = servers_to_install }) + +require('mason-tool-installer').setup({ + ensure_installed = { + 'stylua', -- lua formatter + }, +}) |
