Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.0 KiB

  1. local function getTableKeys(tbl)
  2. local keys = {}
  3. for key in pairs(tbl) do
  4. table.insert(keys, key)
  5. end
  6. return keys
  7. end
  8. local function mergeTables(t1, t2)
  9. for k, v in pairs(t2) do
  10. t1[k] = v
  11. end
  12. return t1
  13. end
  14. local function is_null_ls_formatting_enabled(bufnr)
  15. local file_type = vim.api.nvim_buf_get_option(bufnr, "filetype")
  16. local generators = require("null-ls.generators").get_available(
  17. file_type,
  18. require("null-ls.methods").internal.FORMATTING
  19. )
  20. return #generators > 0
  21. end
  22. local custom_attach = function(client, bufnr)
  23. -- null-ls formatting support
  24. if client.server_capabilities.documentFormattingProvider then
  25. if
  26. client.name == "null-ls" and is_null_ls_formatting_enabled(bufnr)
  27. or client.name ~= "null-ls"
  28. then
  29. vim.bo[bufnr].formatexpr = "v:lua.vim.lsp.formatexpr()"
  30. vim.keymap.set("n", "<leader>gq", "<cmd>lua vim.lsp.buf.format({ async = true })<CR>", opts)
  31. else
  32. vim.bo[bufnr].formatexpr = nil
  33. end
  34. end
  35. vim.api.nvim_create_autocmd("CursorHold", {
  36. buffer=bufnr,
  37. callback = function()
  38. local opts = {
  39. focusable = false,
  40. close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
  41. border = 'rounded',
  42. source = 'always', -- show source in diagnostic popup window
  43. prefix = ' '
  44. }
  45. if not vim.b.diagnostics_pos then
  46. vim.b.diagnostics_pos = { nil, nil }
  47. end
  48. local cursor_pos = vim.api.nvim_win_get_cursor(0)
  49. if (cursor_pos[1] ~= vim.b.diagnostics_pos[1] or cursor_pos[2] ~= vim.b.diagnostics_pos[2]) and
  50. #vim.diagnostic.get() > 0
  51. then
  52. vim.diagnostic.open_float(nil, opts)
  53. end
  54. vim.b.diagnostics_pos = cursor_pos
  55. end
  56. })
  57. -- Make K show hover menu if it is supported
  58. vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr })
  59. -- The blow command will highlight the current variable and its usages in the buffer.
  60. if client.server_capabilities.document_highlight then
  61. vim.cmd([[
  62. hi! link LspReferenceRead Visual
  63. hi! link LspReferenceText Visual
  64. hi! link LspReferenceWrite Visual
  65. augroup lsp_document_highlight
  66. autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
  67. autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()
  68. autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
  69. augroup END
  70. ]])
  71. end
  72. if vim.g.logging_level == 'debug' then
  73. local msg = string.format("Language server %s started!", client.name)
  74. vim.notify(msg, 'info', {title = 'Nvim-config'})
  75. end
  76. end
  77. local lspconfigs = {
  78. clangd = {},
  79. pyright = {},
  80. bashls = {},
  81. html = {},
  82. tsserver = {},
  83. lua_ls = require("config.lsp.lua_ls"),
  84. cssls = {},
  85. asm_lsp = {},
  86. rust_analyzer = require("config.lsp.rust_analyzer"),
  87. cmake = require("config.lsp.cmake"),
  88. }
  89. local mason_extras = {
  90. }
  91. return {
  92. mason_servers = mergeTables(getTableKeys(lspconfigs), mason_extras),
  93. lspconfigs = lspconfigs,
  94. lsp_onattach = custom_attach
  95. }