init.lua 4.3 KB

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