From ae6f1e518cee47be569fb7e8d69aed9624e1d290 Mon Sep 17 00:00:00 2001 From: glepnir Date: Sun, 19 May 2024 10:21:45 +0800 Subject: [PATCH] feat(ui): better ui on diangostic jump --- lua/lspsaga/codeaction/preview.lua | 6 ----- lua/lspsaga/diagnostic/init.lua | 42 ++++++++++++++++++------------ lua/lspsaga/diagnostic/show.lua | 1 - lua/lspsaga/highlight.lua | 8 ++++++ lua/lspsaga/init.lua | 1 + 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/lua/lspsaga/codeaction/preview.lua b/lua/lspsaga/codeaction/preview.lua index 2c625439b..1d793b5c5 100644 --- a/lua/lspsaga/codeaction/preview.lua +++ b/lua/lspsaga/codeaction/preview.lua @@ -140,12 +140,6 @@ local function create_preview_win(content, main_winid) opt.height = math.min(valid_top_height, #content) end end - - if config.ui.title then - opt.title = { { 'Action Preview', 'ActionPreviewTitle' } } - opt.title_pos = 'center' - end - preview_buf, preview_winid = win :new_float(opt, false, true) :setlines(content) diff --git a/lua/lspsaga/diagnostic/init.lua b/lua/lspsaga/diagnostic/init.lua index 37d3baef6..1d98be0fb 100644 --- a/lua/lspsaga/diagnostic/init.lua +++ b/lua/lspsaga/diagnostic/init.lua @@ -32,6 +32,22 @@ local function get_num() return line:match('%*%*(%d+)%*%*') end +local function gen_float_title(counts) + local t = {} + for i, v in ipairs(counts) do + if v > 0 then + local hi = 'Diagnostic' .. vim.diagnostic.severity[i] + t[#t + 1] = { config.ui.button[1], hi } + t[#t + 1] = { + (vim.diagnostic.severity[i]:sub(1, 1) .. ':%s'):format(v), + hi .. 'Reverse', + } + t[#t + 1] = { config.ui.button[2], hi } + end + end + return t +end + ---get the line or cursor diagnostics ---@param opt table function diag:get_diagnostic(opt) @@ -61,8 +77,10 @@ function diag:get_diagnostic(opt) return vim.diagnostic.get() end -function diag:code_action_cb(action_tuples, enriched_ctx) +function diag:code_action_cb(action_tuples, enriched_ctx, counts) local win_conf = api.nvim_win_get_config(self.float_winid) + win_conf.title = gen_float_title(counts) + api.nvim_win_set_config(self.float_winid, win_conf) local contents = { util.gen_truncate_line(win_conf.width), config.ui.actionfix .. 'Actions', @@ -166,7 +184,9 @@ end function diag:get_cursor_diagnostic() local diags = diag:get_diagnostic({ cursor = true }) local res = {} + local counts = { 0, 0, 0, 0 } for _, entry in ipairs(diags) do + counts[entry.severity] = counts[entry.severity] + 1 res[#res + 1] = { code = entry.code or nil, message = entry.message, @@ -190,7 +210,7 @@ function diag:get_cursor_diagnostic() } end - return res + return res, counts end function diag:do_code_action(action_tuples, enriched_ctx) @@ -218,17 +238,6 @@ function diag:clean_data() clean_ctx() end -function diag:get_diag_counts(entrys) - --E W I W - local counts = { 0, 0, 0, 0 } - - for _, item in ipairs(entrys) do - counts[item.severity] = counts[item.severity] + 1 - end - - return counts -end - local original_open_float = vim.diagnostic.open_float ---@diagnostic disable-next-line: duplicate-set-field vim.diagnostic.open_float = function(opts, ...) @@ -257,7 +266,7 @@ function diag:goto_pos(pos, opts) return end (is_forward and vim.diagnostic.goto_next or vim.diagnostic.goto_prev)({ - float = { border = 'rounded' }, + float = { border = config.ui.border, header = '' }, }) util.valid_markdown_parser() require('lspsaga.beacon').jump_beacon({ entry.lnum, entry.col }, #api.nvim_get_current_line()) @@ -281,8 +290,9 @@ function diag:goto_pos(pos, opts) return end local curbuf = api.nvim_get_current_buf() + local diagnostics, counts = self:get_cursor_diagnostic() act:send_request(curbuf, { - context = { diagnostics = self:get_cursor_diagnostic() }, + context = { diagnostics = diagnostics }, range = { start = { entry.lnum + 1, (entry.col or 1) }, ['end'] = { entry.lnum + 1, (entry.col or 1) }, @@ -294,7 +304,7 @@ function diag:goto_pos(pos, opts) end vim.bo[self.float_bufnr].modifiable = true self.main_buf = curbuf - self:code_action_cb(action_tuples, enriched_ctx) + self:code_action_cb(action_tuples, enriched_ctx, counts) vim.bo[self.float_bufnr].modifiable = false end) end) diff --git a/lua/lspsaga/diagnostic/show.lua b/lua/lspsaga/diagnostic/show.lua index bcc1606cb..494129b0f 100644 --- a/lua/lspsaga/diagnostic/show.lua +++ b/lua/lspsaga/diagnostic/show.lua @@ -297,7 +297,6 @@ function sd:show(opt) if i == 1 then ---@diagnostic disable-next-line: param-type-mismatch local fname = fn.fnamemodify(api.nvim_buf_get_name(tonumber(entry.bufnr)), ':t') - -- local counts = diag:get_diag_counts(curnode.diags) local text = ' ' .. fname nvim_buf_set_lines(self.bufnr, count, -1, false, { text }) nvim_buf_set_extmark(self.bufnr, ns, count, 0, { diff --git a/lua/lspsaga/highlight.lua b/lua/lspsaga/highlight.lua index 084d9a852..9ac5559e3 100644 --- a/lua/lspsaga/highlight.lua +++ b/lua/lspsaga/highlight.lua @@ -69,6 +69,14 @@ local function init_highlight() for _, item in pairs(kind) do api.nvim_set_hl(0, 'Saga' .. item[1], { link = item[3], default = true }) end + + for _, v in ipairs(vim.diagnostic.severity) do + local color = api.nvim_get_hl(0, { name = 'Diagnostic' .. v }) + api.nvim_set_hl(0, 'Diagnostic' .. v .. 'Reverse', { + bg = color.fg, + fg = 'Black', + }) + end end return { diff --git a/lua/lspsaga/init.lua b/lua/lspsaga/init.lua index f7dc32c84..58e46241e 100644 --- a/lua/lspsaga/init.lua +++ b/lua/lspsaga/init.lua @@ -14,6 +14,7 @@ local default_config = { actionfix = ' ', lines = { '┗', '┣', '┃', '━', '┏' }, kind = nil, + button = { '', '' }, imp_sign = '󰳛 ', }, hover = {