summaryrefslogtreecommitdiffstats
path: root/lua/conf/plugins/mason-lspconfig.lua
blob: 65c5693a67107d94889a18ea43e38ed3cb82096e (plain) (blame)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
local misc = require('core.misc')
local lsp = require('core.lsp.functions')
local map, auto, augroup = misc.map, misc.auto, misc.augroup

return { 'williamboman/mason-lspconfig.nvim',
  requires = {
    'williamboman/mason.nvim',
    { 'neovim/nvim-lspconfig',
      disable = vim.version().minor < 8,
      function()
        lsp.setup()
      end
    }
  },
  function()
    local util = require('lspconfig.util')

    -- setup language servers
    require('mason-lspconfig').setup {
      ensure_installed = {
        "lua_ls",
        "clangd",
        "jdtls",
        "phpactor",
        "html",
        "cssls",
        "bashls",
        "zls"
        -- "asm-lsp", -- seems to be broken
      }
    }
    require('mason-lspconfig').setup_handlers {
      function(server_name)
        require('lspconfig')[server_name].setup {
          on_attach = lsp.lsp_attach,
          capabilities = lsp.capabilities()
        }
      end,

      -- setup luals
      ["lua_ls"] = function(server_name)
        local root_files = { '.luarc.json', '.luarc.jsonc', '.luacheckrc',
          '.stylua.toml', 'stylua.toml', 'selene.toml', 'selene.yml',
          'README.md'
        }

        -- FIXME: luals seems to start up twice and sends back twice the
        -- completions (one configured with the below settings and one without)
        require('lspconfig')[server_name].setup {
          on_attach = lsp.attach,
          capabilities = lsp.capabilities(),
          settings = {
            Lua = {
              diagnostics = {
                globals = { "vim", 'mp' }
              },
              runtime = {
                version = 'LuaJIT'
              },
              format = {
                enable = false
              },
              workspace = {
                checkThirdParty = false,
                library = {
                  vim.env.VIMRUNTIME
                }
              }
            }
          },

          root_dir = function(fname)
            local root = util.root_pattern(unpack(root_files))(fname)
            if root and root ~= vim.env.HOME then
              return root
            end

            root = util.root_pattern('lua/')(fname)
            if root then
              return root
            end
            return util.find_git_ancestor(fname)
          end
        }
      end,

      -- setup clangd
      ["clangd"] = function(server_name)
        require('lspconfig')[server_name].setup {
          on_attach = function(client, bufnr)
            lsp.attach(client, bufnr)

            -- add some clangd specific mappings
            local opts = { buffer = bufnr }
            map("n", "<leader>o", "<cmd>ClangdSwitchSourceHeader<CR>", opts)
          end,
          capabilities = lsp.capabilities(),

          cmd = {
            "clangd",
            "--background-index",
            "--clang-tidy",
            "--header-insertion=iwyu",
            "--completion-style=detailed",
            "--function-arg-placeholders",
            "--fallback-style=llvm"
          },
          init_options = {
            usePlaceholders = true,
            clangdFileStatus = true,
            fallback_flags = {
              "-xc" -- makes clangd think we're using c instead of c++
            }
          }
        }
      end,

      -- disable it, we start this using nvim-jdtls
      ["jdtls"] = function(_) end
    }

  end
}