Skip to content

Commit

Permalink
feat(lua): deprecate vim.tbl_add_reverse_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaSolOs authored and clason committed Mar 7, 2024
1 parent 6525832 commit e52c25b
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 68 deletions.
1 change: 1 addition & 0 deletions runtime/doc/deprecated.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
12 changes: 0 additions & 12 deletions runtime/doc/lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions runtime/doc/news.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
5 changes: 4 additions & 1 deletion runtime/lua/tohtml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
_styletable_extmarks_highlight(state, v, namespaces)
end
Expand Down
15 changes: 7 additions & 8 deletions runtime/lua/vim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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|.
Expand All @@ -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
Expand Down
26 changes: 15 additions & 11 deletions runtime/lua/vim/lsp/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|integer, string|integer>
--- @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'

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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`
Expand Down
60 changes: 26 additions & 34 deletions runtime/lua/vim/lsp/protocol.lua
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
--- @diagnostic disable: duplicate-doc-alias

-- TODO(clason) can be simplified after reverse lookup is removed
---@param t table<any, any>
---@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<string, string|number>
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.
Expand Down Expand Up @@ -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

--[=[
Expand Down Expand Up @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
Expand Down Expand Up @@ -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<string|number, string|number>
local textDocumentSync = server_capabilities.textDocumentSync
if textDocumentSync == nil then
-- Defaults if omitted.
Expand Down
9 changes: 7 additions & 2 deletions runtime/lua/vim/lsp/rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<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.
---
Expand Down
3 changes: 3 additions & 0 deletions runtime/lua/vim/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<any,any>
--- @type any[]
local keys = vim.tbl_keys(o)
Expand Down

0 comments on commit e52c25b

Please sign in to comment.