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; pum_selected = n;
unsigned cur_cot_flags = get_cot_flags(); unsigned cur_cot_flags = get_cot_flags();
bool use_float = (cur_cot_flags & kOptCotFlagPopup) != 0; 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. // Close the floating preview window if 'selected' is -1, indicating a return to the original
bool force_select = use_float && pum_selected < 0 && win_float_find_preview(); // state. It is also closed when the selected item has no corresponding info item.
if (force_select) { if (use_float && (pum_selected < 0 || pum_array[pum_selected].pum_info == NULL)) {
pum_selected = 0; win_T *wp = win_float_find_preview();
if (wp) {
win_close(wp, true, true);
}
} }
if ((pum_selected >= 0) && (pum_selected < pum_size)) { 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; return resized;
} }

View File

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