lua_ls.lua 1.7 KB

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