lua_ls.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. local default_workspace = {
  2. library = {
  3. vim.fn.expand "$VIMRUNTIME",
  4. get_lvim_base_dir(),
  5. require("neodev.config").types(),
  6. "${3rd}/busted/library",
  7. "${3rd}/luassert/library",
  8. "${3rd}/luv/library",
  9. },
  10. maxPreload = 5000,
  11. preloadFileSize = 10000,
  12. }
  13. local add_packages_to_workspace = function(packages, config)
  14. -- config.settings.Lua = config.settings.Lua or { workspace = default_workspace }
  15. local runtimedirs = vim.api.nvim__get_runtime({ "lua" }, true, { is_lua = true })
  16. local workspace = config.settings.Lua.workspace
  17. for _, v in pairs(runtimedirs) do
  18. for _, pack in ipairs(packages) do
  19. if v:match(pack) and not vim.tbl_contains(workspace.library, v) then
  20. table.insert(workspace.library, v)
  21. end
  22. end
  23. end
  24. end
  25. local lspconfig = require "lspconfig"
  26. local make_on_new_config = function(on_new_config, _)
  27. return lspconfig.util.add_hook_before(on_new_config, function(new_config, _)
  28. local server_name = new_config.name
  29. if server_name ~= "lua_ls" then
  30. return
  31. end
  32. local plugins = { "plenary.nvim", "telescope.nvim", "nvim-treesitter", "LuaSnip" }
  33. add_packages_to_workspace(plugins, new_config)
  34. end)
  35. end
  36. lspconfig.util.default_config = vim.tbl_extend("force", lspconfig.util.default_config, {
  37. on_new_config = make_on_new_config(lspconfig.util.default_config.on_new_config),
  38. })
  39. local opts = {
  40. settings = {
  41. Lua = {
  42. telemetry = { enable = false },
  43. runtime = {
  44. version = "LuaJIT",
  45. special = {
  46. reload = "require",
  47. },
  48. },
  49. diagnostics = {
  50. globals = { "vim", "lvim", "reload" },
  51. },
  52. workspace = default_workspace,
  53. },
  54. },
  55. }
  56. return opts