fix(diagnostic): add cursorlinehl in vim.diagnotic.opts.Signs

Problem: extmark also support cursorline_hl_group when cursorline is on.

Solution: support it on vim.diangostic.optss.Signs
This commit is contained in:
glepnir 2024-09-02 15:44:02 +08:00
parent 9762c5e340
commit 8a3bc607d6
3 changed files with 36 additions and 22 deletions

View File

@ -526,28 +526,32 @@ Lua module: vim.diagnostic *diagnostic-api*
*vim.diagnostic.Opts.Signs* *vim.diagnostic.Opts.Signs*
Fields: ~ Fields: ~
• {severity}? (`vim.diagnostic.SeverityFilter`) Only show virtual text • {severity}? (`vim.diagnostic.SeverityFilter`) Only show virtual
for diagnostics matching the given severity text for diagnostics matching the given severity
|diagnostic-severity| |diagnostic-severity|
• {priority}? (`integer`, default: `10`) Base priority to use for • {priority}? (`integer`, default: `10`) Base priority to use for
signs. When {severity_sort} is used, the priority of a signs. When {severity_sort} is used, the priority of a
sign is adjusted based on its severity. Otherwise, all sign is adjusted based on its severity. Otherwise, all
signs use the same priority. signs use the same priority.
• {text}? (`table<vim.diagnostic.Severity,string>`) A table mapping • {text}? (`table<vim.diagnostic.Severity,string>`) A table
|diagnostic-severity| to the sign text to display in the mapping |diagnostic-severity| to the sign text to
sign column. The default is to use `"E"`, `"W"`, `"I"`, display in the sign column. The default is to use
and `"H"` for errors, warnings, information, and hints, `"E"`, `"W"`, `"I"`, and `"H"` for errors, warnings,
respectively. Example: >lua information, and hints, respectively. Example: >lua
vim.diagnostic.config({ vim.diagnostic.config({
signs = { text = { [vim.diagnostic.severity.ERROR] = 'E', ... } } signs = { text = { [vim.diagnostic.severity.ERROR] = 'E', ... } }
}) })
< <
• {numhl}? (`table<vim.diagnostic.Severity,string>`) A table mapping • {numhl}? (`table<vim.diagnostic.Severity,string>`) A table
|diagnostic-severity| to the highlight group used for the mapping |diagnostic-severity| to the highlight group
line number where the sign is placed. used for the line number where the sign is placed.
• {linehl}? (`table<vim.diagnostic.Severity,string>`) A table mapping • {linehl}? (`table<vim.diagnostic.Severity,string>`) A table
|diagnostic-severity| to the highlight group used for the mapping |diagnostic-severity| to the highlight group
whole line the sign is placed in. used for the whole line the sign is placed in.
• {cursorlinehl} (`table<vim.diagnostic.Severity,string>`) A table
mapping |diagnostic-severity| to the highlight group
used for the highlight the sign column text when
cursor is on target line and 'cursorline' is enabled.
*vim.diagnostic.Opts.Underline* *vim.diagnostic.Opts.Underline*

View File

@ -243,6 +243,10 @@ local M = {}
--- A table mapping |diagnostic-severity| to the highlight group used for the --- A table mapping |diagnostic-severity| to the highlight group used for the
--- whole line the sign is placed in. --- whole line the sign is placed in.
--- @field linehl? table<vim.diagnostic.Severity,string> --- @field linehl? table<vim.diagnostic.Severity,string>
---
--- A table mapping |diagnostic-severity| to the highlight group used for the
--- highlight the sign column text when cursor is on target line and 'cursorline' is enabled.
--- @field cursorlinehl table<vim.diagnostic.Severity,string>
--- @class vim.diagnostic.Opts.Jump --- @class vim.diagnostic.Opts.Jump
--- ---
@ -1447,6 +1451,7 @@ M.handlers.signs = {
local numhl = opts.signs.numhl or {} local numhl = opts.signs.numhl or {}
local linehl = opts.signs.linehl or {} local linehl = opts.signs.linehl or {}
local curlinehl = opts.signs.cursorlinehl or {}
local line_count = api.nvim_buf_line_count(bufnr) local line_count = api.nvim_buf_line_count(bufnr)
@ -1457,6 +1462,7 @@ M.handlers.signs = {
sign_hl_group = sign_highlight_map[diagnostic.severity], sign_hl_group = sign_highlight_map[diagnostic.severity],
number_hl_group = numhl[diagnostic.severity], number_hl_group = numhl[diagnostic.severity],
line_hl_group = linehl[diagnostic.severity], line_hl_group = linehl[diagnostic.severity],
cursorline_hl_group = vim.wo.cursorline and curlinehl[diagnostic.severity] or nil,
priority = get_priority(diagnostic.severity), priority = get_priority(diagnostic.severity),
}) })
end end

View File

@ -2347,6 +2347,7 @@ describe('vim.diagnostic', function()
-- Legacy signs for diagnostics were deprecated in 0.10 and will be removed in 0.12 -- Legacy signs for diagnostics were deprecated in 0.10 and will be removed in 0.12
eq(0, n.fn.has('nvim-0.12')) eq(0, n.fn.has('nvim-0.12'))
n.command('set cursorline')
n.command('sign define DiagnosticSignError text= texthl= linehl=ErrorMsg numhl=ErrorMsg') n.command('sign define DiagnosticSignError text= texthl= linehl=ErrorMsg numhl=ErrorMsg')
n.command('sign define DiagnosticSignWarn text= texthl= linehl=WarningMsg numhl=WarningMsg') n.command('sign define DiagnosticSignWarn text= texthl= linehl=WarningMsg numhl=WarningMsg')
n.command('sign define DiagnosticSignInfo text= texthl= linehl=Underlined numhl=Underlined') n.command('sign define DiagnosticSignInfo text= texthl= linehl=Underlined numhl=Underlined')
@ -2354,7 +2355,7 @@ describe('vim.diagnostic', function()
local result = exec_lua(function() local result = exec_lua(function()
vim.diagnostic.config({ vim.diagnostic.config({
signs = true, signs = { cursorlinehl = { 'DiagnosticError', 'DiagnosticWarn' } },
}) })
local diagnostics = { local diagnostics = {
@ -2382,6 +2383,7 @@ describe('vim.diagnostic', function()
text = s[4].sign_text or '', text = s[4].sign_text or '',
numhl = s[4].number_hl_group, numhl = s[4].number_hl_group,
linehl = s[4].line_hl_group, linehl = s[4].line_hl_group,
culhl = s[4].cursorline_hl_group,
} }
end end
return result return result
@ -2393,6 +2395,7 @@ describe('vim.diagnostic', function()
text = '', text = '',
numhl = 'ErrorMsg', numhl = 'ErrorMsg',
linehl = 'ErrorMsg', linehl = 'ErrorMsg',
culhl = 'DiagnosticError',
}, result[1]) }, result[1])
eq({ eq({
@ -2401,6 +2404,7 @@ describe('vim.diagnostic', function()
text = '', text = '',
numhl = 'WarningMsg', numhl = 'WarningMsg',
linehl = 'WarningMsg', linehl = 'WarningMsg',
culhl = 'DiagnosticWarn',
}, result[2]) }, result[2])
end) end)
end) end)