init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. local M = {
  2. "mson-org/mason-lspconfig.nvim",
  3. dependencies = {
  4. "mson-org/mason.nvim",
  5. "WhoIsSethDaniel/mason-tool-installer.nvim",
  6. "j-hui/fidget.nvim",
  7. "ibhagwan/fzf-lua",
  8. require("plugins.lsp.extras.lazydev"),
  9. require("plugins.lsp.extras.gopher"),
  10. },
  11. }
  12. local function setup_keymaps(bufnr)
  13. local opts = { buffer = bufnr, noremap = true, silent = true }
  14. local keymaps = {
  15. { "K", vim.lsp.buf.hover, desc = "Hover Doc", border = "rounded" },
  16. { "<C-h>", vim.lsp.buf.signature_help, desc = "Signature Help", border = "rounded" },
  17. { "<leader>rn", vim.lsp.buf.rename, desc = "Rename" },
  18. { "<leader>ca", vim.lsp.buf.code_action, desc = "Code Action" },
  19. { "gd", require("fzf-lua").lsp_definitions, desc = "Go to Definition" },
  20. { "gr", require("fzf-lua").lsp_references, desc = "Go to References" },
  21. { "gD", vim.lsp.buf.declaration, desc = "Go to Declaration" },
  22. { "gi", require("fzf-lua").lsp_implementations, desc = "Go to Implementation" },
  23. { "gt", require("fzf-lua").lsp_typedefs, desc = "Go to Type Definition" },
  24. { "<leader>vd", vim.diagnostic.open_float, desc = "View Diagnostics" },
  25. { "<leader>dl", require("fzf-lua").diagnostics_document, desc = "Document Diagnostics" },
  26. { "<leader>dw", require("fzf-lua").diagnostics_workspace, desc = "Workspace Diagnostics" },
  27. { "<leader>ds", require("fzf-lua").lsp_document_symbols, desc = "Document Symbols" },
  28. { "<leader>ws", require("fzf-lua").lsp_workspace_symbols, desc = "Workspace Symbols" },
  29. { "<leader>lr", ":LspRestart<CR>", desc = "Restart LSP" },
  30. { "<leader>li", ":LspInfo<CR>", desc = "LSP Info" },
  31. }
  32. for _, map in ipairs(keymaps) do
  33. nmap(map[1], map[2], vim.tbl_extend("force", opts, { desc = map[3], border = map[4] }))
  34. end
  35. end
  36. function M.config()
  37. require("mason").setup({ max_concurrent_installers = 4 })
  38. require("fidget").setup({})
  39. -- Diagnostic configuration
  40. vim.diagnostic.config({
  41. virtual_text = { spacing = 2, source = "if_many" },
  42. float = { border = "rounded", source = "if_many" },
  43. severity_sort = true,
  44. signs = vim.g.have_nerd_font and {
  45. text = {
  46. [vim.diagnostic.severity.ERROR] = "󰅚",
  47. [vim.diagnostic.severity.WARN] = "󰀪",
  48. [vim.diagnostic.severity.INFO] = "󰋽",
  49. [vim.diagnostic.severity.HINT] = "󰌶",
  50. },
  51. } or {},
  52. })
  53. -- List of servers to enable (matching files in ~/.config/nvim/lsp/)
  54. local servers = {
  55. "gopls",
  56. "jsonls",
  57. "lua_ls",
  58. "yamlls",
  59. "graphql",
  60. "html",
  61. "omnisharp",
  62. "svelte",
  63. "vtsls",
  64. "ccls",
  65. "templ",
  66. }
  67. -- Servers not supported by mason-lspconfig
  68. local mason_unsupported = { "ccls" }
  69. -- Filter supported servers for mason-lspconfig
  70. local mason_servers = vim.tbl_filter(function(server)
  71. return not vim.tbl_contains(mason_unsupported, server)
  72. end, servers)
  73. -- Tools
  74. local tools = {
  75. "prettierd",
  76. "shfmt",
  77. "stylua",
  78. "latexindent",
  79. "clang-format",
  80. "csharpier",
  81. "quick-lint-js",
  82. }
  83. -- Mason-LSPconfig setup for automatic installation
  84. require("mason-lspconfig").setup({
  85. ensure_installed = mason_servers,
  86. automatic_installation = true,
  87. automatic_enable = true,
  88. })
  89. -- Mason tool installer for tools and ccls
  90. require("mason-tool-installer").setup({ ensure_installed = tools })
  91. -- Enable LSP servers
  92. vim.lsp.enable(servers)
  93. -- LSP Attach
  94. vim.api.nvim_create_autocmd("LspAttach", {
  95. group = vim.api.nvim_create_augroup("UserLspConfig", { clear = true }),
  96. callback = function(args)
  97. local client = vim.lsp.get_client_by_id(args.data.client_id)
  98. local bufnr = args.buf
  99. setup_keymaps(bufnr)
  100. -- Inlay hints
  101. if client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then
  102. vim.keymap.set("n", "<leader>th", function()
  103. vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = bufnr }))
  104. end, { buffer = bufnr, desc = "Toggle Inlay Hints" })
  105. end
  106. -- Document highlights
  107. if client.server_capabilities.documentHighlightProvider then
  108. vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
  109. buffer = bufnr,
  110. callback = vim.lsp.buf.document_highlight,
  111. })
  112. vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
  113. buffer = bufnr,
  114. callback = vim.lsp.buf.clear_references,
  115. })
  116. end
  117. end,
  118. })
  119. end
  120. return M