|
| 1 | +local config = function() |
| 2 | + local vim = vim |
| 3 | + local nvim_lsp = require('lspconfig') |
| 4 | + |
| 5 | + -- Set up completion lspconfig. |
| 6 | + local capabilities = require('cmp_nvim_lsp').default_capabilities() |
| 7 | + |
| 8 | + |
| 9 | + -- Use an on_attach function to only map the following keys |
| 10 | + -- after the language server attaches to the current buffer |
| 11 | + local on_attach = function(client, bufnr) |
| 12 | + -- https://github.com/redhat-developer/yaml-language-server/issues/486#issuecomment-1046792026 |
| 13 | + -- if client.name == "yamlls" then |
| 14 | + -- client.server_capabilities.documentFormattingProvider = true |
| 15 | + -- end |
| 16 | + |
| 17 | + -- Enable completion triggered by <c-x><c-o> |
| 18 | + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') |
| 19 | + |
| 20 | + -- Mappings. |
| 21 | + -- See `:help vim.lsp.*` for documentation on any of the below functions |
| 22 | + local bufopts = { noremap = true, silent = true, buffer = bufnr } |
| 23 | + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) |
| 24 | + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) |
| 25 | + vim.keymap.set('n', 'L', vim.lsp.buf.hover, bufopts) |
| 26 | + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) |
| 27 | + vim.keymap.set('n', '<C-s>', vim.lsp.buf.signature_help, bufopts) |
| 28 | + vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts) |
| 29 | + vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts) |
| 30 | + vim.keymap.set('n', '<space>wl', function() |
| 31 | + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) |
| 32 | + end, bufopts) |
| 33 | + vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts) |
| 34 | + vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts) |
| 35 | + vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts) |
| 36 | + vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) |
| 37 | + -- vim.keymap.set('n', '[d', vim.lsp.diagnostic.goto_prev, bufopts) |
| 38 | + -- vim.keymap.set('n', ']d', vim.lsp.diagnostic.goto_next, bufopts) |
| 39 | + vim.keymap.set('n', '<space>f', function() vim.lsp.buf.format { async = true } end, bufopts) |
| 40 | + |
| 41 | + -- Format on save |
| 42 | + -- vim.api.nvim_create_autocmd("BufWritePre", { |
| 43 | + -- buffer = buffer, |
| 44 | + -- callback = function() |
| 45 | + -- if client.server_capabilities.documentFormattingProvider then |
| 46 | + -- vim.lsp.buf.format { async = false } |
| 47 | + -- end |
| 48 | + -- end |
| 49 | + -- }) |
| 50 | + end |
| 51 | + |
| 52 | + local servers = { |
| 53 | + "bashls", |
| 54 | + "dockerls", |
| 55 | + "gopls", |
| 56 | + "html", |
| 57 | + "jsonls", |
| 58 | + "lua_ls", |
| 59 | + "sourcekit", |
| 60 | + "taplo", |
| 61 | + "ts_ls", |
| 62 | + -- "yamlls", |
| 63 | + "marksman", |
| 64 | + -- "graphql", |
| 65 | + } |
| 66 | + for _, lsp in ipairs(servers) do |
| 67 | + nvim_lsp[lsp].setup { on_attach = on_attach, capabilities = capabilities } |
| 68 | + end |
| 69 | + |
| 70 | + require('lspconfig').rust_analyzer.setup { |
| 71 | + capabilities = capabilities, |
| 72 | + on_attach = on_attach, |
| 73 | + -- cmd = { "rustup", "run", "nightly", "rust-analyzer" }, |
| 74 | + settings = { |
| 75 | + ['rust-analyzer'] = { |
| 76 | + check = { |
| 77 | + -- allFeatures = true, |
| 78 | + disabled = { "unresolved-proc-macro" }, |
| 79 | + overrideCommand = { |
| 80 | + 'cargo', 'clippy', '--workspace', '--message-format=json', '--all-targets', '--all-features' |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + -- Ruby |
| 88 | + require('lspconfig').solargraph.setup({ |
| 89 | + capabilities = capabilities, |
| 90 | + on_attach = on_attach, |
| 91 | + settings = { |
| 92 | + solargraph = { |
| 93 | + completion = true, |
| 94 | + definitions = true, |
| 95 | + diagnostics = true, |
| 96 | + formatting = true, |
| 97 | + hover = true, |
| 98 | + references = true, |
| 99 | + rename = true, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + -- Elixir |
| 105 | + require 'lspconfig'.elixirls.setup { |
| 106 | + capabilities = capabilities, |
| 107 | + on_attach = on_attach, |
| 108 | + cmd = { "elixir-ls" }, |
| 109 | + } |
| 110 | + -- Python |
| 111 | + local home = os.getenv("HOME") |
| 112 | + local util = require('lspconfig').util |
| 113 | + require('lspconfig').pyright.setup({ |
| 114 | + capabilities = capabilities, |
| 115 | + on_attach = on_attach, |
| 116 | + cmd = { "pyright-langserver", "--stdio", "--verbose" }, |
| 117 | + -- rootMarkers = { "pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", "pyrightconfig.json", ".git" }, |
| 118 | + |
| 119 | + -- root_dir = vim.fs.dirname(vim.fs.find('.git', { path = root_dir, upward = true })[1]), |
| 120 | + |
| 121 | + root_dir = util.root_pattern(".git/", "pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", |
| 122 | + "pyrightconfig.json"), |
| 123 | + settings = { |
| 124 | + -- rootMarkers = { ".git/" }, |
| 125 | + -- rootMarkers = { ".git/", "pyproject.toml", "setup.py", "setup.cfg", "requirements.txt", "Pipfile", "pyrightconfig.json", }, |
| 126 | + python = { |
| 127 | + analysis = { |
| 128 | + pythonPath = home .. "/.pyenv/shims/python" |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }) |
| 133 | + |
| 134 | + |
| 135 | + require('lspconfig').ruff.setup({ |
| 136 | + init_options = { documentFormatting = true }, |
| 137 | + capabilities = capabilities, |
| 138 | + on_attach = on_attach, |
| 139 | + }) |
| 140 | + |
| 141 | + |
| 142 | + local shfmt = { |
| 143 | + formatCommand = "shfmt -ci -s -bn -i 2", |
| 144 | + formatStdin = true, |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + --https://github.com/lukas-reineke/dotfiles/blob/master/vim/lua/lsp/init.lua |
| 149 | + require("lspconfig").efm.setup { |
| 150 | + capabilities = capabilities, |
| 151 | + on_attach = on_attach, |
| 152 | + init_options = { documentFormatting = true }, |
| 153 | + root_dir = vim.loop.cwd, |
| 154 | + settings = { |
| 155 | + rootMarkers = { ".git/" }, |
| 156 | + languages = { |
| 157 | + zsh = { shfmt }, |
| 158 | + bash = { shfmt }, |
| 159 | + sh = { shfmt }, |
| 160 | + } |
| 161 | + }, |
| 162 | + filetypes = { 'zsh', 'sh', 'bash' } |
| 163 | + } |
| 164 | + |
| 165 | + require("lspconfig").jdtls.setup { |
| 166 | + capabilities = capabilities, |
| 167 | + on_attach = on_attach, |
| 168 | + cmd = { "/opt/homebrew/bin/jdtls", "--java-executable", "/opt/homebrew/opt/openjdk/bin/java", }, |
| 169 | + } |
| 170 | + |
| 171 | + |
| 172 | + vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( |
| 173 | + vim.lsp.diagnostic.on_publish_diagnostics, { |
| 174 | + underline = true, |
| 175 | + |
| 176 | + -- Enable/Disable virtual text diagnostics |
| 177 | + virtual_text = true, |
| 178 | + |
| 179 | + signs = true, |
| 180 | + update_in_insert = false, |
| 181 | + } |
| 182 | + ) |
| 183 | + |
| 184 | + vim.fn.sign_define("LspDiagnosticsSignError", { text = "!!" }) |
| 185 | + vim.fn.sign_define("LspDiagnosticsSignWarning", { text = "⚠" }) |
| 186 | + vim.fn.sign_define("LspDiagnosticsSignInformation", { text = "*" }) |
| 187 | + vim.fn.sign_define("LspDiagnosticsSignHint", { text = "☆" }) |
| 188 | + |
| 189 | + |
| 190 | + -- match the active buffer background color from _colors |
| 191 | + vim.cmd [[autocmd! ColorScheme * highlight NormalFloat guibg=#2E373D guifg=white]] |
| 192 | + vim.cmd [[autocmd! ColorScheme * highlight FloatBorder guifg=white guibg=#2E373D]] |
| 193 | + |
| 194 | + vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { |
| 195 | + border = "rounded", |
| 196 | + }) |
| 197 | +end |
| 198 | + |
| 199 | +return { |
| 200 | + { |
| 201 | + "neovim/nvim-lspconfig", |
| 202 | + dependencies = { |
| 203 | + { "hrsh7th/cmp-nvim-lsp" }, |
| 204 | + { "nvim-lua/lsp_extensions.nvim" }, |
| 205 | + }, |
| 206 | + config = config |
| 207 | + } |
| 208 | +} |
0 commit comments