fix(float): close preview float window when no selected #29745

Problem: Float preview window still exist when back at original.
Or no info item is selected.

Solution: if selected is -1 or no info is selected, if float preview
window exist close it first.
This commit is contained in:
glepnir 2024-12-11 21:20:10 +08:00 committed by GitHub
parent 3dfb9e6f60
commit ff1791c9e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 112 additions and 47 deletions

View File

@ -941,11 +941,14 @@ static bool pum_set_selected(int n, int repeat)
pum_selected = n;
unsigned cur_cot_flags = get_cot_flags();
bool use_float = (cur_cot_flags & kOptCotFlagPopup) != 0;
// when new leader add and info window is shown and no selected we still
// need use the first index item to update the info float window position.
bool force_select = use_float && pum_selected < 0 && win_float_find_preview();
if (force_select) {
pum_selected = 0;
// Close the floating preview window if 'selected' is -1, indicating a return to the original
// state. It is also closed when the selected item has no corresponding info item.
if (use_float && (pum_selected < 0 || pum_array[pum_selected].pum_info == NULL)) {
win_T *wp = win_float_find_preview();
if (wp) {
win_close(wp, true, true);
}
}
if ((pum_selected >= 0) && (pum_selected < pum_size)) {
@ -1164,11 +1167,6 @@ static bool pum_set_selected(int n, int repeat)
}
}
// restore before selected value
if (force_select) {
pum_selected = n;
}
return resized;
}

View File

@ -1684,25 +1684,26 @@ describe('builtin popupmenu', function()
}
end
-- info window position should be adjusted when new leader add
feed('<C-P>o')
-- delete one character make the pum width smaller than before
-- info window position should be adjusted when popupmenu width changed
feed('<BS>')
if multigrid then
screen:expect {
screen:expect({
grid = [[
## grid 1
[2:----------------------------------------]|*10
[3:----------------------------------------]|
## grid 2
o^ |
{1:~ }|*9
## grid 3
{2:-- }{8:Back at original} |
## grid 4
{n:1info}|
{n: }|
## grid 5
{n:one }|
]],
## grid 1
[2:----------------------------------------]|*10
[3:----------------------------------------]|
## grid 2
on^ |
{1:~ }|*9
## grid 3
{2:-- }{5:match 1 of 3} |
## grid 4
{n:1info}|
{n: }|
## grid 5
{s:one }|
]],
float_pos = {
[5] = { -1, 'NW', 2, 1, 0, false, 100 },
[4] = { 1001, 'NW', 1, 1, 15, false, 50 },
@ -1713,7 +1714,7 @@ describe('builtin popupmenu', function()
topline = 0,
botline = 2,
curline = 0,
curcol = 1,
curcol = 2,
linecount = 1,
sum_scroll_delta = 0,
},
@ -1727,22 +1728,88 @@ describe('builtin popupmenu', function()
sum_scroll_delta = 0,
},
},
}
win_viewport_margins = {
[2] = {
bottom = 0,
left = 0,
right = 0,
top = 0,
win = 1000,
},
[4] = {
bottom = 0,
left = 0,
right = 0,
top = 0,
win = 1001,
},
},
})
else
screen:expect {
screen:expect({
grid = [[
o^ |
{n:one 1info}{1: }|
{1:~ }{n: }{1: }|
{1:~ }|*7
{2:-- }{8:Back at original} |
]],
}
on^ |
{s:one }{n:1info}{1: }|
{1:~ }{n: }{1: }|
{1:~ }|*7
{2:-- }{5:match 1 of 3} |
]],
})
end
-- when back to original the preview float should be closed.
feed('<C-P>')
if multigrid then
screen:expect({
grid = [[
## grid 1
[2:----------------------------------------]|*10
[3:----------------------------------------]|
## grid 2
on^ |
{1:~ }|*9
## grid 3
{2:-- }{8:Back at original} |
## grid 5
{n:one }|
]],
float_pos = {
[5] = { -1, 'NW', 2, 1, 0, false, 100 },
},
win_viewport = {
[2] = {
win = 1000,
topline = 0,
botline = 2,
curline = 0,
curcol = 2,
linecount = 1,
sum_scroll_delta = 0,
},
},
win_viewport_margins = {
[2] = {
bottom = 0,
left = 0,
right = 0,
top = 0,
win = 1000,
},
},
})
else
screen:expect({
grid = [[
on^ |
{n:one }{1: }|
{1:~ }|*8
{2:-- }{8:Back at original} |
]],
})
end
-- test nvim__complete_set_info
feed('<ESC>cc<C-X><C-O><C-N><C-N>')
vim.uv.sleep(10)
feed('<ESC>S<C-X><C-O><C-N><C-N>')
if multigrid then
screen:expect {
grid = [[
@ -1758,13 +1825,13 @@ describe('builtin popupmenu', function()
{n:one }|
{n:two }|
{s:looooooooooooooong }|
## grid 6
## grid 7
{n:3info}|
{n: }|
]],
float_pos = {
[5] = { -1, 'NW', 2, 1, 0, false, 100 },
[6] = { 1002, 'NW', 1, 1, 19, false, 50 },
[7] = { 1003, 'NW', 1, 1, 19, false, 50 },
},
win_viewport = {
[2] = {
@ -1776,8 +1843,8 @@ describe('builtin popupmenu', function()
linecount = 1,
sum_scroll_delta = 0,
},
[6] = {
win = 1002,
[7] = {
win = 1003,
topline = 0,
botline = 2,
curline = 0,
@ -1819,12 +1886,12 @@ describe('builtin popupmenu', function()
{s: one }|
{n: two }|
{n: looooooooooooooong }|
## grid 7
## grid 8
{n:1info}|
{n: }|
]],
float_pos = {
[7] = { 1003, 'NW', 1, 1, 14, false, 50 },
[8] = { 1004, 'NW', 1, 1, 14, false, 50 },
[5] = { -1, 'NW', 2, 1, 19, false, 100 },
},
win_viewport = {
@ -1837,8 +1904,8 @@ describe('builtin popupmenu', function()
linecount = 1,
sum_scroll_delta = 0,
},
[7] = {
win = 1003,
[8] = {
win = 1004,
topline = 0,
botline = 2,
curline = 0,