summaryrefslogtreecommitdiff
path: root/lua/lvim/config/init.lua
blob: b201b408d4f2ad5356825f10e54df95c4df80c88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
local utils = require "lvim.utils"
local Log = require "lvim.core.log"

local M = {}
local user_config_dir = get_config_dir()
local user_config_file = utils.join_paths(user_config_dir, "config.lua")

---Get the full path to the user configuration file
---@return string
function M.get_user_config_path()
  return user_config_file
end

--- Initialize lvim default configuration and variables
function M.init()
  lvim = vim.deepcopy(require "lvim.config.defaults")

  require("lvim.keymappings").load_defaults()

  local builtins = require "lvim.core.builtins"
  builtins.config { user_config_file = user_config_file }

  local settings = require "lvim.config.settings"
  settings.load_defaults()

  local autocmds = require "lvim.core.autocmds"
  autocmds.load_defaults()

  local lvim_lsp_config = require "lvim.lsp.config"
  lvim.lsp = vim.deepcopy(lvim_lsp_config)

  lvim.builtin.luasnip = {
    sources = {
      friendly_snippets = true,
    },
  }

  require("lvim.config._deprecated").handle()
end

--- Override the configuration with a user provided one
-- @param config_path The path to the configuration overrides
function M.load(config_path)
  local autocmds = reload "lvim.core.autocmds"
  config_path = config_path or M.get_user_config_path()
  local ok, err = pcall(dofile, config_path)
  if not ok then
    if utils.is_file(user_config_file) then
      Log:warn("Invalid configuration: " .. err)
    else
      vim.notify_once(
        string.format("User-configuration not found. Creating an example configuration in %s", config_path)
      )
      local example_config = join_paths(get_lvim_base_dir(), "utils", "installer", "config.example.lua")
      vim.loop.fs_copyfile(example_config, config_path)
    end
  end

  Log:set_level(lvim.log.level)

  autocmds.define_autocmds(lvim.autocommands)

  vim.g.mapleader = (lvim.leader == "space" and " ") or lvim.leader

  reload("lvim.keymappings").load(lvim.keys)

  if lvim.transparent_window then
    autocmds.enable_transparent_mode()
  end

  if lvim.reload_config_on_save then
    autocmds.enable_reload_config_on_save()
  end
end

--- Override the configuration with a user provided one
-- @param config_path The path to the configuration overrides
function M.reload()
  vim.schedule(function()
    reload("lvim.utils.hooks").run_pre_reload()

    M:load()

    reload("lvim.core.autocmds").configure_format_on_save()

    local plugins = reload "lvim.plugins"
    local plugin_loader = reload "lvim.plugin-loader"

    plugin_loader.reload { plugins, lvim.plugins }
    reload("lvim.core.theme").setup()
    reload("lvim.utils.hooks").run_post_reload()
  end)
end

return M