init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. local M = {}
  2. local Log = require "core.log"
  3. function M.config()
  4. vim.lsp.protocol.CompletionItemKind = lvim.lsp.completion.item_kind
  5. for _, sign in ipairs(lvim.lsp.diagnostics.signs.values) do
  6. vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = sign.name })
  7. end
  8. require("lsp.handlers").setup()
  9. end
  10. local function lsp_highlight_document(client)
  11. if lvim.lsp.document_highlight == false then
  12. return -- we don't need further
  13. end
  14. -- Set autocommands conditional on server_capabilities
  15. if client.resolved_capabilities.document_highlight then
  16. vim.api.nvim_exec(
  17. [[
  18. hi LspReferenceRead cterm=bold ctermbg=red guibg=#464646
  19. hi LspReferenceText cterm=bold ctermbg=red guibg=#464646
  20. hi LspReferenceWrite cterm=bold ctermbg=red guibg=#464646
  21. augroup lsp_document_highlight
  22. autocmd! * <buffer>
  23. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  24. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  25. augroup END
  26. ]],
  27. false
  28. )
  29. end
  30. end
  31. local function add_lsp_buffer_keybindings(bufnr)
  32. local wk = require "which-key"
  33. local keys = {
  34. ["K"] = { "<cmd>lua vim.lsp.buf.hover()<CR>", "Show hover" },
  35. ["gd"] = { "<cmd>lua vim.lsp.buf.definition()<CR>", "Goto Definition" },
  36. ["gD"] = { "<cmd>lua vim.lsp.buf.declaration()<CR>", "Goto declaration" },
  37. ["gr"] = { "<cmd>lua vim.lsp.buf.references()<CR>", "Goto references" },
  38. ["gi"] = { "<cmd>lua vim.lsp.buf.implementation()<CR>", "Goto implementation" },
  39. ["gs"] = { "<cmd>lua vim.lsp.buf.signature_help()<CR>", "show signature help" },
  40. ["gp"] = { "<cmd>lua require'lsp.peek'.Peek('definition')<CR>", "Peek definition" },
  41. ["gl"] = {
  42. "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })<CR>",
  43. "Show line diagnostics",
  44. },
  45. }
  46. wk.register(keys, { mode = "n", buffer = bufnr })
  47. end
  48. local function set_smart_cwd(client)
  49. local proj_dir = client.config.root_dir
  50. if lvim.lsp.smart_cwd and proj_dir ~= "/" then
  51. vim.api.nvim_set_current_dir(proj_dir)
  52. require("core.nvimtree").change_tree_dir(proj_dir)
  53. end
  54. end
  55. function M.common_capabilities()
  56. local capabilities = vim.lsp.protocol.make_client_capabilities()
  57. capabilities.textDocument.completion.completionItem.snippetSupport = true
  58. capabilities.textDocument.completion.completionItem.resolveSupport = {
  59. properties = {
  60. "documentation",
  61. "detail",
  62. "additionalTextEdits",
  63. },
  64. }
  65. return capabilities
  66. end
  67. function M.get_ls_capabilities(client_id)
  68. local client
  69. if not client_id then
  70. local buf_clients = vim.lsp.buf_get_clients()
  71. for _, buf_client in ipairs(buf_clients) do
  72. if buf_client.name ~= "null-ls" then
  73. client_id = buf_client.id
  74. break
  75. end
  76. end
  77. end
  78. if not client_id then
  79. error "Unable to determine client_id"
  80. end
  81. client = vim.lsp.get_client_by_id(tonumber(client_id))
  82. local enabled_caps = {}
  83. for k, v in pairs(client.resolved_capabilities) do
  84. if v == true then
  85. table.insert(enabled_caps, k)
  86. end
  87. end
  88. return enabled_caps
  89. end
  90. function M.common_on_init(client, bufnr)
  91. if lvim.lsp.on_init_callback then
  92. lvim.lsp.on_init_callback(client, bufnr)
  93. Log:get_default().info "Called lsp.on_init_callback"
  94. return
  95. end
  96. local formatters = lvim.lang[vim.bo.filetype].formatters
  97. if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then
  98. client.resolved_capabilities.document_formatting = false
  99. Log:get_default().info(
  100. string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe)
  101. )
  102. end
  103. end
  104. function M.common_on_attach(client, bufnr)
  105. if lvim.lsp.on_attach_callback then
  106. lvim.lsp.on_attach_callback(client, bufnr)
  107. Log:get_default().info "Called lsp.on_init_callback"
  108. end
  109. lsp_highlight_document(client)
  110. add_lsp_buffer_keybindings(bufnr)
  111. set_smart_cwd(client)
  112. require("lsp.null-ls").setup(vim.bo.filetype)
  113. end
  114. function M.setup(lang)
  115. local lsp = lvim.lang[lang].lsp
  116. if require("utils").check_lsp_client_active(lsp.provider) then
  117. return
  118. end
  119. local overrides = lvim.lsp.override
  120. if type(overrides) == "table" then
  121. if vim.tbl_contains(overrides, lang) then
  122. return
  123. end
  124. end
  125. if lsp.provider ~= nil and lsp.provider ~= "" then
  126. local lspconfig = require "lspconfig"
  127. lspconfig[lsp.provider].setup(lsp.setup)
  128. end
  129. end
  130. return M