From 66161c1a3ed58eb2477a80253cdcf042e365d262 Mon Sep 17 00:00:00 2001 From: glepnir Date: Mon, 13 May 2024 16:16:49 +0800 Subject: [PATCH] fix wrong action preview window position --- lua/lspsaga/codeaction/preview.lua | 39 ++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lua/lspsaga/codeaction/preview.lua b/lua/lspsaga/codeaction/preview.lua index a72f4ba8c..64e97a8c1 100644 --- a/lua/lspsaga/codeaction/preview.lua +++ b/lua/lspsaga/codeaction/preview.lua @@ -109,23 +109,42 @@ local function create_preview_win(content, main_winid) else opt.width = content_width < win_conf.width and win_conf.width or content_width end - local winheight = api.nvim_win_get_height(win_conf.win) - local win_margin = config.ui.border == 'none' and 0 or 2 - if win_conf.anchor:find('^S') then - opt.row = util.is_ten and win_conf.row - 3 or win_conf.row[false] - win_conf.height - win_margin - max_height = util.is_ten and win_conf.row or win_conf.row[false] - win_conf.height - elseif win_conf.anchor:find('^N') then - opt.row = util.is_ten and win_conf.row + 3 or win_conf.row[false] + win_conf.height + win_margin - max_height = winheight - opt.row + local margin = config.ui.border == 'none' and 0 or 2 + local north = win_conf.anchor:sub(1, 1) == 'N' + local row = util.is_ten and win_conf.row or win_conf.row[false] + local valid_top_height = north and row or row - win_conf.height - margin + local valid_bot_height = north and winheight - row - win_conf.height - margin + or winheight - row - margin + local new_win_height = #content + margin + -- action is NW under cursor and top is enough to show preview + if north then + if valid_top_height > new_win_height then + opt.anchor = 'SW' + opt.row = row + opt.height = math.min(valid_top_height, #content) + elseif valid_bot_height > new_win_height then + opt.anchor = 'NW' + opt.row = row + win_conf.height + margin + opt.height = math.min(valid_bot_height, #content) + end + else + if valid_bot_height > new_win_height then + opt.anchor = 'NW' + opt.row = row + opt.height = math.min(valid_bot_height, #content) + else + opt.anchor = 'SW' + opt.row = row - win_conf.height - margin + opt.height = math.min(valid_top_height, #content) + end end - opt.height = math.min(max_height, #content) - if config.ui.title then opt.title = { { 'Action Preview', 'ActionPreviewTitle' } } opt.title_pos = 'center' end + print(vim.inspect(opt)) preview_buf, preview_winid = win :new_float(opt, false, true)