init.lua 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. }
  28. require("lvim.config._deprecated").handle()
  29. end
  30. --- Override the configuration with a user provided one
  31. -- @param config_path The path to the configuration overrides
  32. function M.load(config_path)
  33. local autocmds = reload "lvim.core.autocmds"
  34. config_path = config_path or M.get_user_config_path()
  35. local ok, err = pcall(dofile, config_path)
  36. if not ok then
  37. if utils.is_file(user_config_file) then
  38. Log:warn("Invalid configuration: " .. err)
  39. else
  40. vim.notify_once(
  41. string.format("User-configuration not found. Creating an example configuration in %s", config_path)
  42. )
  43. local example_config = join_paths(get_lvim_base_dir(), "utils", "installer", "config.example.lua")
  44. vim.loop.fs_copyfile(example_config, config_path)
  45. end
  46. end
  47. Log:set_level(lvim.log.level)
  48. autocmds.define_autocmds(lvim.autocommands)
  49. vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader
  50. reload("lvim.keymappings").load(lvim.keys)
  51. if lvim.transparent_window then
  52. autocmds.enable_transparent_mode()
  53. end
  54. if lvim.reload_config_on_save then
  55. autocmds.enable_reload_config_on_save()
  56. end
  57. end
  58. --- Override the configuration with a user provided one
  59. -- @param config_path The path to the configuration overrides
  60. function M.reload()
  61. vim.schedule(function()
  62. reload("lvim.utils.hooks").run_pre_reload()
  63. M:load()
  64. reload("lvim.core.autocmds").configure_format_on_save()
  65. local plugins = reload "lvim.plugins"
  66. local plugin_loader = reload "lvim.plugin-loader"
  67. plugin_loader.reload { plugins, lvim.plugins }
  68. reload("lvim.core.theme").setup()
  69. reload("lvim.utils.hooks").run_post_reload()
  70. end)
  71. end
  72. return M