init.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.luasnip = {
  24. sources = {
  25. friendly_snippets = true,
  26. },
  27. config = {
  28. updateevents = "TextChanged,TextChangedI",
  29. ext_opts = {
  30. -- Will be populated within config function
  31. },
  32. },
  33. }
  34. lvim.builtin.bigfile = {
  35. active = true,
  36. config = {},
  37. }
  38. require("lvim.config._deprecated").handle()
  39. end
  40. --- Override the configuration with a user provided one
  41. -- @param config_path The path to the configuration overrides
  42. function M:load(config_path)
  43. local autocmds = reload "lvim.core.autocmds"
  44. config_path = config_path or self:get_user_config_path()
  45. local ok, err = pcall(dofile, config_path)
  46. if not ok then
  47. if utils.is_file(user_config_file) then
  48. Log:warn("Invalid configuration: " .. err)
  49. else
  50. vim.notify_once(
  51. string.format("User-configuration not found. Creating an example configuration in %s", config_path)
  52. )
  53. local config_name = vim.loop.os_uname().version:match "Windows" and "config_win" or "config"
  54. local example_config = join_paths(get_lvim_base_dir(), "utils", "installer", config_name .. ".example.lua")
  55. vim.fn.mkdir(user_config_dir, "p")
  56. vim.loop.fs_copyfile(example_config, config_path)
  57. end
  58. end
  59. Log:set_level(lvim.log.level)
  60. require("lvim.config._deprecated").post_load()
  61. autocmds.define_autocmds(lvim.autocommands)
  62. vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader
  63. reload("lvim.keymappings").load(lvim.keys)
  64. if lvim.transparent_window then
  65. autocmds.enable_transparent_mode()
  66. end
  67. if lvim.reload_config_on_save then
  68. autocmds.enable_reload_config_on_save()
  69. end
  70. end
  71. --- Override the configuration with a user provided one
  72. -- @param config_path The path to the configuration overrides
  73. function M:reload()
  74. vim.schedule(function()
  75. reload("lvim.utils.hooks").run_pre_reload()
  76. M:load()
  77. reload("lvim.core.autocmds").configure_format_on_save()
  78. local plugins = reload "lvim.plugins"
  79. local plugin_loader = reload "lvim.plugin-loader"
  80. plugin_loader.reload { plugins, lvim.plugins }
  81. reload("lvim.core.theme").setup()
  82. reload("lvim.utils.hooks").run_post_reload()
  83. end)
  84. end
  85. return M