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.

293 lines
11 KiB

6 months ago
6 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. local base_path = ""
  77. if (vim.loop.fs_stat("/usr/lib/node_modules")) then
  78. base_path = "/usr/lib/node_modules"
  79. elseif (vim.loop.fs_stat("/usr/local/lib/node_modules")) then
  80. base_path = "/usr/local/lib/node_modules"
  81. elseif (vim.loop.fs_stat("/opt/homebrew/lib/node_modules")) then
  82. base_path = "/opt/homebrew/lib/node_modules"
  83. end
  84. local function get_typescript_server_path(root_dir)
  85. local global_ts = base_path .. "/typescript/lib"
  86. local found_ts = ""
  87. local function check_dir(path)
  88. found_ts = util.path.join(path, "node_modules", "typescript", "lib")
  89. if vim.loop.fs_stat(found_ts) then
  90. return path
  91. end
  92. end
  93. if util.search_ancestors(root_dir .. '/frontend/node_modules', check_dir) then
  94. return found_ts
  95. end
  96. if util.search_ancestors(root_dir .. '/node_modules', check_dir) then
  97. return found_ts
  98. end
  99. return global_ts
  100. end
  101. local path = util.path
  102. local function organize_imports()
  103. local params = {
  104. command = "_typescript.organizeImports",
  105. arguments = {vim.api.nvim_buf_get_name(0)},
  106. title = ""
  107. }
  108. vim.lsp.buf.execute_command(params)
  109. end
  110. local servers = {
  111. ts_ls = {
  112. init_options = {
  113. plugins = {
  114. {
  115. name = "@vue/typescript-plugin",
  116. location = base_path .. "/@vue/typescript-plugin",
  117. languages = { "javascript", "typescript", "vue", "react" },
  118. },
  119. },
  120. },
  121. filetypes = {
  122. "javascript",
  123. "typescript",
  124. "vue",
  125. "typescriptreact",
  126. },
  127. commands = {
  128. OrganizeImports = {
  129. organize_imports,
  130. description = "Organize Imports"
  131. }
  132. },
  133. on_new_config = function(new_config, new_root_dir)
  134. if get_typescript_server_path then
  135. new_config.init_options.typescript = new_config.init_options.typescript or {}
  136. new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir)
  137. else
  138. vim.notify("get_typescript_server_path is not defined", vim.log.levels.ERROR)
  139. end
  140. end,
  141. },
  142. -- volar = {
  143. -- filetypes = {
  144. -- "javascript",
  145. -- "typescript",
  146. -- "vue",
  147. -- },
  148. -- on_new_config = function(new_config, new_root_dir)
  149. -- new_config.init_options.typescript.tsdk = get_typescript_server_path(new_root_dir)
  150. -- end,
  151. -- },
  152. cssls = {},
  153. intelephense = {
  154. root_dir = function(pattern)
  155. ---@diagnostic disable-next-line: undefined-field
  156. local cwd = vim.loop.cwd()
  157. local root = util.root_pattern("composer.json")(pattern)
  158. -- prefer cwd if root is a descendant
  159. return util.path.is_descendant(cwd, root) and cwd or root
  160. end,
  161. init_options = {
  162. licenceKey = vim.fn.expand("$HOME/.local/share/nvim/intelephense-licence.txt"),
  163. },
  164. settings = {
  165. intelephense = {
  166. format = {
  167. enable = true,
  168. sortUseStatements = false,
  169. },
  170. },
  171. },
  172. },
  173. gopls = {
  174. cmd = { "gopls" },
  175. filetypes = { "go", "gomod", "gowork", "gotmpl" },
  176. root_dir = util.root_pattern("go.work", "go.mod", ".git"),
  177. settings = {
  178. gopls = {
  179. completeUnimported = true,
  180. usePlaceholders = true,
  181. analyses = {
  182. unusedparams = true,
  183. },
  184. },
  185. },
  186. },
  187. lua_ls = {
  188. settings = { -- custom settings for lua
  189. Lua = {
  190. -- make the language server recognize "vim" global
  191. diagnostics = {
  192. globals = { "vim" },
  193. },
  194. workspace = {
  195. -- make language server aware of runtime files
  196. library = {
  197. [vim.fn.expand("$VIMRUNTIME/lua")] = true,
  198. [vim.fn.stdpath("config") .. "/lua"] = true,
  199. },
  200. },
  201. },
  202. },
  203. },
  204. dartls = {
  205. cmd = { "dart", "language-server", "--protocol=lsp" },
  206. },
  207. rust_analyzer = {
  208. diagnostics = {
  209. enable = false,
  210. },
  211. },
  212. pyright = {
  213. -- before_init = function(_, config)
  214. -- config.settings.python.pythonpath = get_python_path(config.root_dir)
  215. -- end
  216. },
  217. yamlls = {
  218. settings = {
  219. yaml = {
  220. keyOrdering = false,
  221. },
  222. },
  223. },
  224. }
  225. require("mason").setup()
  226. local ensure_installed = vim.tbl_keys(servers or {})
  227. vim.list_extend(ensure_installed, {
  228. "stylua",
  229. "prettier",
  230. "prettierd",
  231. "eslint",
  232. "eslint_d",
  233. "jsonlint",
  234. "markdownlint",
  235. "phpcbf",
  236. "phpcs",
  237. "golangci-lint",
  238. "hadolint",
  239. "gofumpt",
  240. "goimports",
  241. })
  242. require("mason-tool-installer").setup({
  243. ensure_installed = ensure_installed,
  244. run_on_start = false,
  245. })
  246. require("mason-lspconfig").setup()
  247. for server_name, server in pairs(servers) do
  248. server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
  249. lspconfig[server_name].setup(server)
  250. end
  251. end,
  252. }