init.lua 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. local utils = require "lvim.utils"
  2. local Log = require "lvim.core.log"
  3. local M = {}
  4. local user_config_dir = get_config_dir()
  5. local user_config_file = utils.join_paths(user_config_dir, "config.lua")
  6. ---Get the full path to the user configuration file
  7. ---@return string
  8. function M:get_user_config_path()
  9. return user_config_file
  10. end
  11. --- Initialize lvim default configuration and variables
  12. function M:init()
  13. lvim = vim.deepcopy(require "lvim.config.defaults")
  14. require("lvim.keymappings").load_defaults()
  15. local builtins = require "lvim.core.builtins"
  16. builtins.config { user_config_file = user_config_file }
  17. local settings = require "lvim.config.settings"
  18. settings.load_defaults()
  19. local autocmds = require "lvim.core.autocmds"
  20. autocmds.load_defaults()
  21. local lvim_lsp_config = require "lvim.lsp.config"
  22. lvim.lsp = vim.deepcopy(lvim_lsp_config)
  23. lvim.builtin.bigfile = {
  24. active = true,
  25. config = {},
  26. }
  27. require("lvim.config._deprecated").handle()
  28. end
  29. --- Override the configuration with a user provided one
  30. -- @param config_path The path to the configuration overrides
  31. function M:load(config_path)
  32. local autocmds = reload "lvim.core.autocmds"
  33. config_path = config_path or self:get_user_config_path()
  34. local ok, err = pcall(dofile, config_path)
  35. if not ok then
  36. if utils.is_file(user_config_file) then
  37. Log:warn("Invalid configuration: " .. err)
  38. else
  39. vim.notify_once(
  40. string.format("User-configuration not found. Creating an example configuration in %s", config_path)
  41. )
  42. local config_name = vim.loop.os_uname().version:match "Windows" and "config_win" or "config"
  43. local example_config = join_paths(get_lvim_base_dir(), "utils", "installer", config_name .. ".example.lua")
  44. vim.fn.mkdir(user_config_dir, "p")
  45. vim.loop.fs_copyfile(example_config, config_path)
  46. end
  47. end
  48. Log:set_level(lvim.log.level)
  49. require("lvim.config._deprecated").post_load()
  50. autocmds.define_autocmds(lvim.autocommands)
  51. vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader
  52. reload("lvim.keymappings").load(lvim.keys)
  53. if lvim.transparent_window then
  54. autocmds.enable_transparent_mode()
  55. end
  56. if lvim.reload_config_on_save then
  57. autocmds.enable_reload_config_on_save()
  58. end
  59. end
  60. --- Override the configuration with a user provided one
  61. -- @param config_path The path to the configuration overrides
  62. function M:reload()
  63. vim.schedule(function()
  64. reload("lvim.utils.hooks").run_pre_reload()
  65. M:load()
  66. reload("lvim.core.autocmds").configure_format_on_save()
  67. local plugins = reload "lvim.plugins"
  68. local plugin_loader = reload "lvim.plugin-loader"
  69. plugin_loader.reload { plugins, lvim.plugins }
  70. reload("lvim.core.theme").setup()
  71. reload("lvim.utils.hooks").run_post_reload()
  72. end)
  73. end
  74. return M