init.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 status_ok, wk = pcall(require, "which-key")
  33. if not status_ok then
  34. return
  35. end
  36. local keys = {
  37. ["K"] = { "<cmd>lua vim.lsp.buf.hover()<CR>", "Show hover" },
  38. ["gd"] = { "<cmd>lua vim.lsp.buf.definition()<CR>", "Goto Definition" },
  39. ["gD"] = { "<cmd>lua vim.lsp.buf.declaration()<CR>", "Goto declaration" },
  40. ["gr"] = { "<cmd>lua vim.lsp.buf.references()<CR>", "Goto references" },
  41. ["gi"] = { "<cmd>lua vim.lsp.buf.implementation()<CR>", "Goto implementation" },
  42. ["gs"] = { "<cmd>lua vim.lsp.buf.signature_help()<CR>", "show signature help" },
  43. ["gp"] = { "<cmd>lua require'lsp.peek'.Peek('definition')<CR>", "Peek definition" },
  44. ["gl"] = {
  45. "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })<CR>",
  46. "Show line diagnostics",
  47. },
  48. }
  49. wk.register(keys, { mode = "n", buffer = bufnr })
  50. end
  51. local function set_smart_cwd(client)
  52. local proj_dir = client.config.root_dir
  53. if lvim.lsp.smart_cwd and proj_dir ~= "/" then
  54. vim.api.nvim_set_current_dir(proj_dir)
  55. require("core.nvimtree").change_tree_dir(proj_dir)
  56. end
  57. end
  58. function M.common_capabilities()
  59. local capabilities = vim.lsp.protocol.make_client_capabilities()
  60. capabilities.textDocument.completion.completionItem.snippetSupport = true
  61. capabilities.textDocument.completion.completionItem.resolveSupport = {
  62. properties = {
  63. "documentation",
  64. "detail",
  65. "additionalTextEdits",
  66. },
  67. }
  68. return capabilities
  69. end
  70. function M.get_ls_capabilities(client_id)
  71. local client
  72. if not client_id then
  73. local buf_clients = vim.lsp.buf_get_clients()
  74. for _, buf_client in ipairs(buf_clients) do
  75. if buf_client.name ~= "null-ls" then
  76. client_id = buf_client.id
  77. break
  78. end
  79. end
  80. end
  81. if not client_id then
  82. error "Unable to determine client_id"
  83. end
  84. client = vim.lsp.get_client_by_id(tonumber(client_id))
  85. local enabled_caps = {}
  86. for k, v in pairs(client.resolved_capabilities) do
  87. if v == true then
  88. table.insert(enabled_caps, k)
  89. end
  90. end
  91. return enabled_caps
  92. end
  93. function M.common_on_init(client, bufnr)
  94. if lvim.lsp.on_init_callback then
  95. lvim.lsp.on_init_callback(client, bufnr)
  96. Log:get_default().info "Called lsp.on_init_callback"
  97. return
  98. end
  99. local formatters = lvim.lang[vim.bo.filetype].formatters
  100. if not vim.tbl_isempty(formatters) and formatters[1]["exe"] ~= nil and formatters[1].exe ~= "" then
  101. client.resolved_capabilities.document_formatting = false
  102. Log:get_default().info(
  103. string.format("Overriding language server [%s] with format provider [%s]", client.name, formatters[1].exe)
  104. )
  105. end
  106. end
  107. function M.common_on_attach(client, bufnr)
  108. if lvim.lsp.on_attach_callback then
  109. lvim.lsp.on_attach_callback(client, bufnr)
  110. Log:get_default().info "Called lsp.on_init_callback"
  111. end
  112. lsp_highlight_document(client)
  113. add_lsp_buffer_keybindings(bufnr)
  114. set_smart_cwd(client)
  115. require("lsp.null-ls").setup(vim.bo.filetype)
  116. end
  117. function M.setup(lang)
  118. local lsp_utils = require "lsp.utils"
  119. local lsp = lvim.lang[lang].lsp
  120. if lsp_utils.is_client_active(lsp.provider) then
  121. return
  122. end
  123. local overrides = lvim.lsp.override
  124. if type(overrides) == "table" then
  125. if vim.tbl_contains(overrides, lang) then
  126. return
  127. end
  128. end
  129. if lsp.provider ~= nil and lsp.provider ~= "" then
  130. local lspconfig = require "lspconfig"
  131. lspconfig[lsp.provider].setup(lsp.setup)
  132. end
  133. end
  134. return M