From fb07fbdefd9cbe7d3d8a185812d0a930d7ddc4ab Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Sun, 7 Jul 2024 17:54:36 +0300 Subject: Neovim: reworked lsp setup, along with some minor tweaks --- .config/nvim/lua/custom/completion.lua | 0 .config/nvim/lua/custom/lsp.lua | 99 ++++++++++++++++++++++ .../nvim/lua/custom/plugins/TODO-completion.lua | 50 +++++++++++ .../nvim/lua/custom/plugins/TODO-completions.lua | 50 ----------- .config/nvim/lua/custom/plugins/TODO-lsp.lua | 48 ----------- .config/nvim/lua/custom/plugins/lsp.lua | 30 +++++++ .config/nvim/lua/custom/plugins/utility.lua | 6 -- 7 files changed, 179 insertions(+), 104 deletions(-) create mode 100644 .config/nvim/lua/custom/completion.lua create mode 100644 .config/nvim/lua/custom/lsp.lua create mode 100644 .config/nvim/lua/custom/plugins/TODO-completion.lua delete mode 100644 .config/nvim/lua/custom/plugins/TODO-completions.lua delete mode 100644 .config/nvim/lua/custom/plugins/TODO-lsp.lua create mode 100644 .config/nvim/lua/custom/plugins/lsp.lua (limited to '.config/nvim/lua/custom') diff --git a/.config/nvim/lua/custom/completion.lua b/.config/nvim/lua/custom/completion.lua new file mode 100644 index 0000000..e69de29 diff --git a/.config/nvim/lua/custom/lsp.lua b/.config/nvim/lua/custom/lsp.lua new file mode 100644 index 0000000..1238001 --- /dev/null +++ b/.config/nvim/lua/custom/lsp.lua @@ -0,0 +1,99 @@ +-- NOTE: A list of language servers (and potentially their config) +-- Accepts either true to enable, false to disable or a table with settings +local servers = { + 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 +} + +-- NOTE: Not language servers but still make sure mason installs them +local ensure_installed = { + stylua = true, -- lua formatting +} + +local capabilities = nil +if pcall(require, 'cmp_nvim_lsp') then + capabilities = require('cmp_nvim_lsp').default_capabilities() +end + +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)) + +require('mason').setup() +vim.list_extend(ensure_installed, servers_to_install) +require('mason-lspconfig').setup({ ensure_installed = servers_to_install }) + +local lspconfig = require('lspconfig') +for name, config in pairs(servers) do + if config == true then + ---@diagnostic disable-next-line: cast-local-type + config = {} + end + 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') + + -- TODO: Check if I need this line + -- vim.opt_local.omnifunc = "v:lua.vim.lsp.omnifunc" + + 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 + end, +}) + +-- TODO: Understand what this is +-- -- 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, +-- }) +-- +-- require('lsp_lines').setup() +-- vim.diagnostic.config({ virtual_text = false }) diff --git a/.config/nvim/lua/custom/plugins/TODO-completion.lua b/.config/nvim/lua/custom/plugins/TODO-completion.lua new file mode 100644 index 0000000..0699e9b --- /dev/null +++ b/.config/nvim/lua/custom/plugins/TODO-completion.lua @@ -0,0 +1,50 @@ +return { + { + 'hrsh7th/cmp-nvim-lsp', + }, + { + 'L3MON4D3/LuaSnip', + dependencies = { + 'saadparwaiz1/cmp_luasnip', + 'rafamadriz/friendly-snippets', + }, + }, + { + 'hrsh7th/nvim-cmp', + opts = function(_, opts) + -- Setup options for lazydev.nvim + opts.sources = opts.sources or {} + table.insert(opts.sources, { + name = 'lazydev', + group_index = 0, + }) + end, + config = function() + local cmp = require('cmp') + require('luasnip.loaders.from_vscode').lazy_load() + + cmp.setup({ + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + mapping = cmp.mapping.preset.insert({ + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = true }), + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }, { { name = 'buffer' } }), + }) + end, + }, +} diff --git a/.config/nvim/lua/custom/plugins/TODO-completions.lua b/.config/nvim/lua/custom/plugins/TODO-completions.lua deleted file mode 100644 index 0699e9b..0000000 --- a/.config/nvim/lua/custom/plugins/TODO-completions.lua +++ /dev/null @@ -1,50 +0,0 @@ -return { - { - 'hrsh7th/cmp-nvim-lsp', - }, - { - 'L3MON4D3/LuaSnip', - dependencies = { - 'saadparwaiz1/cmp_luasnip', - 'rafamadriz/friendly-snippets', - }, - }, - { - 'hrsh7th/nvim-cmp', - opts = function(_, opts) - -- Setup options for lazydev.nvim - opts.sources = opts.sources or {} - table.insert(opts.sources, { - name = 'lazydev', - group_index = 0, - }) - end, - config = function() - local cmp = require('cmp') - require('luasnip.loaders.from_vscode').lazy_load() - - cmp.setup({ - snippet = { - expand = function(args) - require('luasnip').lsp_expand(args.body) - end, - }, - window = { - completion = cmp.config.window.bordered(), - documentation = cmp.config.window.bordered(), - }, - mapping = cmp.mapping.preset.insert({ - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), - }), - sources = cmp.config.sources({ - { name = 'nvim_lsp' }, - { name = 'luasnip' }, - }, { { name = 'buffer' } }), - }) - end, - }, -} diff --git a/.config/nvim/lua/custom/plugins/TODO-lsp.lua b/.config/nvim/lua/custom/plugins/TODO-lsp.lua deleted file mode 100644 index 74b9ed4..0000000 --- a/.config/nvim/lua/custom/plugins/TODO-lsp.lua +++ /dev/null @@ -1,48 +0,0 @@ -return { - { - 'williamboman/mason.nvim', - lazy = false, - config = function() - require('mason').setup() - end, - }, - { - 'williamboman/mason-lspconfig.nvim', - lazy = false, - config = function() - require('mason-lspconfig').setup({ - ensure_installed = { - 'lua_ls', - 'clangd', - 'gradle_ls', - 'jdtls', - 'kotlin_language_server', - 'pyright', - 'rust_analyzer', - 'taplo', - 'bashls', - 'lemminx', - 'yamlls', - }, - }) - end, - }, - { - 'neovim/nvim-lspconfig', - lazy = false, - config = function() - local lspconfig = require('lspconfig') - lspconfig.lua_ls.setup({}) - lspconfig.clangd.setup({}) - lspconfig.gradle_ls.setup({}) - lspconfig.jdtls.setup({}) - lspconfig.kotlin_language_server.setup({}) - lspconfig.pyright.setup({}) - lspconfig.rust_analyzer.setup({}) - lspconfig.taplo.setup({}) - lspconfig.bashls.setup({}) - lspconfig.lemminx.setup({}) - lspconfig.yamlls.setup({}) - end, - }, -} diff --git a/.config/nvim/lua/custom/plugins/lsp.lua b/.config/nvim/lua/custom/plugins/lsp.lua new file mode 100644 index 0000000..e555ace --- /dev/null +++ b/.config/nvim/lua/custom/plugins/lsp.lua @@ -0,0 +1,30 @@ +return { + { + 'neovim/nvim-lspconfig', + lazy = false, + dependencies = { + 'williamboman/mason.nvim', + 'williamboman/mason-lspconfig.nvim', + + { + 'folke/lazydev.nvim', + ft = 'lua', + opts = { library = { { path = 'luvit-meta/library', words = { 'vim%.uv' } } } }, + }, + + -- FIXME: Starts with a black blackground + -- Desired behaviour is same color as background or transparent + -- Progress ui + { 'j-hui/fidget.nvim', opts = {} }, + + -- AUtoformatting + -- 'stevear/conform.nvim', + + -- Schema info + -- 'b0o/SchemaStore.nvim', + }, + config = function() + require('custom.lsp') + end, + }, +} diff --git a/.config/nvim/lua/custom/plugins/utility.lua b/.config/nvim/lua/custom/plugins/utility.lua index 41f2925..79eac83 100644 --- a/.config/nvim/lua/custom/plugins/utility.lua +++ b/.config/nvim/lua/custom/plugins/utility.lua @@ -37,12 +37,6 @@ return { vim.keymap.set('n', 'nc', ':Telescope neoclip plus', { desc = '[N]eo[c]lip History' }) end, }, - { 'Bilal2453/luvit-meta', lazy = true }, - { - 'folke/lazydev.nvim', - ft = 'lua', - opts = { library = { { path = 'luvit-meta/library', words = { 'vim%.uv' } } } }, - }, { 'mbbill/undotree', config = function() -- cgit v1.3.1