init.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. local M = {}
  2. local Log = require "lvim.core.log"
  3. local utils = require "lvim.utils"
  4. local autocmds = require "lvim.core.autocmds"
  5. local function lsp_highlight_document(client)
  6. if lvim.lsp.document_highlight == false then
  7. return -- we don't need further
  8. end
  9. autocmds.enable_lsp_document_highlight(client.id)
  10. end
  11. local function lsp_code_lens_refresh(client)
  12. if lvim.lsp.code_lens_refresh == false then
  13. return
  14. end
  15. if client.resolved_capabilities.code_lens then
  16. autocmds.enable_code_lens_refresh()
  17. end
  18. end
  19. local function add_lsp_buffer_keybindings(bufnr)
  20. local mappings = {
  21. normal_mode = "n",
  22. insert_mode = "i",
  23. visual_mode = "v",
  24. }
  25. if lvim.builtin.which_key.active then
  26. -- Remap using which_key
  27. local status_ok, wk = pcall(require, "which-key")
  28. if not status_ok then
  29. return
  30. end
  31. for mode_name, mode_char in pairs(mappings) do
  32. wk.register(lvim.lsp.buffer_mappings[mode_name], { mode = mode_char, buffer = bufnr })
  33. end
  34. else
  35. -- Remap using nvim api
  36. for mode_name, mode_char in pairs(mappings) do
  37. for key, remap in pairs(lvim.lsp.buffer_mappings[mode_name]) do
  38. vim.api.nvim_buf_set_keymap(bufnr, mode_char, key, remap[1], { noremap = true, silent = true })
  39. end
  40. end
  41. end
  42. end
  43. function M.common_capabilities()
  44. local capabilities = vim.lsp.protocol.make_client_capabilities()
  45. capabilities.textDocument.completion.completionItem.snippetSupport = true
  46. capabilities.textDocument.completion.completionItem.resolveSupport = {
  47. properties = {
  48. "documentation",
  49. "detail",
  50. "additionalTextEdits",
  51. },
  52. }
  53. local status_ok, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
  54. if status_ok then
  55. capabilities = cmp_nvim_lsp.update_capabilities(capabilities)
  56. end
  57. return capabilities
  58. end
  59. local function select_default_formater(client)
  60. if client.name == "null-ls" or not client.resolved_capabilities.document_formatting then
  61. return
  62. end
  63. Log:debug("Checking for formatter overriding for " .. client.name)
  64. local formatters = require "lvim.lsp.null-ls.formatters"
  65. local client_filetypes = client.config.filetypes or {}
  66. for _, filetype in ipairs(client_filetypes) do
  67. if #vim.tbl_keys(formatters.list_registered(filetype)) > 0 then
  68. Log:debug("Formatter overriding detected. Disabling formatting capabilities for " .. client.name)
  69. client.resolved_capabilities.document_formatting = false
  70. client.resolved_capabilities.document_range_formatting = false
  71. end
  72. end
  73. end
  74. function M.common_on_exit(_, _)
  75. if lvim.lsp.document_highlight then
  76. autocmds.disable_lsp_document_highlight()
  77. end
  78. if lvim.lsp.code_lens_refresh then
  79. autocmds.disable_code_lens_refresh()
  80. end
  81. end
  82. function M.common_on_init(client, bufnr)
  83. if lvim.lsp.on_init_callback then
  84. lvim.lsp.on_init_callback(client, bufnr)
  85. Log:debug "Called lsp.on_init_callback"
  86. return
  87. end
  88. select_default_formater(client)
  89. end
  90. function M.common_on_attach(client, bufnr)
  91. if lvim.lsp.on_attach_callback then
  92. lvim.lsp.on_attach_callback(client, bufnr)
  93. Log:debug "Called lsp.on_attach_callback"
  94. end
  95. lsp_highlight_document(client)
  96. lsp_code_lens_refresh(client)
  97. add_lsp_buffer_keybindings(bufnr)
  98. end
  99. local function bootstrap_nlsp(opts)
  100. opts = opts or {}
  101. local lsp_settings_status_ok, lsp_settings = pcall(require, "nlspsettings")
  102. if lsp_settings_status_ok then
  103. lsp_settings.setup(opts)
  104. end
  105. end
  106. function M.get_common_opts()
  107. return {
  108. on_attach = M.common_on_attach,
  109. on_init = M.common_on_init,
  110. on_exit = M.common_on_exit,
  111. capabilities = M.common_capabilities(),
  112. }
  113. end
  114. function M.setup()
  115. Log:debug "Setting up LSP support"
  116. local lsp_status_ok, _ = pcall(require, "lspconfig")
  117. if not lsp_status_ok then
  118. return
  119. end
  120. for _, sign in ipairs(lvim.lsp.diagnostics.signs.values) do
  121. vim.fn.sign_define(sign.name, { texthl = sign.name, text = sign.text, numhl = sign.name })
  122. end
  123. require("lvim.lsp.handlers").setup()
  124. if not utils.is_directory(lvim.lsp.templates_dir) then
  125. require("lvim.lsp.templates").generate_templates()
  126. end
  127. bootstrap_nlsp {
  128. config_home = utils.join_paths(get_config_dir(), "lsp-settings"),
  129. append_default_schemas = true,
  130. }
  131. require("lvim.lsp.null-ls").setup()
  132. autocmds.configure_format_on_save()
  133. end
  134. return M