From 3d7e49c201537ee0293a1a3abe67b67f8e7648a5 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 19 Jun 2024 14:30:05 +0100 Subject: [PATCH] feat(config)!: deprecate highlight groups in config.signs These fields haven't been documented for some time, so now hard deprecate them. --- gen_help.lua | 4 +-- lua/gitsigns/config.lua | 63 ++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/gen_help.lua b/gen_help.lua index 2a3be100..1631a91c 100755 --- a/gen_help.lua +++ b/gen_help.lua @@ -101,10 +101,10 @@ local function gen_config_doc_field(field, out) end local vtype = (function() - if v.type == 'table' and v.deep_extend then + local ty = v.type_help or v.type + if ty == 'table' and v.deep_extend then return 'table[extended]' end - local ty = v.type if type(ty) == 'table' then v.type = table.concat(ty, '|') end diff --git a/lua/gitsigns/config.lua b/lua/gitsigns/config.lua index 860b4841..b107892c 100644 --- a/lua/gitsigns/config.lua +++ b/lua/gitsigns/config.lua @@ -11,7 +11,8 @@ --- @field hard? boolean --- @class (exact) Gitsigns.SchemaElem ---- @field type string|string[] +--- @field type string|string[]|fun(x:any): boolean +--- @field type_help? string --- @field refresh? fun(cb: fun()) Function to refresh the config value --- @field deep_extend? boolean --- @field default any @@ -182,10 +183,50 @@ M.config = setmetatable({}, { end, }) +local function warn(s, ...) + vim.notify_once(s:format(...), vim.log.levels.WARN, { title = 'gitsigns' }) +end + +--- @param x Gitsigns.SignConfig +--- @return boolean +local function validate_signs(x) + if type(x) ~= 'table' then + return false + end + + local warnings --- @type table? + + --- @diagnostic disable-next-line:no-unknown + for kind, s in pairs(M.schema.signs.default) do + --- @diagnostic disable-next-line:no-unknown + for ty, v in pairs(s) do + if x[kind] and x[kind][ty] and vim.endswith(ty, 'hl') then + warnings = warnings or {} + local w = string.format( + "'signs.%s.%s' is now deprecated, please define highlight '%s'", + kind, + ty, + v + ) + warnings[w] = true + end + end + end + + if warnings then + for w in vim.spairs(warnings) do + warn(w) + end + end + + return true +end + --- @type table M.schema = { signs = { - type = 'table', + type_help = 'table', + type = validate_signs, deep_extend = true, default = { add = { hl = 'GitSignsAdd', text = '┃', numhl = 'GitSignsAddNr', linehl = 'GitSignsAddLn' }, @@ -861,22 +902,18 @@ M.schema = { }, } -local function warn(s, ...) - vim.notify(s:format(...), vim.log.levels.WARN, { title = 'gitsigns' }) -end - --- @param config Gitsigns.Config local function validate_config(config) - --- @diagnostic disable-next-line:no-unknown - for k, v in pairs(config) do + for k, v in + pairs(config --[[@as table]]) + do local kschema = M.schema[k] if kschema == nil then warn("gitsigns: Ignoring invalid configuration field '%s'", k) - elseif kschema.type then - if type(kschema.type) == 'string' then - vim.validate({ - [k] = { v, kschema.type }, - }) + else + local ty = kschema.type + if type(ty) == 'string' or type(ty) == 'function' then + vim.validate({ [k] = { v, ty } }) end end end