Skip to content

Commit

Permalink
feat(ui): better ui on diangostic jump
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed May 19, 2024
1 parent ac27204 commit ae6f1e5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
6 changes: 0 additions & 6 deletions lua/lspsaga/codeaction/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 26 additions & 16 deletions lua/lspsaga/diagnostic/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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())
Expand All @@ -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) },
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion lua/lspsaga/diagnostic/show.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
8 changes: 8 additions & 0 deletions lua/lspsaga/highlight.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions lua/lspsaga/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local default_config = {
actionfix = '',
lines = { '', '', '', '', '' },
kind = nil,
button = { '', '' },
imp_sign = '󰳛 ',
},
hover = {
Expand Down

0 comments on commit ae6f1e5

Please sign in to comment.