feat(lua): deprecate vim.tbl_add_reverse_lookup

This commit is contained in:
Maria José Solano 2024-03-02 13:11:23 -08:00 committed by Christian Clason
parent 6525832a8c
commit e52c25b761
9 changed files with 66 additions and 68 deletions

View File

@ -169,6 +169,7 @@ LUA
- vim.register_keystroke_callback() Use |vim.on_key()| instead. - vim.register_keystroke_callback() Use |vim.on_key()| instead.
- *vim.pretty_print()* Use |vim.print()| instead. - *vim.pretty_print()* Use |vim.print()| instead.
- *vim.loop* Use |vim.uv| instead. - *vim.loop* Use |vim.uv| instead.
- *vim.tbl_add_reverse_lookup()*
NORMAL COMMANDS NORMAL COMMANDS
- *]f* *[f* Same as "gf". - *]f* *[f* Same as "gf".

View File

@ -2149,18 +2149,6 @@ vim.startswith({s}, {prefix}) *vim.startswith()*
Return: ~ Return: ~
(`boolean`) `true` if `prefix` is a prefix of `s` (`boolean`) `true` if `prefix` is a prefix of `s`
vim.tbl_add_reverse_lookup({o}) *vim.tbl_add_reverse_lookup()*
Add the reverse lookup values to an existing table. For example:
`tbl_add_reverse_lookup { A = 1 } == { [1] = 'A', A = 1 }`
Note that this modifies the input.
Parameters: ~
• {o} (`table`) Table to add the reverse to
Return: ~
(`table`) o
vim.tbl_contains({t}, {value}, {opts}) *vim.tbl_contains()* vim.tbl_contains({t}, {value}, {opts}) *vim.tbl_contains()*
Checks if a table contains a given value, specified either directly or via Checks if a table contains a given value, specified either directly or via
a predicate that is checked for each value. a predicate that is checked for each value.

View File

@ -526,4 +526,7 @@ release.
populated. Background color detection is now performed in Lua by the Nvim populated. Background color detection is now performed in Lua by the Nvim
core, not the TUI. core, not the TUI.
• vim.shared functions:
- |vim.tbl_add_reverse_lookup()|
vim:tw=78:ts=8:sw=2:et:ft=help:norl: vim:tw=78:ts=8:sw=2:et:ft=help:norl:

View File

@ -575,7 +575,10 @@ local function styletable_extmarks(state)
--TODO(altermo) extmarks may have col/row which is outside of the buffer, which could cause an error --TODO(altermo) extmarks may have col/row which is outside of the buffer, which could cause an error
local bufnr = state.bufnr local bufnr = state.bufnr
local extmarks = vim.api.nvim_buf_get_extmarks(bufnr, -1, 0, -1, { details = true }) local extmarks = vim.api.nvim_buf_get_extmarks(bufnr, -1, 0, -1, { details = true })
local namespaces = vim.tbl_add_reverse_lookup(vim.api.nvim_get_namespaces()) local namespaces = {} --- @type table<integer, string>
for ns, ns_id in pairs(vim.api.nvim_get_namespaces()) do
namespaces[ns_id] = ns
end
for _, v in ipairs(extmarks) do for _, v in ipairs(extmarks) do
_styletable_extmarks_highlight(state, v, namespaces) _styletable_extmarks_highlight(state, v, namespaces)
end end

View File

@ -146,9 +146,10 @@ end
local client_errors_base = table.maxn(lsp.rpc.client_errors) local client_errors_base = table.maxn(lsp.rpc.client_errors)
local client_errors_offset = 0 local client_errors_offset = 0
local function new_error_index() local function client_error(name)
client_errors_offset = client_errors_offset + 1 client_errors_offset = client_errors_offset + 1
return client_errors_base + client_errors_offset local index = client_errors_base + client_errors_offset
return { [name] = index, [index] = name }
end end
--- Error codes to be used with `on_error` from |vim.lsp.start_client|. --- Error codes to be used with `on_error` from |vim.lsp.start_client|.
@ -158,12 +159,10 @@ end
lsp.client_errors = tbl_extend( lsp.client_errors = tbl_extend(
'error', 'error',
lsp.rpc.client_errors, lsp.rpc.client_errors,
vim.tbl_add_reverse_lookup({ client_error('BEFORE_INIT_CALLBACK_ERROR'),
BEFORE_INIT_CALLBACK_ERROR = new_error_index(), client_error('ON_INIT_CALLBACK_ERROR'),
ON_INIT_CALLBACK_ERROR = new_error_index(), client_error('ON_ATTACH_ERROR'),
ON_ATTACH_ERROR = new_error_index(), client_error('ON_EXIT_CALLBACK_ERROR')
ON_EXIT_CALLBACK_ERROR = new_error_index(),
})
) )
---@private ---@private

View File

@ -2,16 +2,19 @@
local log = {} local log = {}
local log_levels = vim.log.levels
--- Log level dictionary with reverse lookup as well. --- Log level dictionary with reverse lookup as well.
--- ---
--- Can be used to lookup the number from the name or the name from the number. --- Can be used to lookup the number from the name or the name from the number.
--- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF" --- Levels by name: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "OFF"
--- Level numbers begin with "TRACE" at 0 --- Level numbers begin with "TRACE" at 0
--- @type table<string|integer, string|integer>
--- @nodoc --- @nodoc
log.levels = vim.deepcopy(vim.log.levels) log.levels = vim.deepcopy(log_levels)
-- Default log level is warn. -- Default log level is warn.
local current_log_level = log.levels.WARN local current_log_level = log_levels.WARN
local log_date_format = '%F %H:%M:%S' local log_date_format = '%F %H:%M:%S'
@ -58,7 +61,7 @@ local function open_logfile()
logfile, openerr = io.open(logfilename, 'a+') logfile, openerr = io.open(logfilename, 'a+')
if not logfile then if not logfile then
local err_msg = string.format('Failed to open LSP client log file: %s', openerr) local err_msg = string.format('Failed to open LSP client log file: %s', openerr)
notify(err_msg, vim.log.levels.ERROR) notify(err_msg, log_levels.ERROR)
return false return false
end end
@ -77,12 +80,13 @@ local function open_logfile()
return true return true
end end
for level, levelnr in pairs(log.levels) do for level, levelnr in pairs(log_levels) do
-- Also export the log level on the root object. -- Also export the log level on the root object.
log[level] = levelnr log[level] = levelnr
end
vim.tbl_add_reverse_lookup(log.levels) -- Add a reverse lookup.
log.levels[levelnr] = level
end
--- @param level string --- @param level string
--- @param levelnr integer --- @param levelnr integer
@ -123,19 +127,19 @@ end
-- log at that level (if applicable, it is checked either way). -- log at that level (if applicable, it is checked either way).
--- @nodoc --- @nodoc
log.debug = create_logger('DEBUG', vim.log.levels.DEBUG) log.debug = create_logger('DEBUG', log_levels.DEBUG)
--- @nodoc --- @nodoc
log.error = create_logger('ERROR', vim.log.levels.ERROR) log.error = create_logger('ERROR', log_levels.ERROR)
--- @nodoc --- @nodoc
log.info = create_logger('INFO', vim.log.levels.INFO) log.info = create_logger('INFO', log_levels.INFO)
--- @nodoc --- @nodoc
log.trace = create_logger('TRACE', vim.log.levels.TRACE) log.trace = create_logger('TRACE', log_levels.TRACE)
--- @nodoc --- @nodoc
log.warn = create_logger('WARN', vim.log.levels.WARN) log.warn = create_logger('WARN', log_levels.WARN)
--- Sets the current log level. --- Sets the current log level.
---@param level (string|integer) One of `vim.lsp.log.levels` ---@param level (string|integer) One of `vim.lsp.log.levels`

View File

@ -1,22 +1,19 @@
--- @diagnostic disable: duplicate-doc-alias --- @diagnostic disable: duplicate-doc-alias
-- TODO(clason) can be simplified after reverse lookup is removed ---@param tbl table<string, string|number>
---@param t table<any, any> local function get_value_set(tbl)
---@return number[] local value_set = {}
local function get_value_set(t) for _, v in pairs(tbl) do
local result = {} table.insert(value_set, v)
for _, v in pairs(t) do
if type(v) == 'number' then
table.insert(result, v)
end end
end table.sort(value_set)
table.sort(result) return value_set
return result
end end
-- Protocol for the Microsoft Language Server Protocol (mslsp) -- Protocol for the Microsoft Language Server Protocol (mslsp)
local protocol = {}
local protocol = { local constants = {
--- @enum lsp.DiagnosticSeverity --- @enum lsp.DiagnosticSeverity
DiagnosticSeverity = { DiagnosticSeverity = {
-- Reports an error. -- Reports an error.
@ -309,11 +306,13 @@ local protocol = {
}, },
} }
-- TODO(mariasolos): Remove this reverse lookup. for k1, v1 in pairs(constants) do
for k, v in pairs(protocol) do local tbl = vim.deepcopy(v1, true)
local tbl = vim.deepcopy(v, true) for _, k2 in ipairs(vim.tbl_keys(tbl)) do
vim.tbl_add_reverse_lookup(tbl) local v2 = tbl[k2]
protocol[k] = tbl tbl[v2] = k2
end
protocol[k1] = tbl
end end
--[=[ --[=[
@ -719,14 +718,7 @@ function protocol.make_client_capabilities()
codeActionLiteralSupport = { codeActionLiteralSupport = {
codeActionKind = { codeActionKind = {
valueSet = (function() valueSet = get_value_set(constants.CodeActionKind),
local res = vim.iter.filter(function(value)
-- Filter out the keys that were added by the reverse lookup.
return value:match('^%l')
end, vim.tbl_values(protocol.CodeActionKind))
table.sort(res)
return res
end)(),
}, },
}, },
isPreferredSupport = true, isPreferredSupport = true,
@ -751,10 +743,10 @@ function protocol.make_client_capabilities()
commitCharactersSupport = false, commitCharactersSupport = false,
preselectSupport = false, preselectSupport = false,
deprecatedSupport = false, deprecatedSupport = false,
documentationFormat = { protocol.MarkupKind.Markdown, protocol.MarkupKind.PlainText }, documentationFormat = { constants.MarkupKind.Markdown, constants.MarkupKind.PlainText },
}, },
completionItemKind = { completionItemKind = {
valueSet = get_value_set(protocol.CompletionItemKind), valueSet = get_value_set(constants.CompletionItemKind),
}, },
completionList = { completionList = {
itemDefaults = { itemDefaults = {
@ -783,13 +775,13 @@ function protocol.make_client_capabilities()
}, },
hover = { hover = {
dynamicRegistration = true, dynamicRegistration = true,
contentFormat = { protocol.MarkupKind.Markdown, protocol.MarkupKind.PlainText }, contentFormat = { constants.MarkupKind.Markdown, constants.MarkupKind.PlainText },
}, },
signatureHelp = { signatureHelp = {
dynamicRegistration = false, dynamicRegistration = false,
signatureInformation = { signatureInformation = {
activeParameterSupport = true, activeParameterSupport = true,
documentationFormat = { protocol.MarkupKind.Markdown, protocol.MarkupKind.PlainText }, documentationFormat = { constants.MarkupKind.Markdown, constants.MarkupKind.PlainText },
parameterInformation = { parameterInformation = {
labelOffsetSupport = true, labelOffsetSupport = true,
}, },
@ -804,7 +796,7 @@ function protocol.make_client_capabilities()
documentSymbol = { documentSymbol = {
dynamicRegistration = false, dynamicRegistration = false,
symbolKind = { symbolKind = {
valueSet = get_value_set(protocol.SymbolKind), valueSet = get_value_set(constants.SymbolKind),
}, },
hierarchicalDocumentSymbolSupport = true, hierarchicalDocumentSymbolSupport = true,
}, },
@ -815,7 +807,7 @@ function protocol.make_client_capabilities()
publishDiagnostics = { publishDiagnostics = {
relatedInformation = true, relatedInformation = true,
tagSupport = { tagSupport = {
valueSet = get_value_set(protocol.DiagnosticTag), valueSet = get_value_set(constants.DiagnosticTag),
}, },
dataSupport = true, dataSupport = true,
}, },
@ -827,7 +819,7 @@ function protocol.make_client_capabilities()
symbol = { symbol = {
dynamicRegistration = false, dynamicRegistration = false,
symbolKind = { symbolKind = {
valueSet = get_value_set(protocol.SymbolKind), valueSet = get_value_set(constants.SymbolKind),
}, },
}, },
configuration = true, configuration = true,
@ -867,9 +859,9 @@ end
--- Creates a normalized object describing LSP server capabilities. --- Creates a normalized object describing LSP server capabilities.
---@param server_capabilities table Table of capabilities supported by the server ---@param server_capabilities table Table of capabilities supported by the server
---@return lsp.ServerCapabilities|nil Normalized table of capabilities ---@return lsp.ServerCapabilities|nil : Normalized table of capabilities
function protocol.resolve_capabilities(server_capabilities) function protocol.resolve_capabilities(server_capabilities)
local TextDocumentSyncKind = protocol.TextDocumentSyncKind local TextDocumentSyncKind = protocol.TextDocumentSyncKind ---@type table<string|number, string|number>
local textDocumentSync = server_capabilities.textDocumentSync local textDocumentSync = server_capabilities.textDocumentSync
if textDocumentSync == nil then if textDocumentSync == nil then
-- Defaults if omitted. -- Defaults if omitted.

View File

@ -130,7 +130,7 @@ local M = {}
--- Mapping of error codes used by the client --- Mapping of error codes used by the client
--- @nodoc --- @nodoc
M.client_errors = { local client_errors = {
INVALID_SERVER_MESSAGE = 1, INVALID_SERVER_MESSAGE = 1,
INVALID_SERVER_JSON = 2, INVALID_SERVER_JSON = 2,
NO_RESULT_CALLBACK_FOUND = 3, NO_RESULT_CALLBACK_FOUND = 3,
@ -140,7 +140,12 @@ M.client_errors = {
SERVER_RESULT_CALLBACK_ERROR = 7, SERVER_RESULT_CALLBACK_ERROR = 7,
} }
M.client_errors = vim.tbl_add_reverse_lookup(M.client_errors) --- @type table<string|integer, string|integer>
--- @nodoc
M.client_errors = vim.deepcopy(client_errors)
for k, v in pairs(client_errors) do
M.client_errors[v] = k
end
--- Constructs an error message from an LSP error object. --- Constructs an error message from an LSP error object.
--- ---

View File

@ -465,9 +465,12 @@ end
--- `tbl_add_reverse_lookup { A = 1 } == { [1] = 'A', A = 1 }` --- `tbl_add_reverse_lookup { A = 1 } == { [1] = 'A', A = 1 }`
--- ---
--- Note that this *modifies* the input. --- Note that this *modifies* the input.
---@deprecated
---@param o table Table to add the reverse to ---@param o table Table to add the reverse to
---@return table o ---@return table o
function vim.tbl_add_reverse_lookup(o) function vim.tbl_add_reverse_lookup(o)
vim.deprecate('vim.tbl_add_reverse_lookup', nil, '0.12')
--- @cast o table<any,any> --- @cast o table<any,any>
--- @type any[] --- @type any[]
local keys = vim.tbl_keys(o) local keys = vim.tbl_keys(o)