Skip to content

Commit

Permalink
Merge pull request neovim#30929 from lewis6991/fix/lsp_param_encodings
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 authored Oct 24, 2024
2 parents 0087822 + 2dcbfe7 commit 5c44c02
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 72 deletions.
5 changes: 3 additions & 2 deletions runtime/lua/vim/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ api.nvim_create_autocmd('VimLeavePre', {
---
---@param bufnr (integer) Buffer handle, or 0 for current.
---@param method (string) LSP method name
---@param params table|nil Parameters to send to the server
---@param params? table|(fun(client: vim.lsp.Client, bufnr: integer): table?) Parameters to send to the server
---@param handler? lsp.Handler See |lsp-handler|
--- If nil, follows resolution strategy defined in |lsp-handler-configuration|
---@param on_unsupported? fun()
Expand All @@ -879,7 +879,8 @@ function lsp.buf_request(bufnr, method, params, handler, on_unsupported)
if client.supports_method(method, { bufnr = bufnr }) then
method_supported = true

local request_success, request_id = client.request(method, params, handler, bufnr)
local cparams = type(params) == 'function' and params(client, bufnr) or params --[[@as table?]]
local request_success, request_id = client.request(method, cparams, handler, bufnr)
-- This could only fail if the client shut down in the time since we looked
-- it up and we did the request, which should be rare.
if request_success then
Expand Down
137 changes: 68 additions & 69 deletions runtime/lua/vim/lsp/buf.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
local api = vim.api
local lsp = vim.lsp
local validate = vim.validate
local util = require('vim.lsp.util')
local npcall = vim.F.npcall
local ms = require('vim.lsp.protocol').Methods

local M = {}

--- Sends an async request to all active clients attached to the current
--- buffer.
---
---@param method (string) LSP method name
---@param params (table|nil) Parameters to send to the server
---@param handler lsp.Handler? See |lsp-handler|. Follows |lsp-handler-resolution|
---
---@return table<integer, integer> client_request_ids Map of client-id:request-id pairs
---for all successful requests.
---@return function _cancel_all_requests Function which can be used to
---cancel all the requests. You could instead
---iterate all clients and call their `cancel_request()` methods.
---
---@see |vim.lsp.buf_request()|
local function request(method, params, handler)
validate('method', method, 'string')
validate('handler', handler, 'function', true)
return vim.lsp.buf_request(0, method, params, handler)
--- @param params? table
--- @return fun(client: vim.lsp.Client): lsp.TextDocumentPositionParams
local function client_positional_params(params)
local win = api.nvim_get_current_win()
return function(client)
local ret = util.make_position_params(win, client.offset_encoding)
if params then
ret = vim.tbl_extend('force', ret, params)
end
return ret
end
end

--- Displays hover information about the symbol under the cursor in a floating
Expand All @@ -34,30 +28,29 @@ end
--- except that "q" dismisses the window.
--- You can scroll the contents the same as you would any other buffer.
function M.hover()
local params = util.make_position_params()
request(ms.textDocument_hover, params)
lsp.buf_request(0, ms.textDocument_hover, client_positional_params())
end

local function request_with_opts(name, params, opts)
local req_handler --- @type function?
if opts then
req_handler = function(err, result, ctx, config)
local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
local handler = client.handlers[name] or vim.lsp.handlers[name]
local client = assert(lsp.get_client_by_id(ctx.client_id))
local handler = client.handlers[name] or lsp.handlers[name]
handler(err, result, ctx, vim.tbl_extend('force', config or {}, opts))
end
end
request(name, params, req_handler)
lsp.buf_request(0, name, params, req_handler)
end

---@param method string
---@param opts? vim.lsp.LocationOpts
local function get_locations(method, opts)
opts = opts or {}
local bufnr = api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ method = method, bufnr = bufnr })
local clients = lsp.get_clients({ method = method, bufnr = bufnr })
if not next(clients) then
vim.notify(vim.lsp._unsupported_method(method), vim.log.levels.WARN)
vim.notify(lsp._unsupported_method(method), vim.log.levels.WARN)
return
end
local win = api.nvim_get_current_win()
Expand Down Expand Up @@ -185,8 +178,7 @@ end
--- Displays signature information about the symbol under the cursor in a
--- floating window.
function M.signature_help()
local params = util.make_position_params()
request(ms.textDocument_signatureHelp, params)
lsp.buf_request(0, ms.textDocument_signatureHelp, client_positional_params())
end

--- Retrieves the completion items at the current cursor position. Can only be
Expand All @@ -198,9 +190,13 @@ end
---
---@see vim.lsp.protocol.CompletionTriggerKind
function M.completion(context)
local params = util.make_position_params()
params.context = context
return request(ms.textDocument_completion, params)
return lsp.buf_request(
0,
ms.textDocument_completion,
client_positional_params({
context = context,
})
)
end

