summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/lsp.lua
blob: cb4511d0eb9ff5b42f4f36e19b67e37ba4c35620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
return {
  {
    "neovim/nvim-lspconfig",
    dependencies = {
      "hrsh7th/cmp-nvim-lsp",
      "jose-elias-alvarez/null-ls.nvim",
    },
    event = { "BufReadPre", "BufNewFile" },
    config = function()
      local lspconfig = require("lspconfig")
      local capabilities = require("cmp_nvim_lsp").default_capabilities()
      local common = {
        capabilities = capabilities,
      }

      local lsps = {
        "rust_analyzer",
        "gopls",
        "lua_ls",
        "pylsp",
      }

      for _, lsp in ipairs(lsps) do
        lspconfig[lsp].setup(common)
      end

      local wk = require("which-key")
      wk.register({
        ["<leader>q"] = { vim.diagnostic.open_float, "Show diagnostics in a floating window" },
        ["[d"] = { vim.diagnostic.goto_prev, "Previous diagnostic" },
        ["]d"] = { vim.diagnostic.goto_next, "Next diagnostic" },
      })

      vim.api.nvim_create_autocmd("LspAttach", {
        group = vim.api.nvim_create_augroup("UserLspConfig", {}),
        callback = function(ev)
          wk.register({
            gD = { vim.lsp.buf.declaration, "Go to declaration" },
            gd = { vim.lsp.buf.definition, "Go to definition" },
            gr = { vim.lsp.buf.references, "Go to references" },
            gi = { vim.lsp.buf.implementation, "Go to implementation" },
            K = { vim.lsp.buf.hover, "Show LSP hover info" },
            ["<C-k>"] = { vim.lsp.buf.signature_help, "Show signature help" },
            ["<leader>D"] = { vim.lsp.buf.type_definition, "Type definition" },
            ["<leader>rn"] = { vim.lsp.buf.rename, "Rename" },
            ["<leader>ca"] = { vim.lsp.buf.code_action, "Code action" },
            ["<leader>F"] = {
              function()
                vim.lsp.buf.format({ async = true })
              end,
              "Format buffer",
            },
            ["<leader>w"] = {
              name = "+LSP workspace",
              a = { vim.lsp.buf.add_workspace_folder, "Add workspace folder" },
              r = { vim.lsp.buf.remove_workspace_folder, "Remove workspace folder" },
              l = {
                function()
                  print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
                end,
                "List workspace folders",
              },
            },
          }, {buffer=ev.buf})
        end,
      })

      local nls = require("null-ls")
      nls.setup({
        sources = {
          nls.builtins.formatting.stylua,
          nls.builtins.formatting.shfmt,
          nls.builtins.code_actions.gitsigns,
        },
      })
    end,
  },
}