diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2022-11-23 15:38:16 +0100 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2022-11-23 15:38:16 +0100 |
commit | 99630ae8233f8a2afb6a89016b24a354b0186a21 (patch) | |
tree | 01ccbb1e8843ce1dbc9383579575dc48eef60f2c /lua/user/core/lsp | |
parent | 904acb28247999214aefd33a0cd3ddcea99d7dc0 (diff) | |
download | vim-99630ae8233f8a2afb6a89016b24a354b0186a21.zip vim-99630ae8233f8a2afb6a89016b24a354b0186a21.tar.gz |
plugins -> subdir core
Diffstat (limited to 'lua/user/core/lsp')
-rw-r--r-- | lua/user/core/lsp/handlers.lua | 94 | ||||
-rw-r--r-- | lua/user/core/lsp/init.lua | 3 | ||||
-rw-r--r-- | lua/user/core/lsp/mason.lua | 63 | ||||
-rw-r--r-- | lua/user/core/lsp/null-ls.lua | 20 | ||||
-rw-r--r-- | lua/user/core/lsp/settings/sumneko_lua.lua | 16 |
5 files changed, 196 insertions, 0 deletions
diff --git a/lua/user/core/lsp/handlers.lua b/lua/user/core/lsp/handlers.lua new file mode 100644 index 0000000..853beb3 --- /dev/null +++ b/lua/user/core/lsp/handlers.lua @@ -0,0 +1,94 @@ +local M = {} + +local cmp_nvim_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp") +if not cmp_nvim_ok then + print "cpm_nvim_lsp init failed" + return +end + +M.setup = function() + local signs = { Error = "", Warn = "", Hint = "", Info = "" } + for type, icon in pairs(signs) do + local hl = "DiagnosticSign" .. type + vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl }) + end + + local config = { + -- disable virtual text + virtual_text = false, + -- show signs + signs = { + active = signs, + }, + update_in_insert = false, + underline = true, + severity_sort = true, + float = { + focusable = false, + style = "minimal", + border = "rounded", + source = "always", + header = "", + prefix = "", + }, + } + + vim.diagnostic.config(config) + + vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { + border = "rounded", + }) + + vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { + border = "rounded", + }) +end + +local function lsp_highlight(client) + -- Set autocommands conditional on server_capabilities + if client.server_capabilities.document_highlight then + vim.api.nvim_exec( + [[ + augroup lsp_document_highlight + autocmd! * <buffer> + autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight() + autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references() + augroup END + ]], + false + ) + end +end + +local keymap = vim.keymap.set + +local function lsp_keymaps(bufnr) + local opts = { buffer = bufnr, noremap = true, silent = true } + -- SEE : https://neovim.io/doc/user/lsp.html + keymap("n", "gd", vim.lsp.buf.definition, opts) + keymap("n", "gr", vim.lsp.buf.references, opts) + keymap("n", "gi", vim.lsp.buf.implementation, opts) + keymap("n", "ga", vim.lsp.buf.declaration, opts) + + keymap("n", "gb", vim.lsp.buf.hover, opts) + keymap("n", "gé", vim.lsp.buf.signature_help, opts) + keymap("n", "gk", vim.diagnostic.open_float, opts) + keymap("n", "g.", vim.diagnostic.goto_next, opts) + keymap("n", "gx", vim.diagnostic.goto_prev, opts) + keymap("n", "<leader>q", vim.diagnostic.setloclist, opts) + vim.cmd [[ command! Format execute 'lua vim.lsp.buf.formatting()' ]] -- FIXME what ? +end + +M.on_attach = function(client, bufnr) + if client.name == "tsserver" then + client.server_capabilities.document_formatting = false + end + lsp_keymaps(bufnr) + lsp_highlight(client) +end + +local capabilities = vim.lsp.protocol.make_client_capabilities() + +M.capabilities = cmp_nvim_lsp.default_capabilities(capabilities) + +return M diff --git a/lua/user/core/lsp/init.lua b/lua/user/core/lsp/init.lua new file mode 100644 index 0000000..a95a5ca --- /dev/null +++ b/lua/user/core/lsp/init.lua @@ -0,0 +1,3 @@ +require 'user.core.lsp.mason' +require('user.core.lsp.handlers').setup() +require 'user.core.lsp.null-ls' diff --git a/lua/user/core/lsp/mason.lua b/lua/user/core/lsp/mason.lua new file mode 100644 index 0000000..9708ec8 --- /dev/null +++ b/lua/user/core/lsp/mason.lua @@ -0,0 +1,63 @@ +local mason_ok, mason = pcall(require, "mason") +if not mason_ok then + print "mason init failed" + return +end + +local mason_lspconfig_ok, mason_lspconfig = pcall(require, "mason-lspconfig") +if not mason_lspconfig_ok then + print "mason-lspconfig init failed" + return +end + +local lspconfig_ok, lspconfig = pcall(require, "lspconfig") +if not lspconfig_ok then + print "lspconfig init failed" + return +end + +require('lspconfig.ui.windows').default_options.border = 'rounded' + +local servers = { + "clangd", + "jdtls", + "jsonls", + "ltex", + "rust_analyzer", + "solargraph", + "sumneko_lua", +} + +mason.setup({ + ui = { + border = "single", + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗" + } + } +}) + +mason_lspconfig.setup({ + ensure_installed = servers, +}) + +mason_lspconfig.setup_handlers({ + function(server_name) + local opts = { + on_attach = require("user.core.lsp.handlers").on_attach, + capabilities = require("user.core.lsp.handlers").capabilities, + falgs = { + debounce_text_changes = 5000, + }, + } + + local require_ok, server = pcall(require, "user.core.lsp.settings." .. server_name) + if require_ok then + opts = vim.tbl_deep_extend("force", server, opts) + end + + lspconfig[server_name].setup(opts) + end, +}) diff --git a/lua/user/core/lsp/null-ls.lua b/lua/user/core/lsp/null-ls.lua new file mode 100644 index 0000000..389e68f --- /dev/null +++ b/lua/user/core/lsp/null-ls.lua @@ -0,0 +1,20 @@ +local null_ls_ok, null_ls = pcall(require, "null-ls") +if not null_ls_ok then + print "null-ls init failed" + return +end + +-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/formatting +local formatting = null_ls.builtins.formatting +-- https://github.com/jose-elias-alvarez/null-ls.nvim/tree/main/lua/null-ls/builtins/diagnostics +local diagnostics = null_ls.builtins.diagnostics + +null_ls.setup({ + debug = false, + sources = { + formatting.prettier.with({ extra_args = { "--no-semi", "--single-quote", "--jsx-single-quote" } }), + formatting.black.with({ extra_args = { "--fast" } }), + formatting.stylua, + -- diagnostics.flake8 + }, +}) diff --git a/lua/user/core/lsp/settings/sumneko_lua.lua b/lua/user/core/lsp/settings/sumneko_lua.lua new file mode 100644 index 0000000..d7e82b4 --- /dev/null +++ b/lua/user/core/lsp/settings/sumneko_lua.lua @@ -0,0 +1,16 @@ +return { + settings = { + + Lua = { + diagnostics = { + globals = { "vim" }, -- teach about vim global variable + }, + workspace = { + library = { + [vim.fn.expand("$VIMRUNTIME/lua")] = true, + [vim.fn.stdpath("config") .. "/lua"] = true, + }, + }, + }, + }, +} |