summaryrefslogtreecommitdiff
path: root/neovim/lua/vimrc/lsp.lua
blob: 94a2cc27ea77fae426d4eda6d7fe2f898ed4c15d (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
local utils = require 'vimrc.utils'
local M = {}

-- TODO: parameterise
-- TODO: extend stuff later

M.client_log = {}

-- local functions {{{
local function on_publish_diagnostics(_, result, ctx, config)
    vim.lsp.diagnostic.on_publish_diagnostics(_, result, ctx, config)
    vim.diagnostic.setloclist({
        open = false
    })
end

local function setup_handlers(client, _)
    client.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(on_publish_diagnostics, {
        virtual_text = false,
        underline = true,
        update_in_insert = false,
        severity_sort = true
    })
end

local function setup_server_capabilities_maps(client, bufnr)
    local map = vim.api.nvim_buf_set_keymap
    local opts = { silent = true, noremap = true }
    local capabilities = client.server_capabilities

    if capabilities.completion ~= false then
        vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
    end

    if capabilities.hover ~= false then
        vim.api.nvim_buf_set_option(bufnr, "keywordprg", ":LspHover")
    end

    if capabilities.rename == true then
        map(bufnr, "n", "<leader>gr", "<Cmd>lua vim.lsp.buf.rename()<CR>", opts)
    end

    if capabilities.signature_help == true then
        map(bufnr, "n", "<leader>gs", "<Cmd>lua vim.lsp.buf.signature_help()<CR>", opts)
    end

    if capabilities.goto_definition ~= false then
        map(bufnr, "n", "<leader>gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
    end

    if capabilities.declaration == true then
        map(bufnr, "n", "<leader>gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
    end

    if capabilities.implementation == true then
        map(bufnr, "n", "<leader>gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
    end

    if capabilities.find_references ~= false then
        map(bufnr, "n", "<leader>gg", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
    end

    map(bufnr, "n", "<leader>ge", "<cmd>lua require'vimrc.lsp'.line_diagnostic()<CR>", opts)

    if capabilities.document_symbol ~= false then
        map(bufnr, "n", "<leader>gds", "<cmd>lua vim.lsp.buf.document_symbol()<CR>", opts)
    end

    if capabilities.workspace_symbol ~= true then
        map(bufnr, "n", "<leader>gw", "<cmd>lua vim.lsp.buf.workspace_symbol()<CR>", opts)
    end

    if capabilities.code_action ~= false then
        map(bufnr, "n", "<leader>ga", "<cmd>lua vim.lsp.buf.code_action()<CR>", opts)
    end

    if capabilities.documentFormattingProvider == true then
        vim.api.nvim_buf_set_option(bufnr, "formatexpr", "v:lua.vim.lsp.format()")
        map(bufnr, "n", "<leader>gq", "<cmd>lua vim.lsp.buf.format({ async = true })<CR>", opts)
    end

    if capabilities.document_range_formatting == true then
        map(bufnr, "v", "<leader>gq", "<Esc><Cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
    end

    if capabilities.hover ~= false then
        vim.api.nvim_command("command! -buffer -nargs=1 LspHover lua vim.lsp.buf.hover()<CR>")
    end
end

-- }}}

-- diagnostics {{{
function M.line_diagnostic()
    local line = vim.fn.line('.') - 1
    local diags = vim.diagnostic.get(0, {
        lnum = line
    })
    for _, diag in ipairs(diags) do
        utils.log_warning(diag.message, diag.source)
    end
end

-- }}}

-- setup {{{
function M.setup()

    local buffer_setup = function(client)
        table.insert(M.client_log, client)
        local bufnr = vim.api.nvim_get_current_buf()

        setup_server_capabilities_maps(client, bufnr)
        setup_handlers(client, bufnr)
        require("lsp_signature").on_attach({
            bind = true,
            floating_window = false,
            toggle_key = "<C-g><C-s>",
            extra_trigger_chars = { "{", "}" },
            hint_prefix = "@ ",
            check_pumvisible = false
        }, bufnr)
    end

    local buffer_setup_no_format = function(client)
        client.server_capabilities.document_formatting = false
        buffer_setup(client)
    end

    -- lspconfig {{{
    local lspconfig = require 'lspconfig'
    -- check if docker is executable first?
    local runtime_path = vim.split(package.path, ';')
    table.insert(runtime_path, "lua/?.lua")
    table.insert(runtime_path, "lua/?/init.lua")

    -- always load lua lsp
    require('nlua.lsp.nvim').setup(require('lspconfig'), {
      cmd = { "/lsp/bin/lua-language-server", "-E", "/lsp/main.lua" },
      on_attach = buffer_setup_no_format,

      -- Include globals you want to tell the LSP are real :)
      globals = {
        -- Colorbuddy
        "Color", "c", "Group", "g", "s",
      }
    })

    -- lspconfig.sumneko_lua.setup {
    --     filetypes = { "lua" },
    --     on_attach = buffer_setup_no_format,
    --     settings = {
    --         Lua = {
    --             completion = {
    --                 keywordSnippet = "Disable",
    --                 showWord = "Disable",
    --             },
    --             diagnostics = {
    --                 enable = true,
    --                 globals = vim.list_extend({
    --                     -- Neovim
    --                     "vim",
    --                     -- Busted
    --                     "describe", "it", "before_each", "after_each", "teardown", "pending", "clear"
    --                 }, {})
    --             },
    --             runtime = {
    --                 version = "LuaJIT",
    --             },
    --             workspace = {
    --                 vim.list_extend(get_lua_runtime(), {}),
    --                 maxPreload = 10000,
    --                 preloadFileSize = 10000,
    --             },
    --         }
    --     }
    -- }

    -- out = vim.fn.system('docker images -q mvinkio/azure-pipelines-lsp')
    -- if string.len(out) ~= 0 then
    --     lspconfig.yamlls.setup {
    --         before_init = function(params)
    --             params.processId = vim.NIL
    --         end,
    --         on_new_config = function(new_config, new_root_dir)
    --             new_config.cmd = {
    --                 "node",
    --                 new_root_dir,
    --                 home .. "/projects/devops-pipelines/node_modules/azure-pipelines-language-server/out/server.js",
    --                 "--stdio"
    --             }
    --         end,
    --         filetypes = { "yaml" },
    --         root_dir = lspconfig.util.root_pattern(".git", vim.fn.getcwd()),
    --         on_attach = buffer_setup_no_format,
    --         settings = {
    --             yaml = {
    --                 format = {
    --                     enable = true
    --                 },
    --                 schemas = {
    --                     [home .. "/projects/devops-pipelines/schema"] = "/*"
    --                 },
    --                 validate = true
    --             }
    --         }
    --     }
    -- else
    --     utils.log_warning("No image mvinkio/azure-pipelines-lsp.", "vimrc/lsp", true)
    -- end

    local out = vim.fn.system('docker images -q mvinkio/python')
    if string.len(out) ~= 0 then
        lspconfig.pyright.setup {
            cmd = {
                "docker",
                "run",
                "--rm",
                "--env-file=" .. vim.fn.getcwd() .. "/.env",
                "--interactive",
                "--workdir=" .. vim.fn.getcwd(),
                "--volume=" .. vim.fn.getcwd() .. ":" .. vim.fn.getcwd(),
                "mvinkio/python",
                "pyright-langserver", "--stdio"
            },
            on_new_config = function(new_config, new_root_dir)
                new_config.cmd = {
                    "docker",
                    "run",
                    "--rm",
                    "--env-file=" .. new_root_dir .. "/.env",
                    "--interactive",
                    "--workdir=" .. new_root_dir,
                    "--volume=" .. new_root_dir .. ":" .. new_root_dir,
                    "mvinkio/python",
                    "pyright-langserver", "--stdio"
                }
            end,
            filetypes = { "python" },
            root_dir = lspconfig.util.root_pattern(".git", vim.fn.getcwd()),
            on_attach = buffer_setup_no_format,
        }
    else
        utils.log_warning("No image mvinkio/python.", "vimrc/lsp", true)
    end

    out = vim.fn.system('docker images -q mvinkio/go')
    if string.len(out) ~= 0 then
        lspconfig.gopls.setup {
            before_init = function(params)
                params.processId = vim.NIL
            end,
            on_new_config = function(new_config, new_root_dir)
                new_config.cmd = {
                    "docker",
                    "run",
                    "--rm",
                    "--interactive",
                    "-e=GOPROXY=https://proxy.golang.org",
                    "-e=GOOS=linux",
                    "-e=GOARCH=amd64",
                    "-e=GOPATH=" .. new_root_dir .. "/go",
                    "-e=GOCACHE=" .. new_root_dir .. "/.cache/go-build",
                    "--workdir=" .. new_root_dir,
                    "--volume=" .. new_root_dir .. ":" .. new_root_dir,
                    "--network=bridge",
                    "mvinkio/go",
                    "gopls"
                }
            end,
            -- cmd = { "docker", "run", "--rm", "-i", "-v", home .. ":" .. home, "mvinkio/gopls" },
            filetypes = { "go", "gomod", "gotmpl" },
            on_attach = buffer_setup_no_format,
        }
    else
        utils.log_warning("No image mvinkio/go.", "vimrc/lsp", true)
    end

    -- out = vim.fn.system('docker images -q mvinkio/sveltels')
    -- if string.len(out) ~= 0 then
    --     lspconfig.svelte.setup {
    --         before_init = function(params)
    --             params.processId = vim.NIL
    --         end,
    --         cmd = {
    --             "docker",
    --             "run",
    --             "--rm",
    --             "--interactive",
    --             "--volume=" .. home .. ":" .. home,
    --             "--network=none",
    --             "mvinkio/sveltels"
    --         },
    --         on_attach = buffer_setup,
    --     }
    -- else
    --     utils.log_warning("No image mvinkio/sveltels.", "vimrc/lsp", true)
    -- end

    -- }}}

    local null_ls = require("null-ls")
    local my_black = null_ls.builtins.formatting.black.with({
        filetypes = { "python" },
        command = "black",
        args = { "$FILENAME" }
    })
    null_ls.setup({
        debug = vim.fn.expand("$VIMRC_NULL_LS_DEBUG") == "1",
        update_on_insert = false,
        on_attach = buffer_setup,
        sources = {
            my_black,
            null_ls.builtins.completion.luasnip
        }
    })
end

-- }}}

return M
-- vim: fdm=marker