---@param bufnr integer
Expand Down Expand Up @@ -307,7 +303,7 @@ function M.format(opts)
method = ms.textDocument_formatting
end

local clients = vim.lsp.get_clients({
local clients = lsp.get_clients({
id = opts.id,
bufnr = bufnr,
name = opts.name,
Expand Down Expand Up @@ -344,7 +340,7 @@ function M.format(opts)
end
local params = set_range(client, util.make_formatting_params(opts.formatting_options))
client.request(method, params, function(...)
local handler = client.handlers[method] or vim.lsp.handlers[method]
local handler = client.handlers[method] or lsp.handlers[method]
handler(...)
do_format(next(clients, idx))
end, bufnr)
Expand Down Expand Up @@ -386,7 +382,7 @@ end
function M.rename(new_name, opts)
opts = opts or {}
local bufnr = opts.bufnr or api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({
local clients = lsp.get_clients({
bufnr = bufnr,
name = opts.name,
-- Clients must at least support rename, prepareRename is optional
Expand Down Expand Up @@ -428,7 +424,7 @@ function M.rename(new_name, opts)
local params = util.make_position_params(win, client.offset_encoding)
params.newName = name
local handler = client.handlers[ms.textDocument_rename]
or vim.lsp.handlers[ms.textDocument_rename]
or lsp.handlers[ms.textDocument_rename]
client.request(ms.textDocument_rename, params, function(...)
handler(...)
try_use_client(next(clients, idx))
Expand Down Expand Up @@ -508,7 +504,7 @@ end
function M.references(context, opts)
validate('context', context, 'table', true)
local bufnr = api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ method = ms.textDocument_references, bufnr = bufnr })
local clients = lsp.get_clients({ method = ms.textDocument_references, bufnr = bufnr })
if not next(clients) then
return
end
Expand Down Expand Up @@ -575,7 +571,7 @@ end
--- @param handler? lsp.Handler
--- @param bufnr? integer
local function request_with_id(client_id, method, params, handler, bufnr)
local client = vim.lsp.get_client_by_id(client_id)
local client = lsp.get_client_by_id(client_id)
if not client then
vim.notify(
string.format('Client with id=%d disappeared during call hierarchy request', client_id),
Expand Down Expand Up @@ -606,23 +602,27 @@ end

--- @param method string
local function call_hierarchy(method)
local params = util.make_position_params()
--- @param result lsp.CallHierarchyItem[]?
request(ms.textDocument_prepareCallHierarchy, params, function(err, result, ctx)
if err then
vim.notify(err.message, vim.log.levels.WARN)
return
end
if not result or vim.tbl_isempty(result) then
vim.notify('No item resolved', vim.log.levels.WARN)
return
end
local item = pick_call_hierarchy_item(result)
if not item then
return
lsp.buf_request(
0,
ms.textDocument_prepareCallHierarchy,
client_positional_params(),
--- @param result lsp.CallHierarchyItem[]?
function(err, result, ctx)
if err then
vim.notify(err.message, vim.log.levels.WARN)
return
end
if not result or vim.tbl_isempty(result) then
vim.notify('No item resolved', vim.log.levels.WARN)
return
end
local item = pick_call_hierarchy_item(result)
if not item then
return
end
request_with_id(ctx.client_id, method, { item = item }, nil, ctx.bufnr)
end
request_with_id(ctx.client_id, method, { item = item }, nil, ctx.bufnr)
end)
)
end

--- Lists all the call sites of the symbol under the cursor in the
Expand Down Expand Up @@ -654,9 +654,9 @@ end
function M.typehierarchy(kind)
local method = kind == 'subtypes' and ms.typeHierarchy_subtypes or ms.typeHierarchy_supertypes
local bufnr = api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ bufnr = bufnr, method = method })
local clients = lsp.get_clients({ bufnr = bufnr, method = method })
if not next(clients) then
vim.notify(vim.lsp._unsupported_method(method), vim.log.levels.WARN)
vim.notify(lsp._unsupported_method(method), vim.log.levels.WARN)
return
end

Expand Down Expand Up @@ -713,7 +713,7 @@ end
---
function M.list_workspace_folders()
local workspace_folders = {}
for _, client in pairs(vim.lsp.get_clients({ bufnr = 0 })) do
for _, client in pairs(lsp.get_clients({ bufnr = 0 })) do
for _, folder in pairs(client.workspace_folders or {}) do
table.insert(workspace_folders, folder.name)
end
Expand All @@ -736,7 +736,7 @@ function M.add_workspace_folder(workspace_folder)
return
end
local bufnr = api.nvim_get_current_buf()
for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do
for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do
client:_add_workspace_folder(workspace_folder)
end
end
Expand All @@ -753,7 +753,7 @@ function M.remove_workspace_folder(workspace_folder)
return
end
local bufnr = api.nvim_get_current_buf()
for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do
for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do
client:_remove_workspace_folder(workspace_folder)
end
print(workspace_folder, 'is not currently part of the workspace')
Expand Down Expand Up @@ -792,8 +792,7 @@ end
--- |hl-LspReferenceRead|
--- |hl-LspReferenceWrite|
function M.document_highlight()
local params = util.make_position_params()
request(ms.textDocument_documentHighlight, params)
lsp.buf_request(0, ms.textDocument_documentHighlight, client_positional_params())
end

--- Removes document highlights from current buffer.
Expand Down Expand Up @@ -916,7 +915,7 @@ local function on_code_action_results(results, opts)
-- command: string
-- arguments?: any[]
--
local client = assert(vim.lsp.get_client_by_id(choice.ctx.client_id))
local client = assert(lsp.get_client_by_id(choice.ctx.client_id))
local action = choice.action
local bufnr = assert(choice.ctx.bufnr, 'Must have buffer number')

Expand Down Expand Up @@ -951,14 +950,14 @@ local function on_code_action_results(results, opts)

---@param item {action: lsp.Command|lsp.CodeAction, ctx: lsp.HandlerContext}
local function format_item(item)
local clients = vim.lsp.get_clients({ bufnr = item.ctx.bufnr })
local clients = lsp.get_clients({ bufnr = item.ctx.bufnr })
local title = item.action.title:gsub('\r\n', '\\r\\n'):gsub('\n', '\\n')

if #clients == 1 then
return title
end

local source = vim.lsp.get_client_by_id(item.ctx.client_id).name
local source = lsp.get_client_by_id(item.ctx.client_id).name
return ('%s [%s]'):format(title, source)
end

Expand Down Expand Up @@ -987,16 +986,16 @@ function M.code_action(opts)
end
local context = opts.context and vim.deepcopy(opts.context) or {}
if not context.triggerKind then
context.triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Invoked
context.triggerKind = lsp.protocol.CodeActionTriggerKind.Invoked
end
local mode = api.nvim_get_mode().mode
local bufnr = api.nvim_get_current_buf()
local win = api.nvim_get_current_win()
local clients = vim.lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_codeAction })
local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_codeAction })
local remaining = #clients
if remaining == 0 then
if next(vim.lsp.get_clients({ bufnr = bufnr })) then
vim.notify(vim.lsp._unsupported_method(ms.textDocument_codeAction), vim.log.levels.WARN)
if next(lsp.get_clients({ bufnr = bufnr })) then
vim.notify(lsp._unsupported_method(ms.textDocument_codeAction), vim.log.levels.WARN)
end
return
end
Expand Down Expand Up @@ -1033,8 +1032,8 @@ function M.code_action(opts)
if context.diagnostics then
params.context = context
else
local ns_push = vim.lsp.diagnostic.get_namespace(client.id, false)
local ns_pull = vim.lsp.diagnostic.get_namespace(client.id, true)
local ns_push = lsp.diagnostic.get_namespace(client.id, false)
local ns_pull = lsp.diagnostic.get_namespace(client.id, true)
local diagnostics = {}
local lnum = api.nvim_win_get_cursor(0)[1] - 1
vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_pull, lnum = lnum }))
Expand Down Expand Up @@ -1062,7 +1061,7 @@ function M.execute_command(command_params)
arguments = command_params.arguments,
workDoneToken = command_params.workDoneToken,
}
request(ms.workspace_executeCommand, command_params)
lsp.buf_request(0, ms.workspace_executeCommand, command_params)
end

return M
6 changes: 5 additions & 1 deletion runtime/lua/vim/lsp/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ end

local sysname = vim.uv.os_uname().sysname

--- @class vim.lsp.protocol.constants
--- @nodoc
local constants = {
--- @enum lsp.DiagnosticSeverity
DiagnosticSeverity = {
Expand Down Expand Up @@ -314,7 +316,9 @@ local constants = {
},
}

-- Protocol for the Microsoft Language Server Protocol (mslsp)
--- Protocol for the Microsoft Language Server Protocol (mslsp)
--- @class vim.lsp.protocol : vim.lsp.protocol.constants
--- @nodoc
local protocol = {}

--- @diagnostic disable:no-unknown
Expand Down
3 changes: 3 additions & 0 deletions scripts/gen_vimdoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ local config = {
},
fn_xform = function(fun)
fun.name = fun.name:gsub('result%.', '')
if fun.module == 'vim.lsp.protocol' then
fun.classvar = nil
end
end,
section_fmt = function(name)
if name:lower() == 'lsp' then
Expand Down

0 comments on commit 5c44c02

Please sign in to comment.