aboutsummaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/lsp.lua
blob: 1063c5da744cdc9469dcaf2694b497cb12febe79 (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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
local icons = require("icons")

-- this is horrible and it should be redone someday
-- but that day is not today

return {
    {
        "neovim/nvim-lspconfig",
        -- enabled = false,
        event = { "BufReadPre", "BufNewFile" },
        dependencies = {
            { "williamboman/mason.nvim", config = true, lazy = false },
            { "mason-org/mason-lspconfig.nvim", opts = { automatic_enable = false } },
            "hrsh7th/cmp-nvim-lsp",
        },
        opts = {
            diagnostics = {
            },
            servers = {
                lua_ls = {
                    settings = {
                        Lua = {
                            -- workspace = {
                            -- 	checkThirdParty = false,
                            -- 	library = {
                            -- 		vim.env.VIMRUNTIME,
                            -- 	},
                            -- },
                            completion = {
                                showWord = "Disable",
                                displayContext = 8,
                            },
                            hint = {
                                enable = true,
                            },
                            diagnostics = { "trailing-space" },
                        },
                    },
                },
                rust_analyzer = { settings = { completion = { fullFunctionSignatures = true } } },
                ts_ls = {},
            },
            setup = {}
        },
        config = function(_, opts)
            local function on_attach(fn)
                vim.api.nvim_create_autocmd("LspAttach", {
                    callback = function(args)
                        local buffer = args.buf
                        local client = vim.lsp.get_client_by_id(args.data.client_id)
                        fn(client, buffer)
                    end,
                })
            end

            ---@param method string
            ---@param fn fun(client:vim.lsp.Client, buffer)
            local function on_supports_method(method, fn)
                -- cache[method] = cache[method] or setmetatable({}, { __mode = "k" })
                return vim.api.nvim_create_autocmd("User", {
                    pattern = "LspSupportsMethod",
                    callback = function(args)
                        local client = vim.lsp.get_client_by_id(args.data.client_id)
                        local buffer = args.data.buffer ---@type number
                        if client and method == args.data.method then
                            return fn(client, buffer)
                        end
                    end,
                })
            end

            -- function _check_methods(client, buffer)
            --     if 
            --         -- don't trigger on invalid buffers
            --         not vim.api.nvim_buf_is_valid(buffer) or
            --         -- don't trigger on non-listed buffers
            --         vim.bo[buffer].buflisted or
            --         -- don't trigger on nofile buffers
            --         vim.bo[buffer].buftype == "nofile"
            --     then
            --         return
            --     end
            --     for method, clients in pairs(M._supports_method) do
            --         clients[client] = clients[client] or {}
            --         if not clients[client][buffer] then
            --             if client.supports_method and client.supports_method(method, { bufnr = buffer }) then
            --                 clients[client][buffer] = true
            --                 vim.api.nvim_exec_autocmds("User", {
            --                     pattern = "LspSupportsMethod",
            --                     data = { client_id = client.id, buffer = buffer, method = method },
            --                 })
            --             end
            --         end
            --     end
            -- end

            on_supports_method("textDocument/inlayHint", function(client, buffer)
                if 
                    vim.api.nvim_buf_is_valid(buffer)
                    and vim.bo[buffer].buftype == ""
                    and not vim.tbl_contains(opts.inlay_hints.exclude, vim.bo[buffer].filetype)
                then
                    vim.lsp.inlay_hint.enable(true, { bufnr = buffer })
                end
            end)

            vim.diagnostic.config({
                underline = true,
                update_in_insert = false,
                virtual_text = {
                    spacing = 4,
                    source = "if_many",
                    prefix = function(diagnostic)
                        for name, icon in pairs(icons.diagnostics) do
                            if diagnostic.severity == vim.diagnostic.severity[name:upper()] then
                                return icon
                            end
                        end
                    end

                },
                severity_sort = true,
            })

            local servers = opts.servers
            local cmp_nvim_lsp = require "cmp_nvim_lsp"
            local capabilities = vim.tbl_deep_extend(
                "force",
                {},
                vim.lsp.protocol.make_client_capabilities(),
                cmp_nvim_lsp.default_capabilities()
            )

            local function setup(server)
                local server_opts = vim.tbl_deep_extend("force", {
                    capabilities = vim.deepcopy(capabilities),
                }, servers[server] or {})

                if opts.setup[server] then
                    if opts.setup[server](server, server_opts) then
                        return
                    end
                elseif opts.setup["*"] then
                    if opts.setup["*"](server, server_opts) then
                        return
                    end
                end
                require("lspconfig")[server].setup(server_opts)
            end

            local mlsp = require "mason-lspconfig"
            local all_mslp_servers = require("mason-lspconfig").get_mappings().lspconfig_to_package

            local ensure_installed = {}
            for server, server_opts in pairs(servers) do
                if server_opts then
                    server_opts = server_opts == true and {} or server_opts
                    if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then
                        setup(server)
                    else
                        ensure_installed[#ensure_installed + 1] = server
                    end
                end
            end

            -- mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } }
            mlsp.setup { ensure_installed = ensure_installed }
        end,
        -- config = function(_, opts)
        --     local cache = {}
        --
        --
        --
        --
        --     on_attach(function(client, buffer)
        --         -- TODO: add keybinds
        --     end)
        --
        --     local register_capability = vim.lsp.handlers["client/registerCapability"]
        --
        --     vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx)
        --         local ret = register_capability(err, res, ctx)
        --         local client_id = ctx.client_id
        --         local client = vim.lsp.get_client_by_id(client_id)
        --         local buffer = vim.api.nvim_get_current_buf()
        --         -- TODO: add keybinds
        --         return ret
        --     end
        --
        --     for name, icon in pairs(require("icons").diagnostics) do
        --         name = "DiagnosticSign" .. name
        --         vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" })
        --     end
        --
        --     local inlay_hint = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint
        --
        --
        --     if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then
        --         opts.diagnostics.virtual_text.prefix = vim.fn.has "nvim-0.10.0" == 0 and "●"
        --     end
        --
-- )
        --
        --     local servers = opts.servers
        --     local has_cmp, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
        --     local capabilities = vim.tbl_deep_extend(
        --         "force",
        --         {},
        --         vim.lsp.protocol.make_client_capabilities(),
        --         has_cmp and cmp_nvim_lsp.default_capabilities() or {},
        --         opts.capabilities or {}
        --     )
        --
        --     local function setup(server)
        --         local server_opts = vim.tbl_deep_extend("force", {
        --             capabilities = vim.deepcopy(capabilities),
        --         }, servers[server] or {})
        --
        --         if opts.setup[server] then
        --             if opts.setup[server](server, server_opts) then
        --                 return
        --             end
        --         elseif opts.setup["*"] then
        --             if opts.setup["*"](server, server_opts) then
        --                 return
        --             end
        --         end
        --         require("lspconfig")[server].setup(server_opts)
        --     end
        --
        --     local have_mason, mlsp = pcall(require, "mason-lspconfig")
        --     local all_mslp_servers = {}
        --     if have_mason then
        --         all_mslp_servers = vim.tbl_keys(require("mason-lspconfig.mappings.server").lspconfig_to_package)
        --     end
        --
        --     local ensure_installed = {}
        --     for server, server_opts in pairs(servers) do
        --         if server_opts then
        --             server_opts = server_opts == true and {} or server_opts
        --             if server_opts.mason == false or not vim.tbl_contains(all_mslp_servers, server) then
        --                 setup(server)
        --             else
        --                 ensure_installed[#ensure_installed + 1] = server
        --             end
        --         end
        --     end
        --
        --     if have_mason then
        --         mlsp.setup { ensure_installed = ensure_installed, handlers = { setup } }
        --     end
        -- end,
    },
}