Skip to content

Commit 6582bb9

Browse files
committed
intermediate commit
1 parent 90498cb commit 6582bb9

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

lua/popup/border.lua

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package.loaded['popup.border'] = nil
2-
31
local utils = require('popup.utils')
42

53
local Border = {}
@@ -80,7 +78,7 @@ function Border._create_lines(content_win_options, border_win_options)
8078
return border_lines
8179
end
8280

83-
function Border:new(content_buf_id, content_win_id, content_win_options, border_win_options)
81+
function Border:new(content_bufnr, content_win_id, content_win_options, border_win_options)
8482
assert(type(content_win_id) == 'number', "Must supply a valid win_id. It's possible you forgot to call with ':'")
8583

8684
border_win_options = utils.apply_defaults(border_win_options, {
@@ -104,15 +102,15 @@ function Border:new(content_buf_id, content_win_id, content_win_options, border_
104102
obj._border_win_options = border_win_options
105103

106104

107-
obj.buf_id = vim.api.nvim_create_buf(false, true)
108-
assert(obj.buf_id, "Failed to create border buffer")
105+
obj.bufnr = vim.api.nvim_create_buf(false, true)
106+
assert(obj.bufnr, "Failed to create border buffer")
109107

110108
obj.contents = Border._create_lines(content_win_options, border_win_options)
111-
vim.api.nvim_buf_set_lines(obj.buf_id, 0, -1, false, obj.contents)
109+
vim.api.nvim_buf_set_lines(obj.bufnr, 0, -1, false, obj.contents)
112110

113111
local thickness = border_win_options.border_thickness
114112

115-
obj.win_id = vim.api.nvim_open_win(obj.buf_id, false, {
113+
obj.win_id = vim.api.nvim_open_win(obj.bufnr, false, {
116114
anchor = content_win_options.anchor,
117115
relative = content_win_options.relative,
118116
style = "minimal",
@@ -122,15 +120,31 @@ function Border:new(content_buf_id, content_win_id, content_win_options, border_
122120
height = content_win_options.height + thickness.top + thickness.bot,
123121
})
124122

125-
local silent = true
126-
vim.cmd(
127-
string.format(
128-
"autocmd WinLeave,BufLeave,BufDelete <buffer=%s> ++once ++nested %s call nvim_win_close(%s, v:true)",
129-
content_buf_id,
130-
(silent and "silent!") or "",
131-
obj.win_id
132-
)
133-
)
123+
-- local silent = true
124+
-- vim.cmd(
125+
-- string.format(
126+
-- "autocmd WinLeave,BufLeave,BufDelete %s <buffer=%s> ++once ++nested :call popup#close_win(%s, v:true)",
127+
-- (silent and "<silent>") or "",
128+
-- content_bufnr,
129+
-- obj.win_id
130+
-- )
131+
-- )
132+
-- vim.cmd(string.format(
133+
-- "autocmd WinClosed,BufLeave,BufDelete,WinLeave <silent> <buffer=%s> ++once ++nested :call popup#close_win(%s, v:true)",
134+
-- content_bufnr,
135+
-- obj.win_id
136+
-- ))
137+
vim.cmd(string.format(
138+
"autocmd BufLeave,BufDelete <buffer=%s> ++nested ++once :call popup#close_related_win(%s, %s)",
139+
content_bufnr,
140+
content_win_id,
141+
obj.win_id))
142+
143+
vim.cmd(string.format(
144+
"autocmd WinClosed,WinLeave <buffer=%s> ++nested ++once :call popup#close_win(%s, v:true)",
145+
content_bufnr,
146+
obj.win_id))
147+
134148

135149
setmetatable(obj, Border)
136150

lua/popup/init.lua

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,11 @@ function popup.create(what, vim_options)
178178
-- TODO: Handle word, WORD, expr, and the range functions... which seem hard?
179179
end
180180
else
181+
local silent = false
181182
vim.cmd(
182183
string.format(
183-
"autocmd BufLeave,BufDelete <buffer=%s> ++once call nvim_win_close(%s, v:false)",
184+
"autocmd BufLeave,BufDelete %s <buffer=%s> ++nested call popup#close_win(%s, v:true)",
185+
(silent and "<silent>") or '',
184186
buf,
185187
win_id
186188
)
@@ -190,7 +192,7 @@ function popup.create(what, vim_options)
190192
if vim_options.time then
191193
local timer = vim.loop.new_timer()
192194
timer:start(vim_options.time, 0, vim.schedule_wrap(function()
193-
vim.fn.nvim_win_close(win_id, false)
195+
vim.fn['popup#close_win'](win_id, false)
194196
end))
195197
end
196198

@@ -297,8 +299,9 @@ function popup.create(what, vim_options)
297299
border_options.title = vim_options.title
298300
end
299301

302+
local border = nil
300303
if should_show_border then
301-
Border:new(buf, win_id, win_opts, border_options)
304+
border = Border:new(buf, win_id, win_opts, border_options)
302305
end
303306

304307
if vim_options.highlight then
@@ -309,7 +312,9 @@ function popup.create(what, vim_options)
309312
-- but actually has some extra metadata about it.
310313
--
311314
-- This would make `hidden` a lot easier to manage
312-
return win_id
315+
return win_id, {
316+
border = border,
317+
}
313318
end
314319

315320
function popup.show(self, asdf)

plugin/popup.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
" Auto load the compat module, so that we don't have to write any weird code
22
let s:lua_folder = fnamemodify(expand("<sfile>"), ":h:h") . "/lua"
33
call execute(printf('luafile %s/popup/_compat.lua', s:lua_folder))
4+
5+
function! popup#close_win(win_id, force) abort
6+
try
7+
call nvim_win_close(a:win_id, a:force)
8+
catch
9+
endtry
10+
endfunction
11+
12+
function! popup#close_related_win(parent_win_id, child_win_id) abort
13+
call popup#close_win(a:parent_win_id, v:true)
14+
call popup#close_win(a:child_win_id, v:true)
15+
endfunction

0 commit comments

Comments
 (0)