aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/home/.config/nvim/lua/cfg/lsp.lua
blob: 5767ff680a12e14fe465aefd49a2405643b9752c (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
return {
  on_attach_wrapper = function(client, bufnr, opts)
    local map = require("mapper")
    local autocmd = vim.api.nvim_create_autocmd

    opts = vim.tbl_extend("force", { auto_format = false }, opts or {})

    if client.supports_method("textDocument/codeLens") then
      require("virtualtypes").on_attach(client, bufnr)
      autocmd(
        { "CursorHold", "CursorHoldI", "InsertLeave" },
        { buffer = bufnr, callback = vim.lsp.codelens.refresh }
      )
      map.n("gl", vim.lsp.codelens.run, { buffer = bufnr })
    end

    if client.supports_method("textDocument/definition") then
      map.n("<c-]>", vim.lsp.buf.definition, { buffer = bufnr })
    end
    if client.supports_method("textDocument/declaration") then
      map.n("gD", vim.lsp.buf.declaration, { buffer = bufnr })
    end
    if client.supports_method("textDocument/signatureHelp") then
      require("lsp_signature").on_attach(client, bufnr)
      map.n("gs", vim.lsp.buf.signature_help, { buffer = bufnr })
    end
    if client.supports_method("textDocument/rename") then
      map.n("gR", vim.lsp.buf.rename, { buffer = bufnr })
    end
    if client.supports_method("textDocument/codeAction") then
      map.n("ga", vim.lsp.buf.code_action, { buffer = bufnr })
      map.v("ga", vim.lsp.buf.code_action, { buffer = bufnr })
    end

    local buf_async_format = function()
      vim.lsp.buf.format(
        { bufnr = bufnr, async = true, id = client.id })
    end
    local buf_sync_format = function()
      vim.lsp.buf.format(
        { bufnr = bufnr, async = false, id = client.id })
    end
    local buf_async_format_hunks = function()
      require("cfg.utils").format_hunks(
        { bufnr = bufnr, async = true, id = client.id })
    end

    if client.supports_method("textDocument/formatting") then
      map.n("<leader>f", buf_async_format, { buffer = bufnr })
      if opts.auto_format then
        autocmd(
          "BufWritePre",
          { buffer = bufnr, callback = buf_sync_format }
        )
      end
    end
    if client.supports_method("textDocument/rangeFormatting") then
      map.v("<leader>f", buf_async_format, { buffer = bufnr })
      map.n("<leader>hf", buf_async_format_hunks, { buffer = bufnr })
    end

    require("lsp-inlayhints").on_attach(client, bufnr, false)
  end
}