init.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. local lsp_config = {}
  2. function lsp_config.config()
  3. require("lsp.kind").setup()
  4. require("lsp.handlers").setup()
  5. require("lsp.signs").setup()
  6. end
  7. -- My font didn't like this :/
  8. -- vim.api.nvim_set_keymap(
  9. -- "n",
  10. -- "gl",
  11. -- '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = { { "🭽", "FloatBorder" }, { "▔", "FloatBorder" }, { "🭾", "FloatBorder" }, { "▕", "FloatBorder" }, { "🭿", "FloatBorder" }, { "▁", "FloatBorder" }, { "🭼", "FloatBorder" }, { "▏", "FloatBorder" }, } })<CR>',
  12. -- { noremap = true, silent = true }
  13. -- )
  14. function lsp_config.setup_default_bindings()
  15. if lvim.lsp.default_keybinds then
  16. vim.cmd "nnoremap <silent> gd <cmd>lua vim.lsp.buf.definition()<CR>"
  17. vim.cmd "nnoremap <silent> gD <cmd>lua vim.lsp.buf.declaration()<CR>"
  18. vim.cmd "nnoremap <silent> gr <cmd>lua vim.lsp.buf.references()<CR>"
  19. vim.cmd "nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>"
  20. vim.api.nvim_set_keymap(
  21. "n",
  22. "gl",
  23. '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = "single" })<CR>',
  24. { noremap = true, silent = true }
  25. )
  26. vim.cmd "nnoremap <silent> gp <cmd>lua require'lsp.utils'.PeekDefinition()<CR>"
  27. vim.cmd "nnoremap <silent> K :lua vim.lsp.buf.hover()<CR>"
  28. vim.cmd "nnoremap <silent> <C-p> :lua vim.lsp.diagnostic.goto_prev({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
  29. vim.cmd "nnoremap <silent> <C-n> :lua vim.lsp.diagnostic.goto_next({popup_opts = {border = lvim.lsp.popup_border}})<CR>"
  30. -- vim.cmd "nnoremap <silent> <tab> <cmd>lua vim.lsp.buf.signature_help()<CR>"
  31. -- scroll down hover doc or scroll in definition preview
  32. -- scroll up hover doc
  33. -- vim.cmd 'command! -nargs=0 LspVirtualTextToggle lua require("lsp/virtual_text").toggle()'
  34. end
  35. end
  36. local function no_formatter_on_attach(client, bufnr)
  37. if lvim.lsp.on_attach_callback then
  38. lvim.lsp.on_attach_callback(client, bufnr)
  39. end
  40. require("lsp.utils").lsp_highlight_document(client)
  41. client.resolved_capabilities.document_formatting = false
  42. end
  43. function lsp_config.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. return capabilities
  54. end
  55. require("core.autocmds").define_augroups {
  56. _general_lsp = {
  57. { "FileType", "lspinfo", "nnoremap <silent> <buffer> q :q<CR>" },
  58. },
  59. }
  60. local function is_table(t)
  61. return type(t) == "table"
  62. end
  63. local function is_string(t)
  64. return type(t) == "string"
  65. end
  66. local function has_value(tab, val)
  67. for _, value in ipairs(tab) do
  68. if value == val then
  69. return true
  70. end
  71. end
  72. return false
  73. end
  74. function lsp_config.setup(lang)
  75. local lang_server = lvim.lang[lang].lsp
  76. local provider = lang_server.provider
  77. if require("utils").check_lsp_client_active(provider) then
  78. return
  79. end
  80. local overrides = lvim.lsp.override
  81. if is_table(overrides) then
  82. if has_value(overrides, lang) then
  83. return
  84. end
  85. end
  86. if is_string(overrides) then
  87. if overrides == lang then
  88. return
  89. end
  90. end
  91. local sources = require("lsp.null-ls").setup(lang)
  92. for _, source in pairs(sources) do
  93. local method = source.method
  94. local format_method = "NULL_LS_FORMATTING"
  95. if is_table(method) then
  96. if has_value(method, format_method) then
  97. lang_server.setup.on_attach = no_formatter_on_attach
  98. end
  99. end
  100. if is_string(method) then
  101. if method == format_method then
  102. lang_server.setup.on_attach = no_formatter_on_attach
  103. end
  104. end
  105. end
  106. if provider == "" or provider == nil then
  107. return
  108. end
  109. require("lspconfig")[provider].setup(lang_server.setup)
  110. end
  111. return lsp_config