-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathpreview.lua
More file actions
139 lines (121 loc) · 3.89 KB
/
preview.lua
File metadata and controls
139 lines (121 loc) · 3.89 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
local api = vim.api
local db = require('dashboard')
local view = {}
function view:open_window(opt)
local row = math.floor(opt.height / 5)
local col = math.floor((vim.o.columns - opt.width) / 2)
local opts = {
relative = 'editor',
row = row,
col = col,
width = opt.width,
height = opt.height,
style = 'minimal',
noautocmd = true,
}
self.bufnr = api.nvim_create_buf(false, true)
api.nvim_buf_set_option(self.bufnr, 'filetype', 'dashboardpreview')
self.winid = api.nvim_open_win(self.bufnr, false, opts)
if vim.fn.has('nvim-0.8') == 1 then
local normal = api.nvim_get_hl_by_name('Normal', true)
pcall(api.nvim_set_hl, 0, 'DashboardPreview', normal)
else
api.nvim_set_hl(0, 'DashboardPreview', { bg = 'none' })
end
api.nvim_win_set_option(self.winid, 'winhl', 'Normal:DashboardPreview')
return { self.bufnr, self.winid }
end
function view:close_preview_window()
if self.bufnr and api.nvim_buf_is_loaded(self.bufnr) then
api.nvim_buf_delete(self.bufnr, { force = true })
self.bufnr = nil
end
if self.winid and api.nvim_win_is_valid(self.winid) then
api.nvim_win_close(self.winid, true)
self.winid = nil
end
end
function view:preview_events()
local group =
api.nvim_create_augroup('DashboardClosePreview' .. self.preview_bufnr, { clear = true })
--refresh the preview window col position.
local function refresh_preview_wincol()
if not self.preview_winid or not api.nvim_win_is_valid(self.preview_winid) then
return
end
local winconfig = api.nvim_win_get_config(self.preview_winid)
local cur_width = api.nvim_win_get_width(self.main_winid)
if cur_width ~= self.win_width then
local wins = api.nvim_list_wins()
if #wins == 2 then
local scol = bit.rshift(vim.o.columns, 1) - bit.rshift(winconfig.width, 1)
winconfig.col[false] = scol
api.nvim_win_set_config(self.preview_winid, winconfig)
self.win_width = cur_width
return
end
if #wins == 3 then
local new_win = vim.tbl_filter(function(k)
return k ~= self.main_winid and k ~= self.preview_winid
end, wins)[1]
winconfig.col[false] = winconfig.col[false] + api.nvim_win_get_width(new_win)
api.nvim_win_set_config(self.preview_winid, winconfig)
self.win_width = cur_width
end
end
end
local function winresized()
api.nvim_create_autocmd('WinResized', {
group = group,
callback = function()
refresh_preview_wincol()
end,
desc = ' Dashboard preview window resized for nvim 0.9',
})
end
api.nvim_create_autocmd('VimResized', {
group = group,
callback = function()
refresh_preview_wincol()
end,
})
if vim.fn.has('nvim-0.9') == 1 then
winresized()
else
---@deprecated when 0.9 version release remove
api.nvim_create_autocmd('BufEnter', {
group = group,
callback = function()
refresh_preview_wincol()
end,
desc = 'dashboard preview window resize for neovim 0.8+ version',
})
end
end
function view:open_preview(opt)
self.preview_bufnr, self.preview_winid = unpack(view:open_window(opt))
api.nvim_buf_call(self.preview_bufnr, function()
vim.fn.termopen(opt.cmd, {
on_exit = function() end,
})
end)
self.main_winid = api.nvim_get_current_win()
self.win_width = api.nvim_win_get_width(self.main_winid)
api.nvim_create_autocmd('BufWipeout', {
buffer = db.bufnr,
callback = function()
if self.winid and api.nvim_win_is_valid(self.preview_winid) then
api.nvim_win_close(self.preview_winid, true)
self.preview_winid = nil
self.preview_bufnr = nil
self.main_winid = nil
self.win_width = nil
end
end,
once = true,
desc = 'make preview have same lifetime with dashboard buffer',
})
self:preview_events()
return self.preview_bufnr, self.preview_winid
end
return view