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.

306 lines
11 KiB

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