refactor(lsp): deprecate vim.lsp.util.lookup_section

This function is used only in the `workspace/configuration` handler,
and does not warrant a public API because of its confusing return types.

The only caller `vim.lsp.handlers["workspace.configuration"]` is also
refactored to use `vim.tbl_get()` instead.
This commit is contained in:
Jongwook Choi 2024-01-14 23:12:54 -05:00 committed by Lewis Russell
parent 8f02ae82e2
commit 3973a5e405
4 changed files with 18 additions and 14 deletions

View File

@ -144,6 +144,8 @@ LSP FUNCTIONS
`progress` of |vim.lsp.client|
- *vim.lsp.get_active_clients()* Use |vim.lsp.get_clients()|
- *vim.lsp.for_each_buffer_client()* Use |vim.lsp.get_clients()|
- *vim.lsp.util.lookup_section()* Use |vim.tbl_get()| and
|vim.split()| with {plain=true} instead.
- *vim.lsp.util.trim_empty_lines()* Use |vim.split()| with `trimempty` instead.
- *vim.lsp.util.try_trim_markdown_code_blocks()*
- *vim.lsp.util.set_lines()*

View File

@ -1821,17 +1821,6 @@ locations_to_items({locations}, {offset_encoding})
Return: ~
(`vim.lsp.util.LocationItem[]`) list of items
lookup_section({settings}, {section}) *vim.lsp.util.lookup_section()*
Helper function to return nested values in language server settings
Parameters: ~
• {settings} (`table`) language server settings
• {section} (`string`) indicating the field of the settings table
Return: ~
(`table|string|vim.NIL`) The value of settings accessed via section.
`vim.NIL` if not found.
*vim.lsp.util.make_floating_popup_options()*
make_floating_popup_options({width}, {height}, {opts})
Creates a table with sensible default options for a floating window. The

View File

@ -170,6 +170,14 @@ M[ms.workspace_applyEdit] = function(_, workspace_edit, ctx)
}
end
---@param table table e.g., { foo = { bar = "z" } }
---@param section string indicating the field of the table, e.g., "foo.bar"
---@return any|nil setting value read from the table, or `nil` not found
local function lookup_section(table, section)
local keys = vim.split(section, '.', { plain = true }) --- @type string[]
return vim.tbl_get(table, unpack(keys))
end
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_configuration
M[ms.workspace_configuration] = function(_, result, ctx)
local client_id = ctx.client_id
@ -189,10 +197,13 @@ M[ms.workspace_configuration] = function(_, result, ctx)
local response = {}
for _, item in ipairs(result.items) do
if item.section then
local value = util.lookup_section(client.config.settings, item.section)
local value = lookup_section(client.config.settings, item.section)
-- For empty sections with no explicit '' key, return settings as is
if value == vim.NIL and item.section == '' then
value = client.config.settings or vim.NIL
if value == nil and item.section == '' then
value = client.config.settings
end
if value == nil then
value = vim.NIL
end
table.insert(response, value)
end

View File

@ -2140,7 +2140,9 @@ end
---@param settings table language server settings
---@param section string indicating the field of the settings table
---@return table|string|vim.NIL The value of settings accessed via section. `vim.NIL` if not found.
---@deprecated
function M.lookup_section(settings, section)
vim.deprecate('vim.lsp.util.lookup_section()', 'vim.tbl_get() with `vim.split`', '0.12')
for part in vim.gsplit(section, '.', { plain = true }) do
settings = settings[part]
if settings == nil then