From 26765e8461c1ba1e9a351632212cf89900221781 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 8 Apr 2024 00:41:41 +0200 Subject: feat(diagnostic): is_enabled, enable(…, enable:boolean) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: `vim.diagnostic.is_disabled` and `vim.diagnostic.disable` are unnecessary and inconsistent with the "toggle" pattern (established starting with `vim.lsp.inlay_hint`, see https://github.com/neovim/neovim/pull/25512#pullrequestreview-1676750276 As a reminder, the rationale is: - we always need `enable()` - we always end up needing `is_enabled()` - "toggle" can be achieved via `enable(not is_enabled())` - therefore, - `toggle()` and `disable()` are redundant - `is_disabled()` is a needless inconsistency Solution: - Introduce `vim.diagnostic.is_enabled`, and `vim.diagnostic.enable(…, enable:boolean)` - Note: Future improvement would be to add an `enable()` overload `enable(enable:boolean, opts: table)`. - Deprecate `vim.diagnostic.is_disabled`, `vim.diagnostic.disable` --- runtime/lua/vim/diagnostic.lua | 132 +++++++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 59 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 57491d155d..26374aa055 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -1492,7 +1492,7 @@ end --- diagnostics, use |vim.diagnostic.reset()|. --- --- To hide diagnostics and prevent them from re-displaying, use ---- |vim.diagnostic.disable()|. +--- |vim.diagnostic.enable()|. --- ---@param namespace integer? Diagnostic namespace. When omitted, hide --- diagnostics from all namespaces. @@ -1517,25 +1517,35 @@ function M.hide(namespace, bufnr) end end ---- Check whether diagnostics are disabled in a given buffer. +--- Check whether diagnostics are enabled. --- ----@param bufnr integer? Buffer number, or 0 for current buffer. ----@param namespace integer? Diagnostic namespace. When omitted, checks if ---- all diagnostics are disabled in {bufnr}. ---- Otherwise, only checks if diagnostics from ---- {namespace} are disabled. ----@return boolean -function M.is_disabled(bufnr, namespace) +--- @param bufnr integer? Buffer number, or 0 for current buffer. +--- @param namespace integer? Diagnostic namespace, or `nil` for all diagnostics in {bufnr}. +--- @return boolean +--- @since 12 +function M.is_enabled(bufnr, namespace) bufnr = get_bufnr(bufnr) if namespace and M.get_namespace(namespace).disabled then - return true + return false end if type(diagnostic_disabled[bufnr]) == 'table' then - return diagnostic_disabled[bufnr][namespace] + return not diagnostic_disabled[bufnr][namespace] end - return diagnostic_disabled[bufnr] ~= nil + return diagnostic_disabled[bufnr] == nil +end + +--- @deprecated use `vim.diagnostic.is_enabled()` +function M.is_disabled(bufnr, namespace) + vim.deprecate( + 'Defining diagnostic signs with :sign-define or sign_define()', + 'vim.diagnostic.is_enabled()', + '0.12', + nil, + false + ) + return not M.is_enabled(bufnr, namespace) end --- Display diagnostics for the given namespace and buffer. @@ -1923,71 +1933,75 @@ function M.setloclist(opts) set_list(true, opts) end ---- Disable diagnostics in the given buffer. ---- ----@param bufnr integer? Buffer number, or 0 for current buffer. When ---- omitted, disable diagnostics in all buffers. ----@param namespace integer? Only disable diagnostics for the given namespace. +--- @deprecated use `vim.diagnostic.enabled(…, false)` function M.disable(bufnr, namespace) - vim.validate({ bufnr = { bufnr, 'n', true }, namespace = { namespace, 'n', true } }) - if bufnr == nil then - if namespace == nil then - -- Disable everything (including as yet non-existing buffers and - -- namespaces) by setting diagnostic_disabled to an empty table and set - -- its metatable to always return true. This metatable is removed - -- in enable() - diagnostic_disabled = setmetatable({}, { - __index = function() - return true - end, - }) - else - local ns = M.get_namespace(namespace) - ns.disabled = true - end - else - bufnr = get_bufnr(bufnr) - if namespace == nil then - diagnostic_disabled[bufnr] = true - else - if type(diagnostic_disabled[bufnr]) ~= 'table' then - diagnostic_disabled[bufnr] = {} - end - diagnostic_disabled[bufnr][namespace] = true - end - end - - M.hide(namespace, bufnr) + vim.deprecate( + 'vim.diagnostic.disable()', + 'vim.diagnostic.enabled(…, false)', + '0.12', + nil, + false + ) + M.enable(bufnr, namespace, false) end ---- Enable diagnostics in the given buffer. +--- Enables or disables diagnostics. --- ----@param bufnr integer? Buffer number, or 0 for current buffer. When ---- omitted, enable diagnostics in all buffers. ----@param namespace integer? Only enable diagnostics for the given namespace. -function M.enable(bufnr, namespace) - vim.validate({ bufnr = { bufnr, 'n', true }, namespace = { namespace, 'n', true } }) +--- To "toggle", pass the inverse of `is_enabled()`: +--- +--- ```lua +--- vim.diagnostic.enable(0, not vim.diagnostic.is_enabled()) +--- ``` +--- +--- @param bufnr integer? Buffer number, or 0 for current buffer, or `nil` for all buffers. +--- @param namespace integer? Only for the given namespace, or `nil` for all. +--- @param enable (boolean|nil) true/nil to enable, false to disable +function M.enable(bufnr, namespace, enable) + vim.validate({ + bufnr = { bufnr, 'n', true }, + namespace = { namespace, 'n', true }, + enable = { enable, 'b', true }, + }) + enable = enable == nil and true or enable if bufnr == nil then if namespace == nil then - -- Enable everything by setting diagnostic_disabled to an empty table - diagnostic_disabled = {} + diagnostic_disabled = ( + enable + -- Enable everything by setting diagnostic_disabled to an empty table. + and {} + -- Disable everything (including as yet non-existing buffers and namespaces) by setting + -- diagnostic_disabled to an empty table and set its metatable to always return true. + or setmetatable({}, { + __index = function() + return true + end, + }) + ) else local ns = M.get_namespace(namespace) - ns.disabled = false + ns.disabled = not enable end else bufnr = get_bufnr(bufnr) if namespace == nil then - diagnostic_disabled[bufnr] = nil + diagnostic_disabled[bufnr] = (not enable) and true or nil else if type(diagnostic_disabled[bufnr]) ~= 'table' then - return + if enable then + return + else + diagnostic_disabled[bufnr] = {} + end end - diagnostic_disabled[bufnr][namespace] = nil + diagnostic_disabled[bufnr][namespace] = (not enable) and true or nil end end - M.show(namespace, bufnr) + if enable then + M.show(namespace, bufnr) + else + M.hide(namespace, bufnr) + end end --- Parse a diagnostic from a string. -- cgit From 5ed9916a28b9a09d2e5629f3e4e5b0e81403ded7 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 15 Apr 2024 18:35:59 +0200 Subject: feat(diagnostic): enable(…, opts) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: vim.diagnostic.enable() does not match the signature of vim.lsp.inlay_hint.enable() Solution: - Change the signature so that the first 2 args are (bufnr, enable). - Introduce a 3rd `opts` arg. - Currently it only supports `opts.ns_id`. --- runtime/lua/vim/diagnostic.lua | 67 ++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'runtime/lua/vim/diagnostic.lua') diff --git a/runtime/lua/vim/diagnostic.lua b/runtime/lua/vim/diagnostic.lua index 26374aa055..4fcbfa7507 100644 --- a/runtime/lua/vim/diagnostic.lua +++ b/runtime/lua/vim/diagnostic.lua @@ -239,6 +239,9 @@ local M = {} --- whole line the sign is placed in. --- @field linehl? table +--- @class vim.diagnostic.Filter : vim.diagnostic.Opts +--- @field ns_id? integer Namespace + --- @nodoc --- @enum vim.diagnostic.Severity M.severity = { @@ -1538,13 +1541,7 @@ end --- @deprecated use `vim.diagnostic.is_enabled()` function M.is_disabled(bufnr, namespace) - vim.deprecate( - 'Defining diagnostic signs with :sign-define or sign_define()', - 'vim.diagnostic.is_enabled()', - '0.12', - nil, - false - ) + vim.deprecate('vim.diagnostic.is_disabled()', 'vim.diagnostic.is_enabled()', '0.12', nil, false) return not M.is_enabled(bufnr, namespace) end @@ -1591,7 +1588,7 @@ function M.show(namespace, bufnr, diagnostics, opts) return end - if M.is_disabled(bufnr, namespace) then + if not M.is_enabled(bufnr, namespace) then return end @@ -1942,7 +1939,7 @@ function M.disable(bufnr, namespace) nil, false ) - M.enable(bufnr, namespace, false) + M.enable(bufnr, false, { ns_id = namespace }) end --- Enables or disables diagnostics. @@ -1954,17 +1951,49 @@ end --- ``` --- --- @param bufnr integer? Buffer number, or 0 for current buffer, or `nil` for all buffers. ---- @param namespace integer? Only for the given namespace, or `nil` for all. --- @param enable (boolean|nil) true/nil to enable, false to disable -function M.enable(bufnr, namespace, enable) +--- @param opts vim.diagnostic.Filter? Filter by these opts, or `nil` for all. Only `ns_id` is +--- supported, currently. +function M.enable(bufnr, enable, opts) + opts = opts or {} + if type(enable) == 'number' then + -- Legacy signature. + vim.deprecate( + 'vim.diagnostic.enable(buf:number, namespace)', + 'vim.diagnostic.enable(buf:number, enable:boolean, opts)', + '0.12', + nil, + false + ) + opts.ns_id = enable + enable = true + end vim.validate({ bufnr = { bufnr, 'n', true }, - namespace = { namespace, 'n', true }, - enable = { enable, 'b', true }, + enable = { + enable, + function(o) + return o == nil or type(o) == 'boolean' or type(o) == 'number' + end, + 'boolean or number (deprecated)', + }, + opts = { + opts, + function(o) + return o == nil + or ( + type(o) == 'table' + -- TODO(justinmk): support other `vim.diagnostic.Filter` fields. + and (vim.tbl_isempty(o) or vim.deep_equal(vim.tbl_keys(o), { 'ns_id' })) + ) + end, + 'vim.diagnostic.Filter table (only ns_id is supported currently)', + }, }) enable = enable == nil and true or enable + if bufnr == nil then - if namespace == nil then + if opts.ns_id == nil then diagnostic_disabled = ( enable -- Enable everything by setting diagnostic_disabled to an empty table. @@ -1978,12 +2007,12 @@ function M.enable(bufnr, namespace, enable) }) ) else - local ns = M.get_namespace(namespace) + local ns = M.get_namespace(opts.ns_id) ns.disabled = not enable end else bufnr = get_bufnr(bufnr) - if namespace == nil then + if opts.ns_id == nil then diagnostic_disabled[bufnr] = (not enable) and true or nil else if type(diagnostic_disabled[bufnr]) ~= 'table' then @@ -1993,14 +2022,14 @@ function M.enable(bufnr, namespace, enable) diagnostic_disabled[bufnr] = {} end end - diagnostic_disabled[bufnr][namespace] = (not enable) and true or nil + diagnostic_disabled[bufnr][opts.ns_id] = (not enable) and true or nil end end if enable then - M.show(namespace, bufnr) + M.show(opts.ns_id, bufnr) else - M.hide(namespace, bufnr) + M.hide(opts.ns_id, bufnr) end end -- cgit