From db91953a2a5789f2996e57ab857e08229de884ab Mon Sep 17 00:00:00 2001 From: zymon Date: Thu, 29 Aug 2024 21:29:28 +0200 Subject: [PATCH] lsp init --- lua/znvim/plugins/cmp.lua | 10 ++- lua/znvim/plugins/lsp.lua | 159 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 lua/znvim/plugins/lsp.lua diff --git a/lua/znvim/plugins/cmp.lua b/lua/znvim/plugins/cmp.lua index 9822c41..8f881b4 100644 --- a/lua/znvim/plugins/cmp.lua +++ b/lua/znvim/plugins/cmp.lua @@ -6,6 +6,8 @@ return { "https://github.com/hrsh7th/cmp-buffer", "https://github.com/hrsh7th/cmp-path", "https://github.com/hrsh7th/cmp-cmdline", + "https://github.com/hrsh7th/cmp-nvim-lsp", + "https://github.com/hrsh7th/cmp-nvim-lsp-signature-help", }, event = { "InsertEnter", "CmdlineEnter", "BufReadPost" }, config = function() @@ -20,10 +22,10 @@ return { [""] = cmp.mapping.abort(), }), sources = cmp.config.sources({ - { - name = "buffer", - option = { keyword_length = 3 }, - }, + { name = "buffer", option = { keyword_length = 3 }, }, + { name = "path" }, + { name = "nvim_lsp" }, + { name = 'nvim_lsp_signature_help' }, }), window = { completion = cmp.config.window.bordered(), diff --git a/lua/znvim/plugins/lsp.lua b/lua/znvim/plugins/lsp.lua new file mode 100644 index 0000000..f6a29f8 --- /dev/null +++ b/lua/znvim/plugins/lsp.lua @@ -0,0 +1,159 @@ +return { + { -- lsp configurations + "https://github.com/neovim/nvim-lspconfig", + event = "BufReadPre", + dependencies = { + "https://github.com/williamboman/mason.nvim", + "https://github.com/williamboman/mason-lspconfig.nvim", + }, + config = function() + local lspconfig = require("lspconfig") + vim.diagnostic.config({ + float = { + border = "rounded", + }, + virtual_text = false, + virtual_lines = { + only_current_line = true, + }, + }) + local opts = { noremap = true, silent = true } + vim.keymap.set("n", "e", vim.diagnostic.open_float, opts) + vim.keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) + vim.keymap.set("n", "]d", vim.diagnostic.goto_next, opts) + -- vim.keymap.set('n', 'q', vim.diagnostic.setloclist, opts) + + local on_attach = function(_, bufnr) + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") + + local function bufopts(desc) + return { buffer = bufnr, noremap = true, silent = true, desc = desc } + end + + vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts("go to definition")) + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, bufopts("go to declaration")) + vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts("hover")) + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts("go to implementation")) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, bufopts("help signature")) + vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts("references")) + vim.keymap.set("n", "D", vim.lsp.buf.type_definition, bufopts("type definition")) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, bufopts("rename")) + vim.keymap.set("n", "ca", vim.lsp.buf.code_action, bufopts("code action")) + vim.keymap.set("n", "ff", function() + vim.lsp.buf.format({ async = true }) + end, bufopts("format code")) + end + + local capabilities = vim.lsp.protocol.make_client_capabilities() + + capabilities.textDocument.foldingRange = { + dynamicRegistration = false, + lineFoldingOnly = true, + } + + lspconfig.bashls.setup({ + on_attach = on_attach, + capabilities = capabilities, + }) + + lspconfig.texlab.setup({ + on_attach = on_attach, + capabilities = capabilities, + }) + + lspconfig.typst_lsp.setup({ + on_attach = on_attach, + capabilities = capabilities, + }) + + lspconfig.elixirls.setup({ + cmd = { "/usr/lib/elixir-ls/language_server.sh" }, + on_attach = on_attach, + capabilities = capabilities, + }) + + lspconfig.pyright.setup({ + on_attach = on_attach, + capabilities = capabilities, + }) + + lspconfig.rust_analyzer.setup({ + on_attach = on_attach, + capabilities = capabilities, + settings = { + ["rust-analyzer"] = { + diagnostics = { + enable = true, + }, + }, + }, + }) + + -- Trzeba stworzyć odpowiedniego enva i zainstalować paczki + lspconfig.julials.setup({ + -- This just adds dirname(fname) as a fallback (see nvim-lspconfig#1768). + on_new_config = function(new_config, _) + -- https://discourse.julialang.org/t/neovim-languageserver-jl/37286/83 + local julia = vim.fn.expand("~/.local/share/julia/environments/nvim-lspconfig/bin/julia") + if require("lspconfig").util.path.is_file(julia) then + new_config.cmd[1] = julia + end + end, + root_dir = function(fname) + local util = require("lspconfig.util") + return util.root_pattern("Project.toml")(fname) + or util.find_git_ancestor(fname) + or util.path.dirname(fname) + end, + on_attach = on_attach, + capabilities = capabilities, + settings = { + julia = { + lint = { + call = false, + }, + }, + } + }) + + lspconfig.clangd.setup({ + on_attach = on_attach, + capabilities = capabilities, + }) + + lspconfig.lua_ls.setup({ + on_attach = on_attach, + capabilities = capabilities, + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + telemetry = { + enable = false, + }, + }, + }, + }) + end, + }, + + { -- portable package manager + "https://github.com/williamboman/mason.nvim", + cmd = "Mason", + keys = { + { "pm", "Mason", desc = "Mason" }, + }, + opts = { + ui = { + border = "double", + icons = { + package_installed = "✓", + package_pending = "➜", + package_uninstalled = "✗", + }, + }, + }, + }, +}