From 67d18020f6ef18987aeb3fd634d1eea37a790135 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Fri, 21 Jul 2023 11:48:28 +0200 Subject: [PATCH] vim/lsp: simplify config, no functional changes --- config/nvim/lua/user/plugins/lsp.lua | 142 +++++++++++++-------------- 1 file changed, 67 insertions(+), 75 deletions(-) diff --git a/config/nvim/lua/user/plugins/lsp.lua b/config/nvim/lua/user/plugins/lsp.lua index cc526b1..30c2311 100644 --- a/config/nvim/lua/user/plugins/lsp.lua +++ b/config/nvim/lua/user/plugins/lsp.lua @@ -1,84 +1,76 @@ -local custom_server_opts = { - omnisharp = { - -- Support for showing unimported types and adding `using` directives. - enable_import_completion = true, - - -- Don't include preview versions of the .NET SDK. - sdk_include_prereleases = false, - }, - - lua_ls = { - settings = { - Lua = { - -- I'm using lua only inside neovim, so the runtime is LuaJIT. - runtime = { version = 'LuaJIT' }, - - -- Get the language server to recognize the `vim` global. - diagnostics = { globals = {'vim'} }, - - -- Make the server aware of Neovim runtime files. - workspace = { library = vim.api.nvim_get_runtime_file("", true) }, - - -- Do not send telemetry data containing a randomized but unique identifier - telemetry = { enable = false }, - }, - }, - } -} - -local custom_filetype_attach = { - cs = function(client, _ --[[bufnr]]) - client.server_capabilities.semanticTokensProvider = nil - end, -} - -local custom_attach = function(client, bufnr) - vim.bo.omnifunc = 'v:lua.vim.lsp.omnifunc' -- do completion with - - local map = vim.keymap.set - local opts = { buffer = bufnr } - - map('n', 'ca', vim.lsp.buf.code_action, opts) - map('n', 'cf', vim.lsp.buf.format, opts) - map('n', 'gD', vim.lsp.buf.declaration, opts) - map('n', 'gd', vim.lsp.buf.definition, opts) - map('n', 'K', vim.lsp.buf.hover, opts) - map('n', 'gi', vim.lsp.buf.implementation, opts) - map('n', 'grr', vim.lsp.buf.rename, opts) - map('n', 'gt', vim.lsp.buf.type_definition, opts) - - local filetype_attach = custom_filetype_attach[vim.bo.filetype] - if filetype_attach then filetype_attach(client, bufnr) end -end - -local custom_capabilities = function() +local config = function() local capabilities = vim.lsp.protocol.make_client_capabilities() - - local ok, cmp = pcall(require, 'cmp_nvim_lsp') - if ok then + local has_cmp_nvim_lsp, cmp = pcall(require, 'cmp_nvim_lsp') + if has_cmp_nvim_lsp then vim.tbl_deep_extend('force', capabilities, cmp.default_capabilities()) end - return capabilities -end + local on_attach = function(client, bufnr) + vim.bo.omnifunc = 'v:lua.vim.lsp.omnifunc' -- do completion with -local config = function() - local lsp = require 'lspconfig' - local capabilities = custom_capabilities() + local map = vim.keymap.set + local opts = { buffer = bufnr } - require('mason').setup() - require('mason-lspconfig').setup { - handlers = { - function(server) - local common_opts = { - on_attach = custom_attach, - capabilities = capabilities, - } - local server_opts = custom_server_opts[server] or {} - local opts = vim.tbl_extend('force', common_opts, server_opts) - lsp[server].setup(opts) - end, - } + map('n', 'ca', vim.lsp.buf.code_action, opts) + map('n', 'cf', vim.lsp.buf.format, opts) + map('n', 'gD', vim.lsp.buf.declaration, opts) + map('n', 'gd', vim.lsp.buf.definition, opts) + map('n', 'K', vim.lsp.buf.hover, opts) + map('n', 'gi', vim.lsp.buf.implementation, opts) + map('n', 'grr', vim.lsp.buf.rename, opts) + map('n', 'gt', vim.lsp.buf.type_definition, opts) + + -- Opt out of semantic highlighting because it has been casusing the issues + -- described here: https://github.com/williamboman/mason-lspconfig.nvim/issues/211#issuecomment-1528817490 + client.server_capabilities.semanticTokensProvider = nil + end + + require('mason').setup {} + require('mason-lspconfig').setup {} + require("mason-lspconfig").setup_handlers { + -- Default handler. + function(server) + require('lspconfig')[server].setup { + capabilities = capabilities, + on_attach = on_attach, + } + end, + + lua_ls = function() + require('lspconfig').lua_ls.setup { + capabilities = capabilities, + on_attach = on_attach, + + settings = { + Lua = { + -- I'm using lua only inside neovim, so the runtime is LuaJIT. + runtime = { version = 'LuaJIT' }, + + -- Get the language server to recognize the `vim` global. + diagnostics = { globals = {'vim'} }, + + -- Make the server aware of Neovim runtime files. + workspace = { library = vim.api.nvim_get_runtime_file("", true) }, + + -- Do not send telemetry data containing a randomized but unique identifier + telemetry = { enable = false }, + }, + }, + } + end, + + omnisharp = function() + require('lspconfig').omnisharp.setup { + capabilities = capabilities, + on_attach = on_attach, + + -- Show unimported types and add`using` directives. + enable_import_completion = true, + + -- Don't include preview versions of the .NET SDK. + sdk_include_prereleases = false, + } + end, } end