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.

300 lines
11 KiB

8 months ago
8 months ago
  1. return {
  2. "neovim/nvim-lspconfig",
  3. event = { "BufReadPre", "BufNewFile" },
  4. dependencies = {
  5. { "hrsh7th/cmp-nvim-lsp" },
  6. { "antosha417/nvim-lsp-file-operations", config = true },
  7. { "williamboman/mason.nvim", config = true },
  8. { "williamboman/mason-lspconfig.nvim" },
  9. { "WhoIsSethDaniel/mason-tool-installer.nvim" },
  10. { "j-hui/fidget.nvim", opts = {} },
  11. { "folke/neodev.nvim", opts = {} },
  12. },
  13. config = function()
  14. local lspconfig = require("lspconfig")
  15. local util = require("lspconfig.util")
  16. local keymap = vim.keymap
  17. vim.api.nvim_create_autocmd("LspAttach", {
  18. group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }),
  19. callback = function(event)
  20. local opts = { noremap = true, silent = true }
  21. opts.desc = "Show LSP references"
  22. keymap.set("n", "gr", "<cmd>Telescope lsp_references<CR>", opts)
  23. opts.desc = "Go to declaration"
  24. keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
  25. opts.desc = "Show LSP definitions"
  26. keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts)
  27. opts.desc = "Show LSP implementations"
  28. keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts)
  29. opts.desc = "Show LSP type definitions"
  30. keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts)
  31. opts.desc = "See available code actions"
  32. keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts)
  33. opts.desc = "Smart rename"
  34. keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts)
  35. opts.desc = "Show buffer diagnostics"
  36. keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts)
  37. opts.desc = "Show line diagnostics"
  38. keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts)
  39. opts.desc = "Go to previous diagnostic"
  40. keymap.set("n", "[d", vim.diagnostic.goto_prev, opts)
  41. opts.desc = "Go to next diagnostic"
  42. keymap.set("n", "]d", vim.diagnostic.goto_next, opts)
  43. opts.desc = "Go to previous diagnostic (error only)"
  44. keymap.set("n", "[e", function()
  45. vim.diagnostic.goto_prev({ severity = vim.diagnostic.severity.ERROR })
  46. end, opts)
  47. opts.desc = "Go to next diagnostic (error only)"
  48. keymap.set("n", "]e", function()
  49. vim.diagnostic.goto_next({ severity = vim.diagnostic.severity.ERROR })
  50. end, opts)
  51. opts.desc = "Show documentation for what is under cursor"
  52. keymap.set("n", "K", vim.lsp.buf.hover, opts)
  53. local client = vim.lsp.get_client_by_id(event.data.client_id)
  54. if client and client.server_capabilities.documentHighlightProvider then
  55. vim.api.nvim_create_autocmd("LspDetach", {
  56. group = vim.api.nvim_create_augroup("lsp-detach", { clear = true }),
  57. callback = function(event2)
  58. vim.lsp.buf.clear_references()
  59. end,
  60. })
  61. end
  62. -- The following autocommand is used to enable inlay hints in your
  63. -- code, if the language server you are using supports them
  64. --
  65. -- This may be unwanted, since they displace some of your code
  66. if client and client.server_capabilities.inlayHintProvider and vim.lsp.inlay_hint then
  67. opts.desc = "Toggle Inlay Hints"
  68. keymap.set("n", "<leader>th", function()
  69. vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({}))
  70. end, opts)
  71. end
  72. end,
  73. })
  74. local capabilities = vim.lsp.protocol.make_client_capabilities()
  75. capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())
  76. -- Function to detect the operating system
  77. local function get_os()
  78. local handle = io.popen("uname")
  79. if not handle then
  80. return error("Failed to detect operating system")
  81. end
  82. local result = handle:read("*a")
  83. handle:close()
  84. return result:lower():gsub("%s+", "")
  85. end
  86. local base_path = "/usr/lib/node_modules"
  87. if get_os() == "darwin" then
  88. base_path = "/opt/homebrew/lib/node_modules"
  89. end
  90. local function get_typescript_server_path(root_dir)
  91. local global_ts = base_path .. "/typescript/lib"
  92. local found_ts = ""
  93. local function check_dir(path)
  94. found_ts = util.path.join(path, "node_modules", "typescript", "lib")
  95. if vim.loop.fs_stat(found_ts) then
  96. return path
  97. end
  98. end
  99. if util.search_ancestors(root_dir .. '/frontend/node_modules', check_dir) then
  100. return found_ts
  101. end
  102. if util.search_ancestors(root_dir .. '/node_modules', check_dir) then
  103. return found_ts
  104. end
  105. return global_ts
  106. end
  107. local path = util.path
  108. local function organize_imports()
  109. local params = {
  110. command = "_typescript.organizeImports",
  111. arguments = {vim.api.nvim_buf_get_name(0)},
  112. title = ""
  113. }
  114. vim.lsp.buf.execute_command(params)
  115. end
  116. local servers = {
  117. ts_ls = {
  118. init_options = {
  119. plugins = {
  120. {
  121. name = "@vue/typescript-plugin",
  122. location = base_path .. "/@vue/typescript-plugin",
  123. languages = { "javascript", "typescript", "vue", "react" },
  124. },
  125. },
  126. },
  127. filetypes = {
  128. "javascript",
  129. "typescript",
  130. "vue",
  131. "typescriptreact",
  132. },
  133. commands = {
  134. OrganizeImports = {
  135. organize_imports,
  136. description = "Organize Imports"
  137. }
  138. },
  139. on_new_config = function(new_config, new_root_dir)
  140. if get_typescript_server_path then
  141. new_config.init_options.typescript = new_config.init_options.typescript or {}
  142. new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir)
  143. else
  144. vim.notify("get_typescript_server_path is not defined", vim.log.levels.ERROR)
  145. end
  146. end,
  147. },
  148. -- volar = {
  149. -- filetypes = {
  150. -- "javascript",
  151. -- "typescript",
  152. -- "vue",
  153. -- },
  154. -- on_new_config = function(new_config, new_root_dir)
  155. -- new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir)
  156. -- end,
  157. -- },
  158. cssls = {},
  159. intelephense = {
  160. root_dir = function(pattern)
  161. ---@diagnostic disable-next-line: undefined-field
  162. local cwd = vim.loop.cwd()
  163. local root = util.root_pattern("composer.json")(pattern)
  164. -- prefer cwd if root is a descendant
  165. return util.path.is_descendant(cwd, root) and cwd or root
  166. end,
  167. init_options = {
  168. licenceKey = vim.fn.expand("$HOME/.local/share/nvim/intelephense-licence.txt"),
  169. },
  170. settings = {
  171. intelephense = {
  172. format = {
  173. enable = true,
  174. sortUseStatements = false,
  175. },
  176. },
  177. },
  178. },
  179. gopls = {
  180. cmd = { "gopls" },
  181. filetypes = { "go", "gomod", "gowork", "gotmpl" },
  182. root_dir = util.root_pattern("go.work", "go.mod", ".git"),
  183. settings = {
  184. gopls = {
  185. completeUnimported = true,
  186. usePlaceholders = true,
  187. analyses = {
  188. unusedparams = true,
  189. },
  190. },
  191. },
  192. },
  193. lua_ls = {
  194. settings = { -- custom settings for lua
  195. Lua = {
  196. -- make the language server recognize "vim" global
  197. diagnostics = {
  198. globals = { "vim" },
  199. },
  200. workspace = {
  201. -- make language server aware of runtime files
  202. library = {
  203. [vim.fn.expand("$VIMRUNTIME/lua")] = true,
  204. [vim.fn.stdpath("config") .. "/lua"] = true,
  205. },
  206. },
  207. },
  208. },
  209. },
  210. dartls = {
  211. cmd = { "dart", "language-server", "--protocol=lsp" },
  212. },
  213. rust_analyzer = {
  214. diagnostics = {
  215. enable = false,
  216. },
  217. },
  218. pyright = {
  219. -- before_init = function(_, config)
  220. -- config.settings.python.pythonpath = get_python_path(config.root_dir)
  221. -- end
  222. },
  223. yamlls = {
  224. settings = {
  225. yaml = {
  226. keyOrdering = false,
  227. },
  228. },
  229. },
  230. }
  231. require("mason").setup()
  232. local ensure_installed = vim.tbl_keys(servers or {})
  233. vim.list_extend(ensure_installed, {
  234. "stylua",
  235. "prettier",
  236. "prettierd",
  237. "eslint",
  238. "eslint_d",
  239. "jsonlint",
  240. "markdownlint",
  241. "phpcbf",
  242. "phpcs",
  243. "golangci-lint",
  244. "hadolint",
  245. "gofumpt",
  246. "goimports",
  247. })
  248. require("mason-tool-installer").setup({
  249. ensure_installed = ensure_installed,
  250. run_on_start = false,
  251. })
  252. require("mason-lspconfig").setup()
  253. for server_name, server in pairs(servers) do
  254. server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
  255. lspconfig[server_name].setup(server)
  256. end
  257. end,
  258. }