refactor: use local api = vim.api

This commit is contained in:
ii14 2022-07-15 18:26:47 +02:00
parent 8bccefcb87
commit f59c96903a
5 changed files with 136 additions and 136 deletions

View File

@ -6,13 +6,13 @@ local util = require('vim.lsp.util')
local sync = require('vim.lsp.sync') local sync = require('vim.lsp.sync')
local vim = vim local vim = vim
local a = vim.api local api = vim.api
local nvim_err_writeln, nvim_buf_get_lines, nvim_command, nvim_buf_get_option, nvim_exec_autocmds = local nvim_err_writeln, nvim_buf_get_lines, nvim_command, nvim_buf_get_option, nvim_exec_autocmds =
a.nvim_err_writeln, api.nvim_err_writeln,
a.nvim_buf_get_lines, api.nvim_buf_get_lines,
a.nvim_command, api.nvim_command,
a.nvim_buf_get_option, api.nvim_buf_get_option,
a.nvim_exec_autocmds api.nvim_exec_autocmds
local uv = vim.loop local uv = vim.loop
local tbl_isempty, tbl_extend = vim.tbl_isempty, vim.tbl_extend local tbl_isempty, tbl_extend = vim.tbl_isempty, vim.tbl_extend
local validate = vim.validate local validate = vim.validate
@ -79,7 +79,7 @@ end
local function resolve_bufnr(bufnr) local function resolve_bufnr(bufnr)
validate({ bufnr = { bufnr, 'n', true } }) validate({ bufnr = { bufnr, 'n', true } })
if bufnr == nil or bufnr == 0 then if bufnr == nil or bufnr == 0 then
return a.nvim_get_current_buf() return api.nvim_get_current_buf()
end end
return bufnr return bufnr
end end
@ -505,7 +505,7 @@ do
end end
buf_state.pending_change = nil buf_state.pending_change = nil
buf_state.last_flush = uv.hrtime() buf_state.last_flush = uv.hrtime()
if client.is_stopped() or not a.nvim_buf_is_valid(bufnr) then if client.is_stopped() or not api.nvim_buf_is_valid(bufnr) then
return return
end end
local changes = state.use_incremental_sync and buf_state.pending_changes local changes = state.use_incremental_sync and buf_state.pending_changes
@ -572,7 +572,7 @@ local function text_document_did_open_handler(bufnr, client)
if not vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then if not vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'openClose') then
return return
end end
if not a.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
return return
end end
local filetype = nvim_buf_get_option(bufnr, 'filetype') local filetype = nvim_buf_get_option(bufnr, 'filetype')
@ -592,7 +592,7 @@ local function text_document_did_open_handler(bufnr, client)
vim.schedule(function() vim.schedule(function()
-- Protect against a race where the buffer disappears -- Protect against a race where the buffer disappears
-- between `did_open_handler` and the scheduled function firing. -- between `did_open_handler` and the scheduled function firing.
if a.nvim_buf_is_valid(bufnr) then if api.nvim_buf_is_valid(bufnr) then
local namespace = vim.lsp.diagnostic.get_namespace(client.id) local namespace = vim.lsp.diagnostic.get_namespace(client.id)
vim.diagnostic.show(namespace, bufnr) vim.diagnostic.show(namespace, bufnr)
end end
@ -738,7 +738,7 @@ function lsp.start(config, opts)
return client.config.root_dir == conf.root_dir and client.name == conf.name return client.config.root_dir == conf.root_dir and client.name == conf.name
end end
config.name = config.name or (config.cmd[1] and vim.fs.basename(config.cmd[1])) or nil config.name = config.name or (config.cmd[1] and vim.fs.basename(config.cmd[1])) or nil
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
for _, clients in ipairs({ uninitialized_clients, lsp.get_active_clients() }) do for _, clients in ipairs({ uninitialized_clients, lsp.get_active_clients() }) do
for _, client in pairs(clients) do for _, client in pairs(clients) do
if reuse_client(client, config) then if reuse_client(client, config) then
@ -1433,7 +1433,7 @@ function lsp.buf_attach_client(bufnr, client_id)
client_id = { client_id, 'n' }, client_id = { client_id, 'n' },
}) })
bufnr = resolve_bufnr(bufnr) bufnr = resolve_bufnr(bufnr)
if not a.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
local _ = log.warn() local _ = log.warn()
and log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr)) and log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr))
return false return false
@ -1451,9 +1451,9 @@ function lsp.buf_attach_client(bufnr, client_id)
au BufWritePost <buffer=%d> lua vim.lsp._text_document_did_save_handler(0) au BufWritePost <buffer=%d> lua vim.lsp._text_document_did_save_handler(0)
augroup END augroup END
]=] ]=]
a.nvim_exec(string.format(buf_did_save_autocommand, client_id, bufnr, bufnr), false) api.nvim_exec(string.format(buf_did_save_autocommand, client_id, bufnr, bufnr), false)
-- First time, so attach and set up stuff. -- First time, so attach and set up stuff.
a.nvim_buf_attach(bufnr, false, { api.nvim_buf_attach(bufnr, false, {
on_lines = text_document_did_change_handler, on_lines = text_document_did_change_handler,
on_reload = function() on_reload = function()
local params = { textDocument = { uri = uri } } local params = { textDocument = { uri = uri } }
@ -1888,8 +1888,8 @@ function lsp.omnifunc(findstart, base)
-- Then, perform standard completion request -- Then, perform standard completion request
local _ = log.info() and log.info('base ', base) local _ = log.info() and log.info('base ', base)
local pos = a.nvim_win_get_cursor(0) local pos = api.nvim_win_get_cursor(0)
local line = a.nvim_get_current_line() local line = api.nvim_get_current_line()
local line_to_cursor = line:sub(1, pos[2]) local line_to_cursor = line:sub(1, pos[2])
local _ = log.trace() and log.trace('omnifunc.line', pos, line) local _ = log.trace() and log.trace('omnifunc.line', pos, line)

View File

@ -1,5 +1,5 @@
local vim = vim local vim = vim
local a = vim.api local api = vim.api
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
@ -181,7 +181,7 @@ end
function M.format(options) function M.format(options)
options = options or {} options = options or {}
local bufnr = options.bufnr or a.nvim_get_current_buf() local bufnr = options.bufnr or api.nvim_get_current_buf()
local clients = vim.lsp.get_active_clients({ local clients = vim.lsp.get_active_clients({
id = options.id, id = options.id,
bufnr = bufnr, bufnr = bufnr,
@ -242,7 +242,7 @@ function M.formatting(options)
vim.log.levels.WARN vim.log.levels.WARN
) )
local params = util.make_formatting_params(options) local params = util.make_formatting_params(options)
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
select_client('textDocument/formatting', function(client) select_client('textDocument/formatting', function(client)
if client == nil then if client == nil then
return return
@ -270,7 +270,7 @@ function M.formatting_sync(options, timeout_ms)
vim.log.levels.WARN vim.log.levels.WARN
) )
local params = util.make_formatting_params(options) local params = util.make_formatting_params(options)
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
select_client('textDocument/formatting', function(client) select_client('textDocument/formatting', function(client)
if client == nil then if client == nil then
return return
@ -307,7 +307,7 @@ function M.formatting_seq_sync(options, timeout_ms, order)
vim.log.levels.WARN vim.log.levels.WARN
) )
local clients = vim.tbl_values(vim.lsp.buf_get_clients()) local clients = vim.tbl_values(vim.lsp.buf_get_clients())
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
-- sort the clients according to `order` -- sort the clients according to `order`
for _, client_name in pairs(order or {}) do for _, client_name in pairs(order or {}) do
@ -328,7 +328,7 @@ function M.formatting_seq_sync(options, timeout_ms, order)
'textDocument/formatting', 'textDocument/formatting',
params, params,
timeout_ms, timeout_ms,
a.nvim_get_current_buf() api.nvim_get_current_buf()
) )
if result and result.result then if result and result.result then
util.apply_text_edits(result.result, bufnr, client.offset_encoding) util.apply_text_edits(result.result, bufnr, client.offset_encoding)
@ -374,7 +374,7 @@ end
--- this field. --- this field.
function M.rename(new_name, options) function M.rename(new_name, options)
options = options or {} options = options or {}
local bufnr = options.bufnr or a.nvim_get_current_buf() local bufnr = options.bufnr or api.nvim_get_current_buf()
local clients = vim.lsp.get_active_clients({ local clients = vim.lsp.get_active_clients({
bufnr = bufnr, bufnr = bufnr,
name = options.name, name = options.name,
@ -392,14 +392,14 @@ function M.rename(new_name, options)
vim.notify('[LSP] Rename, no matching language servers with rename capability.') vim.notify('[LSP] Rename, no matching language servers with rename capability.')
end end
local win = a.nvim_get_current_win() local win = api.nvim_get_current_win()
-- Compute early to account for cursor movements after going async -- Compute early to account for cursor movements after going async
local cword = vim.fn.expand('<cword>') local cword = vim.fn.expand('<cword>')
---@private ---@private
local function get_text_at_range(range, offset_encoding) local function get_text_at_range(range, offset_encoding)
return a.nvim_buf_get_text( return api.nvim_buf_get_text(
bufnr, bufnr,
range.start.line, range.start.line,
util._get_line_byte_from_position(bufnr, range.start, offset_encoding), util._get_line_byte_from_position(bufnr, range.start, offset_encoding),
@ -584,7 +584,7 @@ end
function M.add_workspace_folder(workspace_folder) function M.add_workspace_folder(workspace_folder)
workspace_folder = workspace_folder workspace_folder = workspace_folder
or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'), 'dir') or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'), 'dir')
a.nvim_command('redraw') api.nvim_command('redraw')
if not (workspace_folder and #workspace_folder > 0) then if not (workspace_folder and #workspace_folder > 0) then
return return
end end
@ -621,7 +621,7 @@ end
function M.remove_workspace_folder(workspace_folder) function M.remove_workspace_folder(workspace_folder)
workspace_folder = workspace_folder workspace_folder = workspace_folder
or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h')) or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'))
a.nvim_command('redraw') api.nvim_command('redraw')
if not (workspace_folder and #workspace_folder > 0) then if not (workspace_folder and #workspace_folder > 0) then
return return
end end
@ -818,7 +818,7 @@ end
--- with all aggregated results --- with all aggregated results
---@private ---@private
local function code_action_request(params, options) local function code_action_request(params, options)
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
local method = 'textDocument/codeAction' local method = 'textDocument/codeAction'
vim.lsp.buf_request_all(bufnr, method, params, function(results) vim.lsp.buf_request_all(bufnr, method, params, function(results)
local ctx = { bufnr = bufnr, method = method, params = params } local ctx = { bufnr = bufnr, method = method, params = params }
@ -855,7 +855,7 @@ function M.code_action(options)
end end
local context = options.context or {} local context = options.context or {}
if not context.diagnostics then if not context.diagnostics then
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr) context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr)
end end
local params = util.make_range_params() local params = util.make_range_params()
@ -882,7 +882,7 @@ function M.range_code_action(context, start_pos, end_pos)
validate({ context = { context, 't', true } }) validate({ context = { context, 't', true } })
context = context or {} context = context or {}
if not context.diagnostics then if not context.diagnostics then
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr) context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr)
end end
local params = util.make_given_range_params(start_pos, end_pos) local params = util.make_given_range_params(start_pos, end_pos)

View File

@ -1,6 +1,6 @@
local util = require('vim.lsp.util') local util = require('vim.lsp.util')
local log = require('vim.lsp.log') local log = require('vim.lsp.log')
local a = vim.api local api = vim.api
local M = {} local M = {}
--- bufnr → true|nil --- bufnr → true|nil
@ -10,14 +10,14 @@ local active_refreshes = {}
--- bufnr -> client_id -> lenses --- bufnr -> client_id -> lenses
local lens_cache_by_buf = setmetatable({}, { local lens_cache_by_buf = setmetatable({}, {
__index = function(t, b) __index = function(t, b)
local key = b > 0 and b or a.nvim_get_current_buf() local key = b > 0 and b or api.nvim_get_current_buf()
return rawget(t, key) return rawget(t, key)
end, end,
}) })
local namespaces = setmetatable({}, { local namespaces = setmetatable({}, {
__index = function(t, key) __index = function(t, key)
local value = a.nvim_create_namespace('vim_lsp_codelens:' .. key) local value = api.nvim_create_namespace('vim_lsp_codelens:' .. key)
rawset(t, key, value) rawset(t, key, value)
return value return value
end, end,
@ -29,7 +29,7 @@ M.__namespaces = namespaces
---@private ---@private
local function execute_lens(lens, bufnr, client_id) local function execute_lens(lens, bufnr, client_id)
local line = lens.range.start.line local line = lens.range.start.line
a.nvim_buf_clear_namespace(bufnr, namespaces[client_id], line, line + 1) api.nvim_buf_clear_namespace(bufnr, namespaces[client_id], line, line + 1)
local client = vim.lsp.get_client_by_id(client_id) local client = vim.lsp.get_client_by_id(client_id)
assert(client, 'Client is required to execute lens, client_id=' .. client_id) assert(client, 'Client is required to execute lens, client_id=' .. client_id)
@ -78,8 +78,8 @@ end
--- Run the code lens in the current line --- Run the code lens in the current line
--- ---
function M.run() function M.run()
local line = a.nvim_win_get_cursor(0)[1] local line = api.nvim_win_get_cursor(0)[1]
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
local options = {} local options = {}
local lenses_by_client = lens_cache_by_buf[bufnr] or {} local lenses_by_client = lens_cache_by_buf[bufnr] or {}
for client, lenses in pairs(lenses_by_client) do for client, lenses in pairs(lenses_by_client) do
@ -127,10 +127,10 @@ function M.display(lenses, bufnr, client_id)
table.insert(line_lenses, lens) table.insert(line_lenses, lens)
end end
local ns = namespaces[client_id] local ns = namespaces[client_id]
local num_lines = a.nvim_buf_line_count(bufnr) local num_lines = api.nvim_buf_line_count(bufnr)
for i = 0, num_lines do for i = 0, num_lines do
local line_lenses = lenses_by_lnum[i] or {} local line_lenses = lenses_by_lnum[i] or {}
a.nvim_buf_clear_namespace(bufnr, ns, i, i + 1) api.nvim_buf_clear_namespace(bufnr, ns, i, i + 1)
local chunks = {} local chunks = {}
local num_line_lenses = #line_lenses local num_line_lenses = #line_lenses
table.sort(line_lenses, function(a, b) table.sort(line_lenses, function(a, b)
@ -144,7 +144,7 @@ function M.display(lenses, bufnr, client_id)
end end
end end
if #chunks > 0 then if #chunks > 0 then
a.nvim_buf_set_extmark(bufnr, ns, i, 0, { api.nvim_buf_set_extmark(bufnr, ns, i, 0, {
virt_text = chunks, virt_text = chunks,
hl_mode = 'combine', hl_mode = 'combine',
}) })
@ -163,12 +163,12 @@ function M.save(lenses, bufnr, client_id)
lenses_by_client = {} lenses_by_client = {}
lens_cache_by_buf[bufnr] = lenses_by_client lens_cache_by_buf[bufnr] = lenses_by_client
local ns = namespaces[client_id] local ns = namespaces[client_id]
a.nvim_buf_attach(bufnr, false, { api.nvim_buf_attach(bufnr, false, {
on_detach = function(b) on_detach = function(b)
lens_cache_by_buf[b] = nil lens_cache_by_buf[b] = nil
end, end,
on_lines = function(_, b, _, first_lnum, last_lnum) on_lines = function(_, b, _, first_lnum, last_lnum)
a.nvim_buf_clear_namespace(b, ns, first_lnum, last_lnum) api.nvim_buf_clear_namespace(b, ns, first_lnum, last_lnum)
end, end,
}) })
end end
@ -203,7 +203,7 @@ local function resolve_lenses(lenses, bufnr, client_id, callback)
-- Eager display to have some sort of incremental feedback -- Eager display to have some sort of incremental feedback
-- Once all lenses got resolved there will be a full redraw for all lenses -- Once all lenses got resolved there will be a full redraw for all lenses
-- So that multiple lens per line are properly displayed -- So that multiple lens per line are properly displayed
a.nvim_buf_set_extmark( api.nvim_buf_set_extmark(
bufnr, bufnr,
ns, ns,
lens.range.start.line, lens.range.start.line,
@ -249,7 +249,7 @@ function M.refresh()
local params = { local params = {
textDocument = util.make_text_document_params(), textDocument = util.make_text_document_params(),
} }
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
if active_refreshes[bufnr] then if active_refreshes[bufnr] then
return return
end end

View File

@ -2,7 +2,7 @@ local log = require('vim.lsp.log')
local protocol = require('vim.lsp.protocol') local protocol = require('vim.lsp.protocol')
local util = require('vim.lsp.util') local util = require('vim.lsp.util')
local vim = vim local vim = vim
local a = vim.api local api = vim.api
local M = {} local M = {}
@ -13,7 +13,7 @@ local M = {}
---@param ... (table of strings) Will be concatenated before being written ---@param ... (table of strings) Will be concatenated before being written
local function err_message(...) local function err_message(...)
vim.notify(table.concat(vim.tbl_flatten({ ... })), vim.log.levels.ERROR) vim.notify(table.concat(vim.tbl_flatten({ ... })), vim.log.levels.ERROR)
a.nvim_command('redraw') api.nvim_command('redraw')
end end
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand
@ -61,7 +61,7 @@ local function progress_handler(_, result, ctx, _)
client.messages.progress[token].done = true client.messages.progress[token].done = true
end end
a.nvim_command('doautocmd <nomodeline> User LspProgressUpdate') api.nvim_command('doautocmd <nomodeline> User LspProgressUpdate')
end end
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#progress --see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#progress
@ -195,14 +195,14 @@ M['textDocument/references'] = function(_, result, ctx, config)
items = util.locations_to_items(result, client.offset_encoding), items = util.locations_to_items(result, client.offset_encoding),
context = ctx, context = ctx,
}) })
a.nvim_command('lopen') api.nvim_command('lopen')
else else
vim.fn.setqflist({}, ' ', { vim.fn.setqflist({}, ' ', {
title = 'References', title = 'References',
items = util.locations_to_items(result, client.offset_encoding), items = util.locations_to_items(result, client.offset_encoding),
context = ctx, context = ctx,
}) })
a.nvim_command('botright copen') api.nvim_command('botright copen')
end end
end end
end end
@ -230,14 +230,14 @@ local function response_to_list(map_result, entity, title_fn)
items = map_result(result, ctx.bufnr), items = map_result(result, ctx.bufnr),
context = ctx, context = ctx,
}) })
a.nvim_command('lopen') api.nvim_command('lopen')
else else
vim.fn.setqflist({}, ' ', { vim.fn.setqflist({}, ' ', {
title = title_fn(ctx), title = title_fn(ctx),
items = map_result(result, ctx.bufnr), items = map_result(result, ctx.bufnr),
context = ctx, context = ctx,
}) })
a.nvim_command('botright copen') api.nvim_command('botright copen')
end end
end end
end end
@ -290,8 +290,8 @@ M['textDocument/completion'] = function(_, result, _, _)
if vim.tbl_isempty(result or {}) then if vim.tbl_isempty(result or {}) then
return return
end end
local row, col = unpack(a.nvim_win_get_cursor(0)) local row, col = unpack(api.nvim_win_get_cursor(0))
local line = assert(a.nvim_buf_get_lines(0, row - 1, row, false)[1]) local line = assert(api.nvim_buf_get_lines(0, row - 1, row, false)[1])
local line_to_cursor = line:sub(col + 1) local line_to_cursor = line:sub(col + 1)
local textMatch = vim.fn.match(line_to_cursor, '\\k*$') local textMatch = vim.fn.match(line_to_cursor, '\\k*$')
local prefix = line_to_cursor:sub(textMatch + 1) local prefix = line_to_cursor:sub(textMatch + 1)
@ -358,7 +358,7 @@ local function location_handler(_, result, ctx, config)
title = 'LSP locations', title = 'LSP locations',
items = util.locations_to_items(result, client.offset_encoding), items = util.locations_to_items(result, client.offset_encoding),
}) })
a.nvim_command('botright copen') api.nvim_command('botright copen')
end end
else else
util.jump_to_location(result, client.offset_encoding, config.reuse_win) util.jump_to_location(result, client.offset_encoding, config.reuse_win)
@ -402,7 +402,7 @@ function M.signature_help(_, result, ctx, config)
local client = vim.lsp.get_client_by_id(ctx.client_id) local client = vim.lsp.get_client_by_id(ctx.client_id)
local triggers = local triggers =
vim.tbl_get(client.server_capabilities, 'signatureHelpProvider', 'triggerCharacters') vim.tbl_get(client.server_capabilities, 'signatureHelpProvider', 'triggerCharacters')
local ft = a.nvim_buf_get_option(ctx.bufnr, 'filetype') local ft = api.nvim_buf_get_option(ctx.bufnr, 'filetype')
local lines, hl = util.convert_signature_help_to_markdown_lines(result, ft, triggers) local lines, hl = util.convert_signature_help_to_markdown_lines(result, ft, triggers)
lines = util.trim_empty_lines(lines) lines = util.trim_empty_lines(lines)
if vim.tbl_isempty(lines) then if vim.tbl_isempty(lines) then
@ -413,7 +413,7 @@ function M.signature_help(_, result, ctx, config)
end end
local fbuf, fwin = util.open_floating_preview(lines, 'markdown', config) local fbuf, fwin = util.open_floating_preview(lines, 'markdown', config)
if hl then if hl then
a.nvim_buf_add_highlight(fbuf, -1, 'LspSignatureActiveParameter', 0, unpack(hl)) api.nvim_buf_add_highlight(fbuf, -1, 'LspSignatureActiveParameter', 0, unpack(hl))
end end
return fbuf, fwin return fbuf, fwin
end end
@ -459,7 +459,7 @@ local make_call_hierarchy_handler = function(direction)
end end
end end
vim.fn.setqflist({}, ' ', { title = 'LSP call hierarchy', items = items }) vim.fn.setqflist({}, ' ', { title = 'LSP call hierarchy', items = items })
a.nvim_command('botright copen') api.nvim_command('botright copen')
end end
end end
@ -505,7 +505,7 @@ M['window/showMessage'] = function(_, result, ctx, _)
err_message('LSP[', client_name, '] ', message) err_message('LSP[', client_name, '] ', message)
else else
local message_type_name = protocol.MessageType[message_type] local message_type_name = protocol.MessageType[message_type]
a.nvim_out_write(string.format('LSP[%s][%s] %s\n', client_name, message_type_name, message)) api.nvim_out_write(string.format('LSP[%s][%s] %s\n', client_name, message_type_name, message))
end end
return result return result
end end

View File

@ -2,7 +2,7 @@ local protocol = require('vim.lsp.protocol')
local snippet = require('vim.lsp._snippet') local snippet = require('vim.lsp._snippet')
local vim = vim local vim = vim
local validate = vim.validate local validate = vim.validate
local a = vim.api local api = vim.api
local list_extend = vim.list_extend local list_extend = vim.list_extend
local highlight = require('vim.highlight') local highlight = require('vim.highlight')
local uv = vim.loop local uv = vim.loop
@ -238,14 +238,14 @@ local function get_lines(bufnr, rows)
-- This is needed for bufload and bufloaded -- This is needed for bufload and bufloaded
if bufnr == 0 then if bufnr == 0 then
bufnr = a.nvim_get_current_buf() bufnr = api.nvim_get_current_buf()
end end
---@private ---@private
local function buf_lines() local function buf_lines()
local lines = {} local lines = {}
for _, row in pairs(rows) do for _, row in pairs(rows) do
lines[row] = (a.nvim_buf_get_lines(bufnr, row, row + 1, false) or { '' })[1] lines[row] = (api.nvim_buf_get_lines(bufnr, row, row + 1, false) or { '' })[1]
end end
return lines return lines
end end
@ -264,7 +264,7 @@ local function get_lines(bufnr, rows)
return buf_lines() return buf_lines()
end end
local filename = a.nvim_buf_get_name(bufnr) local filename = api.nvim_buf_get_name(bufnr)
-- get the data from the file -- get the data from the file
local fd = uv.fs_open(filename, 'r', 438) local fd = uv.fs_open(filename, 'r', 438)
@ -389,10 +389,10 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
if not next(text_edits) then if not next(text_edits) then
return return
end end
if not a.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
end end
a.nvim_buf_set_option(bufnr, 'buflisted', true) api.nvim_buf_set_option(bufnr, 'buflisted', true)
-- Fix reversed range and indexing each text_edits -- Fix reversed range and indexing each text_edits
local index = 0 local index = 0
@ -427,7 +427,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
-- Some LSP servers are depending on the VSCode behavior. -- Some LSP servers are depending on the VSCode behavior.
-- The VSCode will re-locate the cursor position after applying TextEdit so we also do it. -- The VSCode will re-locate the cursor position after applying TextEdit so we also do it.
local is_current_buf = a.nvim_get_current_buf() == bufnr local is_current_buf = api.nvim_get_current_buf() == bufnr
local cursor = (function() local cursor = (function()
if not is_current_buf then if not is_current_buf then
return { return {
@ -435,7 +435,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
col = -1, col = -1,
} }
end end
local cursor = a.nvim_win_get_cursor(0) local cursor = api.nvim_win_get_cursor(0)
return { return {
row = cursor[1] - 1, row = cursor[1] - 1,
col = cursor[2], col = cursor[2],
@ -459,7 +459,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
} }
-- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't accept it so we should fix it here. -- Some LSP servers may return +1 range of the buffer content but nvim_buf_set_text can't accept it so we should fix it here.
local max = a.nvim_buf_line_count(bufnr) local max = api.nvim_buf_line_count(bufnr)
if max <= e.start_row or max <= e.end_row then if max <= e.start_row or max <= e.end_row then
local len = #(get_line(bufnr, max - 1) or '') local len = #(get_line(bufnr, max - 1) or '')
if max <= e.start_row then if max <= e.start_row then
@ -473,7 +473,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
end end
has_eol_text_edit = true has_eol_text_edit = true
end end
a.nvim_buf_set_text(bufnr, e.start_row, e.start_col, e.end_row, e.end_col, e.text) api.nvim_buf_set_text(bufnr, e.start_row, e.start_col, e.end_row, e.end_col, e.text)
-- Fix cursor position. -- Fix cursor position.
local row_count = (e.end_row - e.start_row) + 1 local row_count = (e.end_row - e.start_row) + 1
@ -490,7 +490,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
end end
end end
local max = a.nvim_buf_line_count(bufnr) local max = api.nvim_buf_line_count(bufnr)
-- Apply fixed cursor position. -- Apply fixed cursor position.
if is_cursor_fixed then if is_cursor_fixed then
@ -498,7 +498,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
is_valid_cursor = is_valid_cursor and cursor.row < max is_valid_cursor = is_valid_cursor and cursor.row < max
is_valid_cursor = is_valid_cursor and cursor.col <= #(get_line(bufnr, max - 1) or '') is_valid_cursor = is_valid_cursor and cursor.col <= #(get_line(bufnr, max - 1) or '')
if is_valid_cursor then if is_valid_cursor then
a.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col }) api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col })
end end
end end
@ -506,12 +506,12 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
local fix_eol = has_eol_text_edit local fix_eol = has_eol_text_edit
fix_eol = fix_eol fix_eol = fix_eol
and ( and (
a.nvim_buf_get_option(bufnr, 'eol') api.nvim_buf_get_option(bufnr, 'eol')
or (a.nvim_buf_get_option(bufnr, 'fixeol') and not a.nvim_buf_get_option(bufnr, 'binary')) or (api.nvim_buf_get_option(bufnr, 'fixeol') and not api.nvim_buf_get_option(bufnr, 'binary'))
) )
fix_eol = fix_eol and get_line(bufnr, max - 1) == '' fix_eol = fix_eol and get_line(bufnr, max - 1) == ''
if fix_eol then if fix_eol then
a.nvim_buf_set_lines(bufnr, -2, -1, false, {}) api.nvim_buf_set_lines(bufnr, -2, -1, false, {})
end end
end end
@ -710,8 +710,8 @@ end
---@private ---@private
--- Like vim.fn.bufwinid except it works across tabpages. --- Like vim.fn.bufwinid except it works across tabpages.
local function bufwinid(bufnr) local function bufwinid(bufnr)
for _, win in ipairs(a.nvim_list_wins()) do for _, win in ipairs(api.nvim_list_wins()) do
if a.nvim_win_get_buf(win) == bufnr then if api.nvim_win_get_buf(win) == bufnr then
return win return win
end end
end end
@ -733,7 +733,7 @@ function M.rename(old_fname, new_fname, opts)
vim.fn.bufload(oldbuf) vim.fn.bufload(oldbuf)
-- The there may be pending changes in the buffer -- The there may be pending changes in the buffer
a.nvim_buf_call(oldbuf, function() api.nvim_buf_call(oldbuf, function()
vim.cmd('w!') vim.cmd('w!')
end) end)
@ -743,9 +743,9 @@ function M.rename(old_fname, new_fname, opts)
local newbuf = vim.fn.bufadd(new_fname) local newbuf = vim.fn.bufadd(new_fname)
local win = bufwinid(oldbuf) local win = bufwinid(oldbuf)
if win then if win then
a.nvim_win_set_buf(win, newbuf) api.nvim_win_set_buf(win, newbuf)
end end
a.nvim_buf_delete(oldbuf, { force = true }) api.nvim_buf_delete(oldbuf, { force = true })
end end
---@private ---@private
@ -778,7 +778,7 @@ local function delete_file(change)
local bufnr = vim.fn.bufadd(fname) local bufnr = vim.fn.bufadd(fname)
local result = tonumber(vim.fn.delete(fname, flags)) local result = tonumber(vim.fn.delete(fname, flags))
assert(result == 0, 'Could not delete file: ' .. fname .. ', stat: ' .. vim.inspect(stat)) assert(result == 0, 'Could not delete file: ' .. fname .. ', stat: ' .. vim.inspect(stat))
a.nvim_buf_delete(bufnr, { force = true }) api.nvim_buf_delete(bufnr, { force = true })
end end
--- Applies a `WorkspaceEdit`. --- Applies a `WorkspaceEdit`.
@ -1013,7 +1013,7 @@ function M.make_floating_popup_options(width, height, opts)
row = 0 row = 0
end end
if vim.fn.wincol() + width + (opts.offset_x or 0) <= a.nvim_get_option('columns') then if vim.fn.wincol() + width + (opts.offset_x or 0) <= api.nvim_get_option('columns') then
anchor = anchor .. 'W' anchor = anchor .. 'W'
col = 0 col = 0
else else
@ -1065,15 +1065,15 @@ function M.jump_to_location(location, offset_encoding, reuse_win)
--- Jump to new location (adjusting for UTF-16 encoding of characters) --- Jump to new location (adjusting for UTF-16 encoding of characters)
local win = reuse_win and bufwinid(bufnr) local win = reuse_win and bufwinid(bufnr)
if win then if win then
a.nvim_set_current_win(win) api.nvim_set_current_win(win)
else else
a.nvim_buf_set_option(bufnr, 'buflisted', true) api.nvim_buf_set_option(bufnr, 'buflisted', true)
a.nvim_set_current_buf(bufnr) api.nvim_set_current_buf(bufnr)
end end
local range = location.range or location.targetSelectionRange local range = location.range or location.targetSelectionRange
local row = range.start.line local row = range.start.line
local col = get_line_byte_from_position(bufnr, range.start, offset_encoding) local col = get_line_byte_from_position(bufnr, range.start, offset_encoding)
a.nvim_win_set_cursor(0, { row + 1, col }) api.nvim_win_set_cursor(0, { row + 1, col })
-- Open folds under the cursor -- Open folds under the cursor
vim.cmd('normal! zv') vim.cmd('normal! zv')
return true return true
@ -1094,17 +1094,17 @@ function M.preview_location(location, opts)
return return
end end
local bufnr = vim.uri_to_bufnr(uri) local bufnr = vim.uri_to_bufnr(uri)
if not a.nvim_buf_is_loaded(bufnr) then if not api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
end end
local range = location.targetRange or location.range local range = location.targetRange or location.range
local contents = a.nvim_buf_get_lines(bufnr, range.start.line, range['end'].line + 1, false) local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range['end'].line + 1, false)
local syntax = a.nvim_buf_get_option(bufnr, 'syntax') local syntax = api.nvim_buf_get_option(bufnr, 'syntax')
if syntax == '' then if syntax == '' then
-- When no syntax is set, we use filetype as fallback. This might not result -- When no syntax is set, we use filetype as fallback. This might not result
-- in a valid syntax definition. See also ft detection in stylize_markdown. -- in a valid syntax definition. See also ft detection in stylize_markdown.
-- An empty syntax is more common now with TreeSitter, since TS disables syntax. -- An empty syntax is more common now with TreeSitter, since TS disables syntax.
syntax = a.nvim_buf_get_option(bufnr, 'filetype') syntax = api.nvim_buf_get_option(bufnr, 'filetype')
end end
opts = opts or {} opts = opts or {}
opts.focus_id = 'location' opts.focus_id = 'location'
@ -1113,8 +1113,8 @@ end
---@private ---@private
local function find_window_by_var(name, value) local function find_window_by_var(name, value)
for _, win in ipairs(a.nvim_list_wins()) do for _, win in ipairs(api.nvim_list_wins()) do
if npcall(a.nvim_win_get_var, win, name) == value then if npcall(api.nvim_win_get_var, win, name) == value then
return win return win
end end
end end
@ -1279,7 +1279,7 @@ function M.stylize_markdown(bufnr, contents, opts)
end end
-- Compute size of float needed to show (wrapped) lines -- Compute size of float needed to show (wrapped) lines
opts.wrap_at = opts.wrap_at or (vim.wo['wrap'] and a.nvim_win_get_width(0)) opts.wrap_at = opts.wrap_at or (vim.wo['wrap'] and api.nvim_win_get_width(0))
local width = M._make_floating_popup_size(stripped, opts) local width = M._make_floating_popup_size(stripped, opts)
local sep_line = string.rep('', math.min(width, opts.wrap_at or width)) local sep_line = string.rep('', math.min(width, opts.wrap_at or width))
@ -1290,7 +1290,7 @@ function M.stylize_markdown(bufnr, contents, opts)
end end
end end
a.nvim_buf_set_lines(bufnr, 0, -1, false, stripped) api.nvim_buf_set_lines(bufnr, 0, -1, false, stripped)
local idx = 1 local idx = 1
---@private ---@private
@ -1315,7 +1315,7 @@ function M.stylize_markdown(bufnr, contents, opts)
local lang = '@' .. ft:upper() local lang = '@' .. ft:upper()
if not langs[lang] then if not langs[lang] then
-- HACK: reset current_syntax, since some syntax files like markdown won't load if it is already set -- HACK: reset current_syntax, since some syntax files like markdown won't load if it is already set
pcall(a.nvim_buf_del_var, bufnr, 'current_syntax') pcall(api.nvim_buf_del_var, bufnr, 'current_syntax')
-- TODO(ashkan): better validation before this. -- TODO(ashkan): better validation before this.
if not pcall(vim.cmd, string.format('syntax include %s syntax/%s.vim', lang, ft)) then if not pcall(vim.cmd, string.format('syntax include %s syntax/%s.vim', lang, ft)) then
return return
@ -1334,7 +1334,7 @@ function M.stylize_markdown(bufnr, contents, opts)
end end
-- needs to run in the buffer for the regions to work -- needs to run in the buffer for the regions to work
a.nvim_buf_call(bufnr, function() api.nvim_buf_call(bufnr, function()
-- we need to apply lsp_markdown regions speperately, since otherwise -- we need to apply lsp_markdown regions speperately, since otherwise
-- markdown regions can "bleed" through the other syntax regions -- markdown regions can "bleed" through the other syntax regions
-- and mess up the formatting -- and mess up the formatting
@ -1362,13 +1362,13 @@ end
local function close_preview_window(winnr, bufnrs) local function close_preview_window(winnr, bufnrs)
vim.schedule(function() vim.schedule(function()
-- exit if we are in one of ignored buffers -- exit if we are in one of ignored buffers
if bufnrs and vim.tbl_contains(bufnrs, a.nvim_get_current_buf()) then if bufnrs and vim.tbl_contains(bufnrs, api.nvim_get_current_buf()) then
return return
end end
local augroup = 'preview_window_' .. winnr local augroup = 'preview_window_' .. winnr
pcall(a.nvim_del_augroup_by_name, augroup) pcall(api.nvim_del_augroup_by_name, augroup)
pcall(a.nvim_win_close, winnr, true) pcall(api.nvim_win_close, winnr, true)
end) end)
end end
@ -1380,13 +1380,13 @@ end
---@param bufnrs table list of buffers where the preview window will remain visible ---@param bufnrs table list of buffers where the preview window will remain visible
---@see |autocmd-events| ---@see |autocmd-events|
local function close_preview_autocmd(events, winnr, bufnrs) local function close_preview_autocmd(events, winnr, bufnrs)
local augroup = a.nvim_create_augroup('preview_window_' .. winnr, { local augroup = api.nvim_create_augroup('preview_window_' .. winnr, {
clear = true, clear = true,
}) })
-- close the preview window when entered a buffer that is not -- close the preview window when entered a buffer that is not
-- the floating window buffer or the buffer that spawned it -- the floating window buffer or the buffer that spawned it
a.nvim_create_autocmd('BufEnter', { api.nvim_create_autocmd('BufEnter', {
group = augroup, group = augroup,
callback = function() callback = function()
close_preview_window(winnr, bufnrs) close_preview_window(winnr, bufnrs)
@ -1394,7 +1394,7 @@ local function close_preview_autocmd(events, winnr, bufnrs)
}) })
if #events > 0 then if #events > 0 then
a.nvim_create_autocmd(events, { api.nvim_create_autocmd(events, {
buffer = bufnrs[2], buffer = bufnrs[2],
callback = function() callback = function()
close_preview_window(winnr) close_preview_window(winnr)
@ -1438,7 +1438,7 @@ function M._make_floating_popup_size(contents, opts)
end end
local border_width = get_border_size(opts).width local border_width = get_border_size(opts).width
local screen_width = a.nvim_win_get_width(0) local screen_width = api.nvim_win_get_width(0)
width = math.min(width, screen_width) width = math.min(width, screen_width)
-- make sure borders are always inside the screen -- make sure borders are always inside the screen
@ -1511,35 +1511,35 @@ function M.open_floating_preview(contents, syntax, opts)
opts.focus = opts.focus ~= false opts.focus = opts.focus ~= false
opts.close_events = opts.close_events or { 'CursorMoved', 'CursorMovedI', 'InsertCharPre' } opts.close_events = opts.close_events or { 'CursorMoved', 'CursorMovedI', 'InsertCharPre' }
local bufnr = a.nvim_get_current_buf() local bufnr = api.nvim_get_current_buf()
-- check if this popup is focusable and we need to focus -- check if this popup is focusable and we need to focus
if opts.focus_id and opts.focusable ~= false and opts.focus then if opts.focus_id and opts.focusable ~= false and opts.focus then
-- Go back to previous window if we are in a focusable one -- Go back to previous window if we are in a focusable one
local current_winnr = a.nvim_get_current_win() local current_winnr = api.nvim_get_current_win()
if npcall(a.nvim_win_get_var, current_winnr, opts.focus_id) then if npcall(api.nvim_win_get_var, current_winnr, opts.focus_id) then
a.nvim_command('wincmd p') api.nvim_command('wincmd p')
return bufnr, current_winnr return bufnr, current_winnr
end end
do do
local win = find_window_by_var(opts.focus_id, bufnr) local win = find_window_by_var(opts.focus_id, bufnr)
if win and a.nvim_win_is_valid(win) and vim.fn.pumvisible() == 0 then if win and api.nvim_win_is_valid(win) and vim.fn.pumvisible() == 0 then
-- focus and return the existing buf, win -- focus and return the existing buf, win
a.nvim_set_current_win(win) api.nvim_set_current_win(win)
a.nvim_command('stopinsert') api.nvim_command('stopinsert')
return a.nvim_win_get_buf(win), win return api.nvim_win_get_buf(win), win
end end
end end
end end
-- check if another floating preview already exists for this buffer -- check if another floating preview already exists for this buffer
-- and close it if needed -- and close it if needed
local existing_float = npcall(a.nvim_buf_get_var, bufnr, 'lsp_floating_preview') local existing_float = npcall(api.nvim_buf_get_var, bufnr, 'lsp_floating_preview')
if existing_float and a.nvim_win_is_valid(existing_float) then if existing_float and api.nvim_win_is_valid(existing_float) then
a.nvim_win_close(existing_float, true) api.nvim_win_close(existing_float, true)
end end
local floating_bufnr = a.nvim_create_buf(false, true) local floating_bufnr = api.nvim_create_buf(false, true)
local do_stylize = syntax == 'markdown' and opts.stylize_markdown local do_stylize = syntax == 'markdown' and opts.stylize_markdown
-- Clean up input: trim empty lines from the end, pad -- Clean up input: trim empty lines from the end, pad
@ -1550,33 +1550,33 @@ function M.open_floating_preview(contents, syntax, opts)
contents = M.stylize_markdown(floating_bufnr, contents, opts) contents = M.stylize_markdown(floating_bufnr, contents, opts)
else else
if syntax then if syntax then
a.nvim_buf_set_option(floating_bufnr, 'syntax', syntax) api.nvim_buf_set_option(floating_bufnr, 'syntax', syntax)
end end
a.nvim_buf_set_lines(floating_bufnr, 0, -1, true, contents) api.nvim_buf_set_lines(floating_bufnr, 0, -1, true, contents)
end end
-- Compute size of float needed to show (wrapped) lines -- Compute size of float needed to show (wrapped) lines
if opts.wrap then if opts.wrap then
opts.wrap_at = opts.wrap_at or a.nvim_win_get_width(0) opts.wrap_at = opts.wrap_at or api.nvim_win_get_width(0)
else else
opts.wrap_at = nil opts.wrap_at = nil
end end
local width, height = M._make_floating_popup_size(contents, opts) local width, height = M._make_floating_popup_size(contents, opts)
local float_option = M.make_floating_popup_options(width, height, opts) local float_option = M.make_floating_popup_options(width, height, opts)
local floating_winnr = a.nvim_open_win(floating_bufnr, false, float_option) local floating_winnr = api.nvim_open_win(floating_bufnr, false, float_option)
if do_stylize then if do_stylize then
a.nvim_win_set_option(floating_winnr, 'conceallevel', 2) api.nvim_win_set_option(floating_winnr, 'conceallevel', 2)
a.nvim_win_set_option(floating_winnr, 'concealcursor', 'n') api.nvim_win_set_option(floating_winnr, 'concealcursor', 'n')
end end
-- disable folding -- disable folding
a.nvim_win_set_option(floating_winnr, 'foldenable', false) api.nvim_win_set_option(floating_winnr, 'foldenable', false)
-- soft wrapping -- soft wrapping
a.nvim_win_set_option(floating_winnr, 'wrap', opts.wrap) api.nvim_win_set_option(floating_winnr, 'wrap', opts.wrap)
a.nvim_buf_set_option(floating_bufnr, 'modifiable', false) api.nvim_buf_set_option(floating_bufnr, 'modifiable', false)
a.nvim_buf_set_option(floating_bufnr, 'bufhidden', 'wipe') api.nvim_buf_set_option(floating_bufnr, 'bufhidden', 'wipe')
a.nvim_buf_set_keymap( api.nvim_buf_set_keymap(
floating_bufnr, floating_bufnr,
'n', 'n',
'q', 'q',
@ -1587,22 +1587,22 @@ function M.open_floating_preview(contents, syntax, opts)
-- save focus_id -- save focus_id
if opts.focus_id then if opts.focus_id then
a.nvim_win_set_var(floating_winnr, opts.focus_id, bufnr) api.nvim_win_set_var(floating_winnr, opts.focus_id, bufnr)
end end
a.nvim_buf_set_var(bufnr, 'lsp_floating_preview', floating_winnr) api.nvim_buf_set_var(bufnr, 'lsp_floating_preview', floating_winnr)
return floating_bufnr, floating_winnr return floating_bufnr, floating_winnr
end end
do --[[ References ]] do --[[ References ]]
local reference_ns = a.nvim_create_namespace('vim_lsp_references') local reference_ns = api.nvim_create_namespace('vim_lsp_references')
--- Removes document highlights from a buffer. --- Removes document highlights from a buffer.
--- ---
---@param bufnr number Buffer id ---@param bufnr number Buffer id
function M.buf_clear_references(bufnr) function M.buf_clear_references(bufnr)
validate({ bufnr = { bufnr, 'n', true } }) validate({ bufnr = { bufnr, 'n', true } })
a.nvim_buf_clear_namespace(bufnr or 0, reference_ns, 0, -1) api.nvim_buf_clear_namespace(bufnr or 0, reference_ns, 0, -1)
end end
--- Shows a list of document highlights for a certain buffer. --- Shows a list of document highlights for a certain buffer.
@ -1750,7 +1750,7 @@ function M.symbols_to_items(symbols, bufnr)
local kind = M._get_symbol_kind_name(symbol.kind) local kind = M._get_symbol_kind_name(symbol.kind)
table.insert(_items, { table.insert(_items, {
-- bufnr = _bufnr, -- bufnr = _bufnr,
filename = a.nvim_buf_get_name(_bufnr), filename = api.nvim_buf_get_name(_bufnr),
lnum = symbol.selectionRange.start.line + 1, lnum = symbol.selectionRange.start.line + 1,
col = symbol.selectionRange.start.character + 1, col = symbol.selectionRange.start.character + 1,
kind = kind, kind = kind,
@ -1824,11 +1824,11 @@ end
---@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to `offset_encoding` of first client of buffer of `window` ---@param offset_encoding string utf-8|utf-16|utf-32|nil defaults to `offset_encoding` of first client of buffer of `window`
local function make_position_param(window, offset_encoding) local function make_position_param(window, offset_encoding)
window = window or 0 window = window or 0
local buf = a.nvim_win_get_buf(window) local buf = api.nvim_win_get_buf(window)
local row, col = unpack(a.nvim_win_get_cursor(window)) local row, col = unpack(api.nvim_win_get_cursor(window))
offset_encoding = offset_encoding or M._get_offset_encoding(buf) offset_encoding = offset_encoding or M._get_offset_encoding(buf)
row = row - 1 row = row - 1
local line = a.nvim_buf_get_lines(buf, row, row + 1, true)[1] local line = api.nvim_buf_get_lines(buf, row, row + 1, true)[1]
if not line then if not line then
return { line = 0, character = 0 } return { line = 0, character = 0 }
end end
@ -1846,7 +1846,7 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentPositionParams ---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentPositionParams
function M.make_position_params(window, offset_encoding) function M.make_position_params(window, offset_encoding)
window = window or 0 window = window or 0
local buf = a.nvim_win_get_buf(window) local buf = api.nvim_win_get_buf(window)
offset_encoding = offset_encoding or M._get_offset_encoding(buf) offset_encoding = offset_encoding or M._get_offset_encoding(buf)
return { return {
textDocument = M.make_text_document_params(buf), textDocument = M.make_text_document_params(buf),
@ -1898,7 +1898,7 @@ end
---@returns { textDocument = { uri = `current_file_uri` }, range = { start = ---@returns { textDocument = { uri = `current_file_uri` }, range = { start =
---`current_position`, end = `current_position` } } ---`current_position`, end = `current_position` } }
function M.make_range_params(window, offset_encoding) function M.make_range_params(window, offset_encoding)
local buf = a.nvim_win_get_buf(window or 0) local buf = api.nvim_win_get_buf(window or 0)
offset_encoding = offset_encoding or M._get_offset_encoding(buf) offset_encoding = offset_encoding or M._get_offset_encoding(buf)
local position = make_position_param(window, offset_encoding) local position = make_position_param(window, offset_encoding)
return { return {
@ -1924,10 +1924,10 @@ function M.make_given_range_params(start_pos, end_pos, bufnr, offset_encoding)
end_pos = { end_pos, 't', true }, end_pos = { end_pos, 't', true },
offset_encoding = { offset_encoding, 's', true }, offset_encoding = { offset_encoding, 's', true },
}) })
bufnr = bufnr or a.nvim_get_current_buf() bufnr = bufnr or api.nvim_get_current_buf()
offset_encoding = offset_encoding or M._get_offset_encoding(bufnr) offset_encoding = offset_encoding or M._get_offset_encoding(bufnr)
local A = list_extend({}, start_pos or a.nvim_buf_get_mark(bufnr, '<')) local A = list_extend({}, start_pos or api.nvim_buf_get_mark(bufnr, '<'))
local B = list_extend({}, end_pos or a.nvim_buf_get_mark(bufnr, '>')) local B = list_extend({}, end_pos or api.nvim_buf_get_mark(bufnr, '>'))
-- convert to 0-index -- convert to 0-index
A[1] = A[1] - 1 A[1] = A[1] - 1
B[1] = B[1] - 1 B[1] = B[1] - 1