refactor(lsp.buf): use alias for vim.lsp

This commit is contained in:
Lewis Russell
2024-10-24 09:55:23 +01:00
parent 008782208d
commit ad4e14c201

View File

@@ -1,4 +1,5 @@
local api = vim.api local api = vim.api
local lsp = vim.lsp
local validate = vim.validate local validate = vim.validate
local util = require('vim.lsp.util') local util = require('vim.lsp.util')
local npcall = vim.F.npcall local npcall = vim.F.npcall
@@ -23,7 +24,7 @@ local M = {}
local function request(method, params, handler) local function request(method, params, handler)
validate('method', method, 'string') validate('method', method, 'string')
validate('handler', handler, 'function', true) validate('handler', handler, 'function', true)
return vim.lsp.buf_request(0, method, params, handler) return lsp.buf_request(0, method, params, handler)
end end
--- Displays hover information about the symbol under the cursor in a floating --- Displays hover information about the symbol under the cursor in a floating
@@ -35,15 +36,15 @@ end
--- You can scroll the contents the same as you would any other buffer. --- You can scroll the contents the same as you would any other buffer.
function M.hover() function M.hover()
local params = util.make_position_params() local params = util.make_position_params()
request(ms.textDocument_hover, params) lsp.buf_request(0, ms.textDocument_hover, params)
end end
local function request_with_opts(name, params, opts) local function request_with_opts(name, params, opts)
local req_handler --- @type function? local req_handler --- @type function?
if opts then if opts then
req_handler = function(err, result, ctx, config) req_handler = function(err, result, ctx, config)
local client = assert(vim.lsp.get_client_by_id(ctx.client_id)) local client = assert(lsp.get_client_by_id(ctx.client_id))
local handler = client.handlers[name] or vim.lsp.handlers[name] local handler = client.handlers[name] or lsp.handlers[name]
handler(err, result, ctx, vim.tbl_extend('force', config or {}, opts)) handler(err, result, ctx, vim.tbl_extend('force', config or {}, opts))
end end
end end
@@ -55,9 +56,9 @@ end
local function get_locations(method, opts) local function get_locations(method, opts)
opts = opts or {} opts = opts or {}
local bufnr = api.nvim_get_current_buf() 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 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 return
end end
local win = api.nvim_get_current_win() local win = api.nvim_get_current_win()
@@ -307,7 +308,7 @@ function M.format(opts)
method = ms.textDocument_formatting method = ms.textDocument_formatting
end end
local clients = vim.lsp.get_clients({ local clients = lsp.get_clients({
id = opts.id, id = opts.id,
bufnr = bufnr, bufnr = bufnr,
name = opts.name, name = opts.name,
@@ -344,7 +345,7 @@ function M.format(opts)
end end
local params = set_range(client, util.make_formatting_params(opts.formatting_options)) local params = set_range(client, util.make_formatting_params(opts.formatting_options))
client.request(method, params, function(...) 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(...) handler(...)
do_format(next(clients, idx)) do_format(next(clients, idx))
end, bufnr) end, bufnr)
@@ -386,7 +387,7 @@ end
function M.rename(new_name, opts) function M.rename(new_name, opts)
opts = opts or {} opts = opts or {}
local bufnr = opts.bufnr or api.nvim_get_current_buf() local bufnr = opts.bufnr or api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({ local clients = lsp.get_clients({
bufnr = bufnr, bufnr = bufnr,
name = opts.name, name = opts.name,
-- Clients must at least support rename, prepareRename is optional -- Clients must at least support rename, prepareRename is optional
@@ -428,7 +429,7 @@ function M.rename(new_name, opts)
local params = util.make_position_params(win, client.offset_encoding) local params = util.make_position_params(win, client.offset_encoding)
params.newName = name params.newName = name
local handler = client.handlers[ms.textDocument_rename] 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(...) client.request(ms.textDocument_rename, params, function(...)
handler(...) handler(...)
try_use_client(next(clients, idx)) try_use_client(next(clients, idx))
@@ -508,7 +509,7 @@ end
function M.references(context, opts) function M.references(context, opts)
validate('context', context, 'table', true) validate('context', context, 'table', true)
local bufnr = api.nvim_get_current_buf() 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 if not next(clients) then
return return
end end
@@ -575,7 +576,7 @@ end
--- @param handler? lsp.Handler --- @param handler? lsp.Handler
--- @param bufnr? integer --- @param bufnr? integer
local function request_with_id(client_id, method, params, handler, bufnr) 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 if not client then
vim.notify( vim.notify(
string.format('Client with id=%d disappeared during call hierarchy request', client_id), string.format('Client with id=%d disappeared during call hierarchy request', client_id),
@@ -654,9 +655,9 @@ end
function M.typehierarchy(kind) function M.typehierarchy(kind)
local method = kind == 'subtypes' and ms.typeHierarchy_subtypes or ms.typeHierarchy_supertypes local method = kind == 'subtypes' and ms.typeHierarchy_subtypes or ms.typeHierarchy_supertypes
local bufnr = api.nvim_get_current_buf() 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 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 return
end end
@@ -713,7 +714,7 @@ end
--- ---
function M.list_workspace_folders() function M.list_workspace_folders()
local 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 for _, folder in pairs(client.workspace_folders or {}) do
table.insert(workspace_folders, folder.name) table.insert(workspace_folders, folder.name)
end end
@@ -736,7 +737,7 @@ function M.add_workspace_folder(workspace_folder)
return return
end end
local bufnr = api.nvim_get_current_buf() 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) client:_add_workspace_folder(workspace_folder)
end end
end end
@@ -753,7 +754,7 @@ function M.remove_workspace_folder(workspace_folder)
return return
end end
local bufnr = api.nvim_get_current_buf() 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) client:_remove_workspace_folder(workspace_folder)
end end
print(workspace_folder, 'is not currently part of the workspace') print(workspace_folder, 'is not currently part of the workspace')
@@ -916,7 +917,7 @@ local function on_code_action_results(results, opts)
-- command: string -- command: string
-- arguments?: any[] -- 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 action = choice.action
local bufnr = assert(choice.ctx.bufnr, 'Must have buffer number') local bufnr = assert(choice.ctx.bufnr, 'Must have buffer number')
@@ -951,14 +952,14 @@ local function on_code_action_results(results, opts)
---@param item {action: lsp.Command|lsp.CodeAction, ctx: lsp.HandlerContext} ---@param item {action: lsp.Command|lsp.CodeAction, ctx: lsp.HandlerContext}
local function format_item(item) 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') local title = item.action.title:gsub('\r\n', '\\r\\n'):gsub('\n', '\\n')
if #clients == 1 then if #clients == 1 then
return title return title
end 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) return ('%s [%s]'):format(title, source)
end end
@@ -987,16 +988,16 @@ function M.code_action(opts)
end end
local context = opts.context and vim.deepcopy(opts.context) or {} local context = opts.context and vim.deepcopy(opts.context) or {}
if not context.triggerKind then if not context.triggerKind then
context.triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Invoked context.triggerKind = lsp.protocol.CodeActionTriggerKind.Invoked
end end
local mode = api.nvim_get_mode().mode local mode = api.nvim_get_mode().mode
local bufnr = api.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
local win = api.nvim_get_current_win() 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 local remaining = #clients
if remaining == 0 then if remaining == 0 then
if next(vim.lsp.get_clients({ bufnr = bufnr })) then if next(lsp.get_clients({ bufnr = bufnr })) then
vim.notify(vim.lsp._unsupported_method(ms.textDocument_codeAction), vim.log.levels.WARN) vim.notify(lsp._unsupported_method(ms.textDocument_codeAction), vim.log.levels.WARN)
end end
return return
end end
@@ -1033,8 +1034,8 @@ function M.code_action(opts)
if context.diagnostics then if context.diagnostics then
params.context = context params.context = context
else else
local ns_push = vim.lsp.diagnostic.get_namespace(client.id, false) local ns_push = lsp.diagnostic.get_namespace(client.id, false)
local ns_pull = vim.lsp.diagnostic.get_namespace(client.id, true) local ns_pull = lsp.diagnostic.get_namespace(client.id, true)
local diagnostics = {} local diagnostics = {}
local lnum = api.nvim_win_get_cursor(0)[1] - 1 local lnum = api.nvim_win_get_cursor(0)[1] - 1
vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_pull, lnum = lnum })) vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_pull, lnum = lnum }))