From e52c25b7617ac6401b080f76b0e227161dfef230 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maria=20Jos=C3=A9=20Solano?= Date: Sat, 2 Mar 2024 13:11:23 -0800 Subject: [PATCH] feat(lua): deprecate vim.tbl_add_reverse_lookup --- runtime/doc/deprecated.txt | 1 + runtime/doc/lua.txt | 12 ------- runtime/doc/news.txt | 3 ++ runtime/lua/tohtml.lua | 5 ++- runtime/lua/vim/lsp.lua | 15 ++++---- runtime/lua/vim/lsp/log.lua | 26 ++++++++------ runtime/lua/vim/lsp/protocol.lua | 60 ++++++++++++++------------------ runtime/lua/vim/lsp/rpc.lua | 9 +++-- runtime/lua/vim/shared.lua | 3 ++ 9 files changed, 66 insertions(+), 68 deletions(-) diff --git a/runtime/doc/deprecated.txt b/runtime/doc/deprecated.txt index 9ca4e66c7b..5ac4ad4ce2 100644 --- a/runtime/doc/deprecated.txt +++ b/runtime/doc/deprecated.txt @@ -169,6 +169,7 @@ LUA - vim.register_keystroke_callback() Use |vim.on_key()| instead. - *vim.pretty_print()* Use |vim.print()| instead. - *vim.loop* Use |vim.uv| instead. +- *vim.tbl_add_reverse_lookup()* NORMAL COMMANDS - *]f* *[f* Same as "gf". diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index 4f8c685a41..d0ce737e49 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -2149,18 +2149,6 @@ vim.startswith({s}, {prefix}) *vim.startswith()* Return: ~ (`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()* Checks if a table contains a given value, specified either directly or via a predicate that is checked for each value. diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 516ff6f0fe..3029414500 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -526,4 +526,7 @@ release. populated. Background color detection is now performed in Lua by the Nvim core, not the TUI. +• vim.shared functions: + - |vim.tbl_add_reverse_lookup()| + vim:tw=78:ts=8:sw=2:et:ft=help:norl: diff --git a/runtime/lua/tohtml.lua b/runtime/lua/tohtml.lua index 68fef38c64..505de720ba 100644 --- a/runtime/lua/tohtml.lua +++ b/runtime/lua/tohtml.lua @@ -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 local bufnr = state.bufnr 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 + for ns, ns_id in pairs(vim.api.nvim_get_namespaces()) do + namespaces[ns_id] = ns + end for _, v in ipairs(extmarks) do _styletable_extmarks_highlight(state, v, namespaces) end diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua index ef5d9d7cff..d5c376ba44 100644 --- a/runtime/lua/vim/lsp.lua +++ b/runtime/lua/vim/lsp.lua @@ -146,9 +146,10 @@ end local client_errors_base = table.maxn(lsp.rpc.client_errors) local client_errors_offset = 0 -local function new_error_index() +local function client_error(name) 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 --- Error codes to be used with `on_error` from |vim.lsp.start_client|. @@ -158,12 +159,10 @@ end lsp.client_errors = tbl_extend( 'error', lsp.rpc.client_errors, - vim.tbl_add_reverse_lookup({ - BEFORE_INIT_CALLBACK_ERROR = new_error_index(), - ON_INIT_CALLBACK_ERROR = new_error_index(), - ON_ATTACH_ERROR = new_error_index(), - ON_EXIT_CALLBACK_ERROR = new_error_index(), - }) + client_error('BEFORE_INIT_CALLBACK_ERROR'), + client_error('ON_INIT_CALLBACK_ERROR'), + client_error('ON_ATTACH_ERROR'), + client_error('ON_EXIT_CALLBACK_ERROR') ) ---@private diff --git a/runtime/lua/vim/lsp/log.lua b/runtime/lua/vim/lsp/log.lua index 018003bb81..9f2bd71158 100644 --- a/runtime/lua/vim/lsp/log.lua +++ b/runtime/lua/vim/lsp/log.lua @@ -2,16 +2,19 @@ local log = {} +local log_levels = vim.log.levels + --- Log level dictionary with reverse lookup as well. --- --- 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" --- Level numbers begin with "TRACE" at 0 +--- @type table --- @nodoc -log.levels = vim.deepcopy(vim.log.levels) +log.levels = vim.deepcopy(log_levels) -- 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' @@ -58,7 +61,7 @@ local function open_logfile() logfile, openerr = io.open(logfilename, 'a+') if not logfile then 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 end @@ -77,12 +80,13 @@ local function open_logfile() return true 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. log[level] = levelnr -end -vim.tbl_add_reverse_lookup(log.levels) + -- Add a reverse lookup. + log.levels[levelnr] = level +end --- @param level string --- @param levelnr integer @@ -123,19 +127,19 @@ end -- log at that level (if applicable, it is checked either way). --- @nodoc -log.debug = create_logger('DEBUG', vim.log.levels.DEBUG) +log.debug = create_logger('DEBUG', log_levels.DEBUG) --- @nodoc -log.error = create_logger('ERROR', vim.log.levels.ERROR) +log.error = create_logger('ERROR', log_levels.ERROR) --- @nodoc -log.info = create_logger('INFO', vim.log.levels.INFO) +log.info = create_logger('INFO', log_levels.INFO) --- @nodoc -log.trace = create_logger('TRACE', vim.log.levels.TRACE) +log.trace = create_logger('TRACE', log_levels.TRACE) --- @nodoc -log.warn = create_logger('WARN', vim.log.levels.WARN) +log.warn = create_logger('WARN', log_levels.WARN) --- Sets the current log level. ---@param level (string|integer) One of `vim.lsp.log.levels` diff --git a/runtime/lua/vim/lsp/protocol.lua b/runtime/lua/vim/lsp/protocol.lua index 7016209372..599f02425e 100644 --- a/runtime/lua/vim/lsp/protocol.lua +++ b/runtime/lua/vim/lsp/protocol.lua @@ -1,22 +1,19 @@ --- @diagnostic disable: duplicate-doc-alias --- TODO(clason) can be simplified after reverse lookup is removed ----@param t table ----@return number[] -local function get_value_set(t) - local result = {} - for _, v in pairs(t) do - if type(v) == 'number' then - table.insert(result, v) - end +---@param tbl table +local function get_value_set(tbl) + local value_set = {} + for _, v in pairs(tbl) do + table.insert(value_set, v) end - table.sort(result) - return result + table.sort(value_set) + return value_set end -- Protocol for the Microsoft Language Server Protocol (mslsp) +local protocol = {} -local protocol = { +local constants = { --- @enum lsp.DiagnosticSeverity DiagnosticSeverity = { -- Reports an error. @@ -309,11 +306,13 @@ local protocol = { }, } --- TODO(mariasolos): Remove this reverse lookup. -for k, v in pairs(protocol) do - local tbl = vim.deepcopy(v, true) - vim.tbl_add_reverse_lookup(tbl) - protocol[k] = tbl +for k1, v1 in pairs(constants) do + local tbl = vim.deepcopy(v1, true) + for _, k2 in ipairs(vim.tbl_keys(tbl)) do + local v2 = tbl[k2] + tbl[v2] = k2 + end + protocol[k1] = tbl end --[=[ @@ -719,14 +718,7 @@ function protocol.make_client_capabilities() codeActionLiteralSupport = { codeActionKind = { - valueSet = (function() - 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)(), + valueSet = get_value_set(constants.CodeActionKind), }, }, isPreferredSupport = true, @@ -751,10 +743,10 @@ function protocol.make_client_capabilities() commitCharactersSupport = false, preselectSupport = false, deprecatedSupport = false, - documentationFormat = { protocol.MarkupKind.Markdown, protocol.MarkupKind.PlainText }, + documentationFormat = { constants.MarkupKind.Markdown, constants.MarkupKind.PlainText }, }, completionItemKind = { - valueSet = get_value_set(protocol.CompletionItemKind), + valueSet = get_value_set(constants.CompletionItemKind), }, completionList = { itemDefaults = { @@ -783,13 +775,13 @@ function protocol.make_client_capabilities() }, hover = { dynamicRegistration = true, - contentFormat = { protocol.MarkupKind.Markdown, protocol.MarkupKind.PlainText }, + contentFormat = { constants.MarkupKind.Markdown, constants.MarkupKind.PlainText }, }, signatureHelp = { dynamicRegistration = false, signatureInformation = { activeParameterSupport = true, - documentationFormat = { protocol.MarkupKind.Markdown, protocol.MarkupKind.PlainText }, + documentationFormat = { constants.MarkupKind.Markdown, constants.MarkupKind.PlainText }, parameterInformation = { labelOffsetSupport = true, }, @@ -804,7 +796,7 @@ function protocol.make_client_capabilities() documentSymbol = { dynamicRegistration = false, symbolKind = { - valueSet = get_value_set(protocol.SymbolKind), + valueSet = get_value_set(constants.SymbolKind), }, hierarchicalDocumentSymbolSupport = true, }, @@ -815,7 +807,7 @@ function protocol.make_client_capabilities() publishDiagnostics = { relatedInformation = true, tagSupport = { - valueSet = get_value_set(protocol.DiagnosticTag), + valueSet = get_value_set(constants.DiagnosticTag), }, dataSupport = true, }, @@ -827,7 +819,7 @@ function protocol.make_client_capabilities() symbol = { dynamicRegistration = false, symbolKind = { - valueSet = get_value_set(protocol.SymbolKind), + valueSet = get_value_set(constants.SymbolKind), }, }, configuration = true, @@ -867,9 +859,9 @@ end --- Creates a normalized object describing LSP server capabilities. ---@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) - local TextDocumentSyncKind = protocol.TextDocumentSyncKind + local TextDocumentSyncKind = protocol.TextDocumentSyncKind ---@type table local textDocumentSync = server_capabilities.textDocumentSync if textDocumentSync == nil then -- Defaults if omitted. diff --git a/runtime/lua/vim/lsp/rpc.lua b/runtime/lua/vim/lsp/rpc.lua index 8e014b1063..984e4f040a 100644 --- a/runtime/lua/vim/lsp/rpc.lua +++ b/runtime/lua/vim/lsp/rpc.lua @@ -130,7 +130,7 @@ local M = {} --- Mapping of error codes used by the client --- @nodoc -M.client_errors = { +local client_errors = { INVALID_SERVER_MESSAGE = 1, INVALID_SERVER_JSON = 2, NO_RESULT_CALLBACK_FOUND = 3, @@ -140,7 +140,12 @@ M.client_errors = { SERVER_RESULT_CALLBACK_ERROR = 7, } -M.client_errors = vim.tbl_add_reverse_lookup(M.client_errors) +--- @type table +--- @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. --- diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index 561d9e3f1b..bd553598c7 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -465,9 +465,12 @@ end --- `tbl_add_reverse_lookup { A = 1 } == { [1] = 'A', A = 1 }` --- --- Note that this *modifies* the input. +---@deprecated ---@param o table Table to add the reverse to ---@return table o function vim.tbl_add_reverse_lookup(o) + vim.deprecate('vim.tbl_add_reverse_lookup', nil, '0.12') + --- @cast o table --- @type any[] local keys = vim.tbl_keys(o)