aboutsummaryrefslogtreecommitdiff
path: root/.config/nvim/lua/plugins/lsp.lua
blob: a438f3ad9bff62c35e0b7c4f38b7f6265b764a5c (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
local icons = require "icons"

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

local _supports_method = {}

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",
        "saghen/blink.cmp",
    },
    opts = {
        diagnostics = {},
        servers = {
            lua_ls = {
                settings = {
                    Lua = {
                        workspace = {
                            checkThirdParty = false,
                        },
                        completion = {
                            showWord = "Disable",
                            displayContext = 8,
                        },
                        hint = {
                            enable = true,
                        },
                        diagnostics = { "trailing-space" },
                    },
                },
            },
            rust_analyzer = { settings = { completion = { fullFunctionSignatures = true } } },
            ts_ls = {},
        },
        setup = {},
        inlay_hints = {
            exclude = {},
        },
    },
    config = function(_, opts)
        ---@param client vim.lsp.Client
        local function _check_methods(client, buffer)
            -- don't trigger on invalid buffers
            if not vim.api.nvim_buf_is_valid(buffer) then
                return
            end
            -- don't trigger on non-listed buffers
            if not vim.bo[buffer].buflisted then
                return
            end
            -- don't trigger on nofile buffers
            if vim.bo[buffer].buftype == "nofile" then
                return
            end
            for method, clients in pairs(_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

        ---@param on_attach fun(client:vim.lsp.Client, buffer)
        ---@param name? string
        local function on_attach(_on_attach, name)
            return vim.api.nvim_create_autocmd("LspAttach", {
                callback = function(args)
                    local buffer = args.buf ---@type number
                    local client = vim.lsp.get_client_by_id(args.data.client_id)
                    if client and (not name or client.name == name) then
                        return _on_attach(client, buffer)
                    end
                end,
            })
        end

        ---@param method string
        ---@param fn fun(client:vim.lsp.Client, buffer)
        local function on_supports_method(method, fn)
            _supports_method[method] = _supports_method[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

        ---@param fn fun(client:vim.lsp.Client, buffer):boolean?
        ---@param opts? {group?: integer}
        local function on_dynamic_capability(fn, opts)
            return vim.api.nvim_create_autocmd("User", {
                pattern = "LspDynamicCapability",
                group = opts and opts.group or nil,
                callback = function(args)
                    local client = vim.lsp.get_client_by_id(args.data.client_id)
                    local buffer = args.data.buffer ---@type number
                    if client then
                        return fn(client, buffer)
                    end
                end,
            })
        end

        local register_capability = vim.lsp.handlers["client/registerCapability"]
        vim.lsp.handlers["client/registerCapability"] = function(err, res, ctx)
            ---@diagnostic disable-next-line: no-unknown
            local ret = register_capability(err, res, ctx)
            local client = vim.lsp.get_client_by_id(ctx.client_id)
            if client then
                for buffer in pairs(client.attached_buffers) do
                    vim.api.nvim_exec_autocmds("User", {
                        pattern = "LspDynamicCapability",
                        data = { client_id = client.id, buffer = buffer },
                    })
                end
            end
            return ret
        end
        on_attach(_check_methods)
        on_dynamic_capability(_check_methods)

        -- 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 blink_cmp = require "blink.cmp"
        local capabilities = vim.tbl_deep_extend(
            "force",
            {},
            vim.lsp.protocol.make_client_capabilities(),
            blink_cmp.get_lsp_capabilities({}, false)
        )

        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
            vim.lsp.config(server, server_opts)
            vim.lsp.enable(server)
        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 }
    end,
}