Skip to content

Commit ee1de03

Browse files
authored
fix(diagnostics): add fallback to signs on nvim v0.10+ (#1741)
1 parent dda4c9e commit ee1de03

File tree

3 files changed

+91
-32
lines changed

3 files changed

+91
-32
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,21 @@ return {
158158
-----@type neotree.Config
159159
--opts = {},
160160
config = function()
161-
-- If you want icons for diagnostic errors, you'll need to define them somewhere:
161+
-- If you want icons for diagnostic errors, you'll need to define them somewhere.
162+
-- In Neovim v0.10+, you can configure them in vim.diagnostic.config(), like:
163+
--
164+
-- vim.diagnostic.config({
165+
-- signs = {
166+
-- text = {
167+
-- [vim.diagnostic.severity.ERROR] = '',
168+
-- [vim.diagnostic.severity.WARN] = '',
169+
-- [vim.diagnostic.severity.INFO] = '',
170+
-- [vim.diagnostic.severity.HINT] = '󰌵',
171+
-- },
172+
-- }
173+
-- })
174+
--
175+
-- In older versions, you can define the signs manually:
162176
-- vim.fn.sign_define("DiagnosticSignError", { text = " ", texthl = "DiagnosticSignError" })
163177
-- vim.fn.sign_define("DiagnosticSignWarn", { text = " ", texthl = "DiagnosticSignWarn" })
164178
-- vim.fn.sign_define("DiagnosticSignInfo", { text = " ", texthl = "DiagnosticSignInfo" })

doc/neo-tree.txt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,29 @@ display the highest severity level for files, and errors only for directories.
11831183
If you want to use symbols instead of "E", "W", "I", and H", you'll need to
11841184
define those somewhere in your nvim configuration. Here is an example:
11851185

1186+
>lua
1187+
vim.diagnostic.config({
1188+
signs = {
1189+
text = {
1190+
[vim.diagnostic.severity.ERROR] = '',
1191+
[vim.diagnostic.severity.WARN] = '',
1192+
[vim.diagnostic.severity.INFO] = '',
1193+
[vim.diagnostic.severity.HINT] = '󰌵',
1194+
},
1195+
}
1196+
})
1197+
<
1198+
1199+
If you don't change the `signs` option from its default, e.g.:
1200+
1201+
>lua
1202+
vim.diagnostic.config({
1203+
signs = true
1204+
})
1205+
<
1206+
1207+
or are on Neovim v0.9 and below, then this will work instead:
1208+
11861209
>lua
11871210
vim.fn.sign_define("DiagnosticSignError",
11881211
{text = " ", texthl = "DiagnosticSignError"})
@@ -1216,7 +1239,8 @@ neo-tree setup call like this:
12161239
},
12171240
}
12181241
})
1219-
>
1242+
<
1243+
12201244
Anything not specified in the `default_component_configs` will fallback to the
12211245
`sign_define` method.
12221246

lua/neo-tree/sources/common/components.lua

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,40 +120,61 @@ end
120120
---`sign_getdefined` based wrapper with compatibility
121121
---@param severity string
122122
---@return vim.fn.sign_getdefined.ret.item
123-
local function get_defined_sign(severity)
124-
local defined
123+
local get_legacy_sign = function(severity)
124+
local sign = vim.fn.sign_getdefined("DiagnosticSign" .. severity)
125+
if vim.tbl_isempty(sign) then
126+
-- backwards compatibility...
127+
local old_severity = severity
128+
if severity == "Warning" then
129+
old_severity = "Warn"
130+
elseif severity == "Information" then
131+
old_severity = "Info"
132+
end
133+
sign = vim.fn.sign_getdefined("LspDiagnosticsSign" .. old_severity)
134+
end
135+
return sign and sign[1]
136+
end
137+
138+
local nvim_0_10 = vim.fn.has("nvim-0.10") > 0
139+
---Returns the sign corresponding to the given severity
140+
---@param severity string
141+
---@return vim.fn.sign_getdefined.ret.item
142+
local function get_diagnostic_sign(severity)
143+
local sign
144+
145+
if nvim_0_10 then
146+
local signs = vim.diagnostic.config().signs
147+
148+
if type(signs) == "function" then
149+
--TODO: Find a better way to get a namespace
150+
local namespaces = vim.diagnostic.get_namespaces()
151+
if not vim.tbl_isempty(namespaces) then
152+
local ns_id = next(namespaces)
153+
---@cast ns_id -nil
154+
signs = signs(ns_id, 0)
155+
end
156+
end
125157

126-
if vim.fn.has("nvim-0.10") > 0 then
127-
local signs_config = vim.diagnostic.config().signs
128-
if type(signs_config) == "table" then
158+
if type(signs) == "table" then
129159
local identifier = severity:sub(1, 1)
130160
if identifier == "H" then
131161
identifier = "N"
132162
end
133-
defined = {
134-
text = (signs_config.text or {})[vim.diagnostic.severity[identifier]],
163+
sign = {
164+
text = (signs.text or {})[vim.diagnostic.severity[identifier]],
135165
texthl = "DiagnosticSign" .. severity,
136166
}
167+
elseif signs == true then
168+
sign = get_legacy_sign(severity)
137169
end
138170
else -- before 0.10
139-
defined = vim.fn.sign_getdefined("DiagnosticSign" .. severity)
140-
if vim.tbl_isempty(defined) then
141-
-- backwards compatibility...
142-
local old_severity = severity
143-
if severity == "Warning" then
144-
old_severity = "Warn"
145-
elseif severity == "Information" then
146-
old_severity = "Info"
147-
end
148-
defined = vim.fn.sign_getdefined("LspDiagnosticsSign" .. old_severity)
149-
end
150-
defined = defined and defined[1]
171+
sign = get_legacy_sign(severity)
151172
end
152173

153-
if type(defined) ~= "table" then
154-
defined = {}
174+
if type(sign) ~= "table" then
175+
sign = {}
155176
end
156-
return defined
177+
return sign
157178
end
158179

159180
---@class (exact) neotree.Component.Common.Diagnostics : neotree.Component
@@ -178,23 +199,23 @@ M.diagnostics = function(config, node, state)
178199
end
179200
---@type string
180201
local severity = diag_state.severity_string
181-
local defined = get_defined_sign(severity)
202+
local sign = get_diagnostic_sign(severity)
182203

183204
-- check for overrides in the component config
184205
local severity_lower = severity:lower()
185206
if config.symbols and config.symbols[severity_lower] then
186-
defined.texthl = defined.texthl or ("Diagnostic" .. severity)
187-
defined.text = config.symbols[severity_lower]
207+
sign.texthl = sign.texthl or ("Diagnostic" .. severity)
208+
sign.text = config.symbols[severity_lower]
188209
end
189210
if config.highlights and config.highlights[severity_lower] then
190-
defined.text = defined.text or severity:sub(1, 1)
191-
defined.texthl = config.highlights[severity_lower]
211+
sign.text = sign.text or severity:sub(1, 1)
212+
sign.texthl = config.highlights[severity_lower]
192213
end
193214

194-
if defined.text and defined.texthl then
215+
if sign.text and sign.texthl then
195216
return {
196-
text = make_two_char(defined.text),
197-
highlight = defined.texthl,
217+
text = make_two_char(sign.text),
218+
highlight = sign.texthl,
198219
}
199220
else
200221
return {

0 commit comments

Comments
 (0)