From e4eede11cbd0b0c240378ffb059d9db896b57e76 Mon Sep 17 00:00:00 2001 From: Dmtri <0xDmtri@protonmail.com> Date: Fri, 27 Dec 2024 16:31:54 +0100 Subject: [PATCH 1/5] try sorting diagnostic severity --- lua/lspsaga/diagnostic/show.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/lspsaga/diagnostic/show.lua b/lua/lspsaga/diagnostic/show.lua index 494129b0f..d22bd6674 100644 --- a/lua/lspsaga/diagnostic/show.lua +++ b/lua/lspsaga/diagnostic/show.lua @@ -36,6 +36,11 @@ end ---single linked list local function generate_list(entrys) + -- Sort diagnostics by severity (ascending: Error -> Warning -> Info -> Hint) + table.sort(entrys, function(a, b) + return a.severity < b.severity + end) + local list = new_node() local curnode From ae643d13a13f046692d210a95defcf9e1d01ccd8 Mon Sep 17 00:00:00 2001 From: Dmtri <0xDmtri@protonmail.com> Date: Fri, 27 Dec 2024 16:46:12 +0100 Subject: [PATCH 2/5] improve severity sort --- lua/lspsaga/diagnostic/show.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lua/lspsaga/diagnostic/show.lua b/lua/lspsaga/diagnostic/show.lua index d22bd6674..0b926163f 100644 --- a/lua/lspsaga/diagnostic/show.lua +++ b/lua/lspsaga/diagnostic/show.lua @@ -36,10 +36,16 @@ end ---single linked list local function generate_list(entrys) - -- Sort diagnostics by severity (ascending: Error -> Warning -> Info -> Hint) - table.sort(entrys, function(a, b) - return a.severity < b.severity - end) + -- Safely check if severity_sort is enabled + local diagnostic_config = vim.diagnostic.config and vim.diagnostic.config() + local severity_sort_enabled = diagnostic_config and diagnostic_config.severity_sort + + if severity_sort_enabled then + -- Sort diagnostics by severity (ascending: Error -> Warning -> Info -> Hint) + table.sort(entrys, function(a, b) + return a.severity < b.severity + end) + end local list = new_node() From 538f424cbe522b912e0522f5cbe00f6d5e56a6d7 Mon Sep 17 00:00:00 2001 From: Dmtri <0xDmtri@protonmail.com> Date: Fri, 27 Dec 2024 17:25:41 +0100 Subject: [PATCH 3/5] sort by severity, line and col --- lua/lspsaga/diagnostic/show.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lua/lspsaga/diagnostic/show.lua b/lua/lspsaga/diagnostic/show.lua index 0b926163f..a2f3f260a 100644 --- a/lua/lspsaga/diagnostic/show.lua +++ b/lua/lspsaga/diagnostic/show.lua @@ -41,8 +41,17 @@ local function generate_list(entrys) local severity_sort_enabled = diagnostic_config and diagnostic_config.severity_sort if severity_sort_enabled then - -- Sort diagnostics by severity (ascending: Error -> Warning -> Info -> Hint) + -- Sort diagnostics by severity, then by line number, then by column number table.sort(entrys, function(a, b) + if a.severity == b.severity then + if a.lnum == b.lnum then + -- Sort by column if severity and line are equal + return a.col < b.col + end + -- Sort by line number if severities are equal + return a.lnum < b.lnum + end + -- Otherwise, sort by severity (ascending: Error -> Warning -> Info -> Hint) return a.severity < b.severity end) end From 6f89bd335c1fa8b582f44e7615c9522935d156fb Mon Sep 17 00:00:00 2001 From: Dmtri <0xDmtri@protonmail.com> Date: Fri, 27 Dec 2024 21:05:11 +0100 Subject: [PATCH 4/5] make sorting async --- lua/lspsaga/diagnostic/show.lua | 62 ++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/lua/lspsaga/diagnostic/show.lua b/lua/lspsaga/diagnostic/show.lua index a2f3f260a..d3fa8de63 100644 --- a/lua/lspsaga/diagnostic/show.lua +++ b/lua/lspsaga/diagnostic/show.lua @@ -34,30 +34,8 @@ local function new_node() } end ----single linked list -local function generate_list(entrys) - -- Safely check if severity_sort is enabled - local diagnostic_config = vim.diagnostic.config and vim.diagnostic.config() - local severity_sort_enabled = diagnostic_config and diagnostic_config.severity_sort - - if severity_sort_enabled then - -- Sort diagnostics by severity, then by line number, then by column number - table.sort(entrys, function(a, b) - if a.severity == b.severity then - if a.lnum == b.lnum then - -- Sort by column if severity and line are equal - return a.col < b.col - end - -- Sort by line number if severities are equal - return a.lnum < b.lnum - end - -- Otherwise, sort by severity (ascending: Error -> Warning -> Info -> Hint) - return a.severity < b.severity - end) - end - +local function create_linked_list(entrys) local list = new_node() - local curnode for _, item in ipairs(entrys) do if #list.diags == 0 then @@ -73,6 +51,37 @@ local function generate_list(entrys) return list end +local function sort_entries(entrys) + table.sort(entrys, function(a, b) + if a.severity ~= b.severity then + return a.severity < b.severity + elseif a.lnum ~= b.lnum then + return a.lnum < b.lnum + else + return a.col < b.col + end + end) +end + +---single linked list +local function generate_list(entrys, callback) + local diagnostic_config = vim.diagnostic.config and vim.diagnostic.config() + local severity_sort_enabled = diagnostic_config and diagnostic_config.severity_sort + + if severity_sort_enabled then + vim.defer_fn(function() + sort_entries(entrys) + local list = create_linked_list(entrys) + if callback then + callback(list) + end + end, 0) + return nil + else + return create_linked_list(entrys) + end +end + local function find_node(list, lnum) local curnode = list while curnode do @@ -365,8 +374,11 @@ function sd:show_diagnostics(opt) if next(entrys) == nil then return end - opt.entrys_list = generate_list(entrys) - self:show(opt) + + generate_list(entrys, function(sorted_list) + opt.entrys_list = sorted_list + self:show(opt) + end) end return setmetatable(ctx, sd) From db7c5061611f569434a17631d88813d11d94aa57 Mon Sep 17 00:00:00 2001 From: Dmtri <0xDmtri@protonmail.com> Date: Mon, 27 Jan 2025 01:13:49 +0100 Subject: [PATCH 5/5] fix floating pane --- lua/lspsaga/codeaction/preview.lua | 61 ++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/lua/lspsaga/codeaction/preview.lua b/lua/lspsaga/codeaction/preview.lua index 18b199da3..280fed772 100644 --- a/lua/lspsaga/codeaction/preview.lua +++ b/lua/lspsaga/codeaction/preview.lua @@ -90,6 +90,7 @@ local preview_buf, preview_winid ---create a preview window according given window ---default is under the given window local function create_preview_win(content, main_winid) + -- Get the configuration of the main window local win_conf = api.nvim_win_get_config(main_winid) local opt = { relative = win_conf.relative, @@ -98,45 +99,75 @@ local function create_preview_win(content, main_winid) anchor = win_conf.anchor, focusable = false, } + + -- Determine content width and apply constraints local content_width = util.get_max_content_length(content) local max_win_width = api.nvim_win_get_width(win_conf.win) - if content_width < win_conf.width then - opt.width = win_conf.width - else - opt.width = math.min(max_win_width, content_width) - end + opt.width = math.min(max_win_width, math.max(content_width, win_conf.width)) + + -- Get dimensions of the main window local winheight = api.nvim_win_get_height(win_conf.win) 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 - 1 or row - win_conf.height - margin - 1 - local valid_bot_height = north and winheight - row - win_conf.height - margin - or winheight - row - margin + + -- Calculate available space above (valid_top_height) and below (valid_bot_height) + local valid_top_height = math.max(0, row - margin) + local valid_bot_height = north and (winheight - row - win_conf.height - margin) + or (winheight - row - margin) + + -- Determine the preview window height local new_win_height = #content + margin - -- action is NW under cursor and top is enough to show preview - local east_or_west = win_conf.anchor:sub(2, 2) new_win_height = math.min(new_win_height, math.max(valid_bot_height, valid_top_height)) + + -- Adjust anchor and position based on available space + local east_or_west = win_conf.anchor:sub(2, 2) + if north then if valid_top_height >= new_win_height then opt.anchor = 'S' .. east_or_west opt.row = row - opt.height = math.min(valid_top_height, #content) + opt.height = new_win_height elseif valid_bot_height >= new_win_height then opt.anchor = 'N' .. east_or_west opt.row = row + win_conf.height + margin - opt.height = math.min(valid_bot_height, #content) - 2 + opt.height = new_win_height + else + -- Fallback: Fit within whichever space is larger + if valid_top_height > valid_bot_height then + opt.anchor = 'S' .. east_or_west + opt.row = row + opt.height = valid_top_height + else + opt.anchor = 'N' .. east_or_west + opt.row = row + win_conf.height + margin + opt.height = valid_bot_height + end end else if valid_bot_height >= new_win_height then opt.anchor = 'N' .. east_or_west opt.row = row - opt.height = math.min(valid_bot_height, #content) - else + opt.height = new_win_height + elseif valid_top_height >= new_win_height then opt.anchor = 'S' .. east_or_west opt.row = row - win_conf.height - margin - opt.height = math.min(valid_top_height, #content) + opt.height = new_win_height + else + -- Fallback: Fit within whichever space is larger + if valid_bot_height > valid_top_height then + opt.anchor = 'N' .. east_or_west + opt.row = row + opt.height = valid_bot_height + else + opt.anchor = 'S' .. east_or_west + opt.row = row - win_conf.height - margin + opt.height = valid_top_height + end end end + + -- Create the preview window with calculated options and set content/buffer options. preview_buf, preview_winid = win :new_float(opt, false, true) :setlines(content)