aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/buf.lua
diff options
context:
space:
mode:
authorLewis Russell <lewis6991@gmail.com>2024-10-24 11:19:38 +0100
committerGitHub <noreply@github.com>2024-10-24 11:19:38 +0100
commit5c44c0240535272064bd4bb11364cb3a64d9b2cb (patch)
tree7e8f780565d187f0da993b13a470a5f423ff87db /runtime/lua/vim/lsp/buf.lua
parent008782208d826a03ab046bb2cb1bdda27c6554d0 (diff)
parent2dcbfe78fcec5f73ce061bb24b718187b9c6b134 (diff)
downloadrneovim-5c44c0240535272064bd4bb11364cb3a64d9b2cb.tar.gz
rneovim-5c44c0240535272064bd4bb11364cb3a64d9b2cb.tar.bz2
rneovim-5c44c0240535272064bd4bb11364cb3a64d9b2cb.zip
Merge pull request #30929 from lewis6991/fix/lsp_param_encodings
Diffstat (limited to 'runtime/lua/vim/lsp/buf.lua')
-rw-r--r--runtime/lua/vim/lsp/buf.lua137
1 files changed, 68 insertions, 69 deletions
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index fc41246588..80ba3c648f 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -1,4 +1,5 @@
local api = vim.api
+local lsp = vim.lsp
local validate = vim.validate
local util = require('vim.lsp.util')
local npcall = vim.F.npcall
@@ -6,24 +7,17 @@ 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
@@ -34,20 +28,19 @@ 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
@@ -55,9 +48,9 @@ end
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()
@@ -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
@@ -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
@@ -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,
@@ -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)
@@ -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
@@ -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))
@@ -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
@@ -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),
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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')
@@ -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.
@@ -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')
@@ -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
@@ -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
@@ -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 }))
@@ -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