summaryrefslogtreecommitdiffstats
path: root/lua/core/lsp/functions.lua
blob: ab47ccce241cc1b2917d25c38f69e23c4f26b552 (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
local misc = require('core.misc')
local map = misc.map

local M = {}

function M.setup()
  vim.diagnostic.config {
    virtual_text = false,
    virtual_lines = {
      only_current_line = true,
    },
    signs = true,
    update_in_insert = false,
    underline = true,
    severity_sort = true
  }

  vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
    vim.lsp.handlers.hover, { border = 'solid' })

  vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
    vim.lsp.handlers.signature_help, { border = 'solid' })

  for _, v in pairs({
    { "DiagnosticSignError", "x" },
    { "DiagnosticSignWarn", "!" },
    { "DiagnosticSignInfo", "i" },
    { "DiagnosticSignHint", "h" }
  }) do
    vim.fn.sign_define(v[1], { text = v[2], texthl = v[1] })
  end
end

--- generate client capabilities for lsp servers
---@return table capabilities
function M.capabilities()
  local capabilities = vim.lsp.protocol.make_client_capabilities()
  capabilities.textDocument.completion.completionItem = {
    snippetSupport = true,
    preselectSupport = true,
    insertReplaceSupport = true,
    labelDetailsSupport = true,
    deprecatedSupport = true,
    commitCharactersSupport = true,
    tagSupport = {
      valueSet = { 1 }
    },
    resolveSupport = {
      properties = {
        "documentation",
        "detail",
        "additionalTextEdits"
      }
    }
  }

  return capabilities
end

--- setup basic options on lsp attach
---@param client number client number
---@param bufnr number buffer number
function M.attach(client, bufnr)
  local opts = { buffer = bufnr }
  -- LSP actions
  map('n', 'K', vim.lsp.buf.hover, opts)
  map('n', 'gd', vim.lsp.buf.definition, opts)
  map('n', 'gD', vim.lsp.buf.declaration)
  map('n', 'gi', vim.lsp.buf.implementation, opts)
  map('n', 'gy', vim.lsp.buf.type_definition, opts)
  map('n', 'gr', vim.lsp.buf.references, opts)
  map('n', '<S-Tab>', vim.lsp.buf.signature_help, opts)
  map('n', '<leader>r', vim.lsp.buf.rename, opts)
  map('n', '<F2>', vim.lsp.buf.rename, opts)
  map('n', 'gA', vim.lsp.buf.code_action, {
    buffer = bufnr,
    desc = 'check code actions',
  })
  map('n', '<F4>', vim.lsp.buf.code_action, {
    buffer = bufnr,
    desc = 'check code actions'
  })

  -- Diagnostics
  map('n', '[d', vim.diagnostic.goto_prev, opts)
  map('n', ']d', vim.diagnostic.goto_next, opts)
  -- map('n', 'qD', vim.diagnostic.setqflist, opts)
end

return M