-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathinit.lua
More file actions
296 lines (257 loc) · 6.95 KB
/
init.lua
File metadata and controls
296 lines (257 loc) · 6.95 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
local api, fn = vim.api, vim.fn
local utils = require('dashboard.utils')
local ctx = {}
local db = {}
db.__index = db
db.__newindex = function(t, k, v)
rawset(t, k, v)
end
local function clean_ctx()
for k, _ in pairs(ctx) do
ctx[k] = nil
end
end
local function cache_dir()
local dir = utils.path_join(vim.fn.stdpath('cache'), 'dashboard')
if fn.isdirectory(dir) == 0 then
fn.mkdir(dir, 'p')
end
return dir
end
local function cache_path()
local dir = cache_dir()
return utils.path_join(dir, 'cache')
end
local function conf_cache_path()
return utils.path_join(cache_dir(), 'conf')
end
local function default_options()
return {
theme = 'hyper',
disable_move = false,
shortcut_type = 'letter',
shuffle_letter = false,
letter_list = 'abcdefghilmnopqrstuvwxyz',
buffer_name = 'Dashboard',
change_to_vcs_root = false,
config = {
week_header = {
enable = false,
concat = nil,
append = nil,
},
},
hide = {
statusline = true,
tabline = true,
},
preview = {
command = '',
file_path = nil,
file_height = 0,
file_width = 0,
},
}
end
local function buf_local()
local opts = {
['bufhidden'] = 'wipe',
['colorcolumn'] = '',
['foldcolumn'] = '0',
['matchpairs'] = '',
['buflisted'] = false,
['cursorcolumn'] = false,
['cursorline'] = false,
['list'] = false,
['number'] = false,
['relativenumber'] = false,
['spell'] = false,
['swapfile'] = false,
['readonly'] = false,
['filetype'] = 'dashboard',
['wrap'] = false,
['signcolumn'] = 'no',
}
for opt, val in pairs(opts) do
vim.opt_local[opt] = val
end
if fn.has('nvim-0.9') == 1 then
vim.opt_local.stc = ''
end
end
function db:new_file()
vim.cmd('enew')
end
function db:save_user_options()
self.user_cursor_line = vim.opt.cursorline:get()
self.user_laststatus_value = vim.opt.laststatus:get()
self.user_tabline_value = vim.opt.showtabline:get()
self.user_winbar_value = vim.opt.winbar:get()
end
function db:set_ui_options(opts)
if opts.hide.statusline then
vim.opt.laststatus = 0
end
if opts.hide.tabline then
vim.opt.showtabline = 0
end
if opts.hide.winbar then
vim.opt.winbar = ''
end
end
function db:restore_user_options(opts)
if self.user_cursor_line then
vim.opt.cursorline = self.user_cursor_line
end
if opts.hide.statusline and self.user_laststatus_value then
vim.opt.laststatus = tonumber(self.user_laststatus_value)
end
if opts.hide.tabline and self.user_tabline_value then
vim.opt.showtabline = tonumber(self.user_tabline_value)
end
if opts.hide.winbar and self.user_winbar_value then
vim.opt.winbar = self.user_winbar_value
end
end
function db:cache_opts()
if not self.opts then
return
end
local uv = vim.loop
local path = conf_cache_path()
if self.opts.config.shortcut then
for _, item in pairs(self.opts.config.shortcut) do
if type(item.action) == 'function' then
---@diagnostic disable-next-line: param-type-mismatch
local dump = assert(string.dump(item.action))
item.action = dump
end
end
end
if self.opts.config.project and type(self.opts.config.project.action) == 'function' then
---@diagnostic disable-next-line: param-type-mismatch
local dump = assert(string.dump(self.opts.config.project.action))
self.opts.config.project.action = dump
end
if self.opts.config.center then
for _, item in pairs(self.opts.config.center) do
if type(item.action) == 'function' then
---@diagnostic disable-next-line: param-type-mismatch
local dump = assert(string.dump(item.action))
item.action = dump
end
end
end
if self.opts.config.footer and type(self.opts.config.footer) == 'function' then
---@diagnostic disable-next-line: param-type-mismatch
local dump = assert(string.dump(self.opts.config.footer))
self.opts.config.footer = dump
end
local dump = vim.json.encode(self.opts)
uv.fs_open(path, 'w+', tonumber('664', 8), function(err, fd)
assert(not err, err)
---@diagnostic disable-next-line: redefined-local
uv.fs_write(fd, dump, 0, function(err, _)
assert(not err, err)
uv.fs_close(fd)
end)
end)
end
function db:get_opts(callback)
utils.async_read(
conf_cache_path(),
vim.schedule_wrap(function(data)
if not data or #data == 0 then
return
end
local obj = vim.json.decode(data)
if obj then
callback(obj)
end
end)
)
end
function db:load_theme(opts)
local config = vim.tbl_extend('force', opts.config, {
path = cache_path(),
bufnr = self.bufnr,
winid = self.winid,
confirm_key = opts.confirm_key or nil,
shortcut_type = opts.shortcut_type,
shuffle_letter = opts.shuffle_letter,
letter_list = opts.letter_list,
change_to_vcs_root = opts.change_to_vcs_root,
})
if #opts.preview.command > 0 then
config = vim.tbl_extend('force', config, opts.preview)
end
require('dashboard.theme.' .. opts.theme)(config)
self:set_ui_options(opts)
api.nvim_create_autocmd('VimResized', {
buffer = self.bufnr,
callback = function()
require('dashboard.theme.' .. opts.theme)(config)
vim.bo[self.bufnr].modifiable = false
end,
})
api.nvim_create_autocmd('BufEnter', {
callback = function(opt)
if vim.bo.filetype == 'dashboard' then
self:set_ui_options(opts)
return
end
local bufs = api.nvim_list_bufs()
bufs = vim.tbl_filter(function(k)
return vim.bo[k].filetype == 'dashboard'
end, bufs)
-- restore the user's UI settings is no dashboard buffers are visible
local wins = api.nvim_tabpage_list_wins(0)
wins = vim.tbl_filter(function(k)
return vim.tbl_contains(bufs, api.nvim_win_get_buf(k))
end, wins)
if #wins == 0 then
self:restore_user_options(opts)
end
-- clean up if there are no dashboard buffers at all
if #bufs == 0 then
self:cache_opts()
clean_ctx()
pcall(api.nvim_del_autocmd, opt.id)
end
end,
desc = '[Dashboard] clean dashboard data reduce memory',
})
end
-- create dashboard instance
function db:instance()
local mode = api.nvim_get_mode().mode
if mode == 'i' or not vim.bo.modifiable then
return
end
if not vim.o.hidden and vim.bo.modified then
--save before open
vim.cmd.write()
return
end
if not utils.buf_is_empty(0) then
self.bufnr = api.nvim_create_buf(false, true)
else
self.bufnr = api.nvim_get_current_buf()
end
self.winid = api.nvim_get_current_win()
api.nvim_win_set_buf(self.winid, self.bufnr)
self:save_user_options()
buf_local()
if self.opts then
self:load_theme(self.opts)
else
self:get_opts(function(obj)
self:load_theme(obj)
end)
end
end
function db.setup(opts)
opts = opts or {}
ctx.opts = vim.tbl_deep_extend('force', default_options(), opts)
end
return setmetatable(ctx, db)