-
Notifications
You must be signed in to change notification settings - Fork 42
/
init.lua
150 lines (131 loc) · 4.12 KB
/
init.lua
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---@class Snacks: snacks.plugins
local M = {}
setmetatable(M, {
__index = function(t, k)
---@diagnostic disable-next-line: no-unknown
t[k] = require("snacks." .. k)
return rawget(t, k)
end,
})
_G.Snacks = M
---@class snacks.Config.base
---@field example? string
---@field config? fun(opts: table, defaults: table)
---@class snacks.Config: snacks.plugins.Config
---@field styles? table<string, snacks.win.Config>
local config = {
bigfile = { enabled = false },
dashboard = { enabled = false },
indent = { enabled = false },
input = { enabled = false },
notifier = { enabled = false },
quickfile = { enabled = false },
scope = { enabled = false },
scroll = { enabled = false },
statuscolumn = { enabled = false },
styles = {},
words = { enabled = false },
}
---@class snacks.config: snacks.Config
M.config = setmetatable({}, {
__index = function(_, k)
return config[k]
end,
})
--- Get an example config from the docs/examples directory.
---@param snack string
---@param name string
---@param opts? table
function M.config.example(snack, name, opts)
local path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":h:h:h") .. "/docs/examples/" .. snack .. ".lua"
local ok, ret = pcall(function()
return loadfile(path)().examples[name] or error(("`%s` not found"):format(name))
end)
if not ok then
M.notify.error(("Failed to load `%s.%s`:\n%s"):format(snack, name, ret))
end
return ok and vim.tbl_deep_extend("force", {}, vim.deepcopy(ret), opts or {}) or {}
end
---@generic T: table
---@param snack string
---@param defaults T
---@param ... T[]
---@return T
function M.config.get(snack, defaults, ...)
local merge, todo = {}, { defaults, config[snack], ... }
for i = 1, select("#", ...) + 2 do
local v = todo[i] --[[@as snacks.Config.base]]
if type(v) == "table" then
if v.example then
table.insert(merge, vim.deepcopy(M.config.example(snack, v.example)))
v.example = nil
end
table.insert(merge, vim.deepcopy(v))
end
end
local ret = #merge == 1 and merge[1] or vim.tbl_deep_extend("force", unpack(merge)) --[[@as snacks.Config.base]]
if type(ret.config) == "function" then
ret.config(ret, defaults)
end
return ret
end
--- Register a new window style config.
---@param name string
---@param defaults snacks.win.Config
function M.config.style(name, defaults)
config.styles[name] = vim.tbl_deep_extend("force", vim.deepcopy(defaults), config.styles[name] or {})
end
M.did_setup = false
M.did_setup_after_vim_enter = false
---@param opts snacks.Config?
function M.setup(opts)
if M.did_setup then
return vim.notify("snacks.nvim is already setup", vim.log.levels.ERROR, { title = "snacks.nvim" })
end
M.did_setup = true
if vim.fn.has("nvim-0.9.4") ~= 1 then
return vim.notify("snacks.nvim requires Neovim >= 0.9.4", vim.log.levels.ERROR, { title = "snacks.nvim" })
end
-- enable all by default when config is passed
opts = opts or {}
for k in pairs(opts) do
opts[k].enabled = opts[k].enabled == nil or opts[k].enabled
end
config = vim.tbl_deep_extend("force", config, opts or {})
local events = {
BufReadPre = { "bigfile" },
BufReadPost = { "quickfile", "indent" },
LspAttach = { "words" },
UIEnter = { "dashboard", "scroll", "input", "scope" },
}
local function load(event)
for _, snack in ipairs(events[event] or {}) do
if M.config[snack] and M.config[snack].enabled then
(M[snack].setup or M[snack].enable)()
end
end
events[event] = nil
end
if vim.v.vim_did_enter == 1 then
M.did_setup_after_vim_enter = true
load("UIEnter")
end
vim.api.nvim_create_autocmd(vim.tbl_keys(events), {
group = vim.api.nvim_create_augroup("snacks", { clear = true }),
once = true,
nested = true,
callback = function(ev)
load(ev.event)
end,
})
if M.config.statuscolumn.enabled then
vim.o.statuscolumn = [[%!v:lua.require'snacks.statuscolumn'.get()]]
end
if M.config.notifier.enabled then
vim.notify = function(msg, level, o)
vim.notify = Snacks.notifier.notify
return Snacks.notifier.notify(msg, level, o)
end
end
end
return M