aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorii14 <ii14@users.noreply.github.com>2022-07-15 18:26:47 +0200
committerii14 <ii14@users.noreply.github.com>2022-07-15 18:26:47 +0200
commitf59c96903a61702c69a5bf23a7adb09fff435331 (patch)
tree5329c3cfd9c212be9e12900cebfd9a07cd0a44f0
parent8bccefcb87ec1beb91fc42e4646201917d85baf7 (diff)
downloadrneovim-f59c96903a61702c69a5bf23a7adb09fff435331.tar.gz
rneovim-f59c96903a61702c69a5bf23a7adb09fff435331.tar.bz2
rneovim-f59c96903a61702c69a5bf23a7adb09fff435331.zip
refactor: use `local api = vim.api`
-rw-r--r--runtime/lua/vim/lsp.lua32
-rw-r--r--runtime/lua/vim/lsp/buf.lua28
-rw-r--r--runtime/lua/vim/lsp/codelens.lua26
-rw-r--r--runtime/lua/vim/lsp/handlers.lua28
-rw-r--r--runtime/lua/vim/lsp/util.lua158
5 files changed, 136 insertions, 136 deletions
diff --git a/runtime/lua/vim/lsp.lua b/runtime/lua/vim/lsp.lua
index f6870cbd94..7c95ecef92 100644
--- a/runtime/lua/vim/lsp.lua
+++ b/runtime/lua/vim/lsp.lua
@@ -6,13 +6,13 @@ local util = require('vim.lsp.util')
local sync = require('vim.lsp.sync')
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 =
- a.nvim_err_writeln,
- a.nvim_buf_get_lines,
- a.nvim_command,
- a.nvim_buf_get_option,
- a.nvim_exec_autocmds
+ api.nvim_err_writeln,
+ api.nvim_buf_get_lines,
+ api.nvim_command,
+ api.nvim_buf_get_option,
+ api.nvim_exec_autocmds
local uv = vim.loop
local tbl_isempty, tbl_extend = vim.tbl_isempty, vim.tbl_extend
local validate = vim.validate
@@ -79,7 +79,7 @@ end
local function resolve_bufnr(bufnr)
validate({ bufnr = { bufnr, 'n', true } })
if bufnr == nil or bufnr == 0 then
- return a.nvim_get_current_buf()
+ return api.nvim_get_current_buf()
end
return bufnr
end
@@ -505,7 +505,7 @@ do
end
buf_state.pending_change = nil
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
end
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
return
end
- if not a.nvim_buf_is_loaded(bufnr) then
+ if not api.nvim_buf_is_loaded(bufnr) then
return
end
local filetype = nvim_buf_get_option(bufnr, 'filetype')
@@ -592,7 +592,7 @@ local function text_document_did_open_handler(bufnr, client)
vim.schedule(function()
-- Protect against a race where the buffer disappears
-- 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)
vim.diagnostic.show(namespace, bufnr)
end
@@ -738,7 +738,7 @@ function lsp.start(config, opts)
return client.config.root_dir == conf.root_dir and client.name == conf.name
end
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 _, client in pairs(clients) do
if reuse_client(client, config) then
@@ -1433,7 +1433,7 @@ function lsp.buf_attach_client(bufnr, client_id)
client_id = { client_id, 'n' },
})
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()
and log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr))
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)
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.
- a.nvim_buf_attach(bufnr, false, {
+ api.nvim_buf_attach(bufnr, false, {
on_lines = text_document_did_change_handler,
on_reload = function()
local params = { textDocument = { uri = uri } }
@@ -1888,8 +1888,8 @@ function lsp.omnifunc(findstart, base)
-- Then, perform standard completion request
local _ = log.info() and log.info('base ', base)
- local pos = a.nvim_win_get_cursor(0)
- local line = a.nvim_get_current_line()
+ local pos = api.nvim_win_get_cursor(0)
+ local line = api.nvim_get_current_line()
local line_to_cursor = line:sub(1, pos[2])
local _ = log.trace() and log.trace('omnifunc.line', pos, line)
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index 07f552239f..50a51e897c 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -1,5 +1,5 @@
local vim = vim
-local a = vim.api
+local api = vim.api
local validate = vim.validate
local util = require('vim.lsp.util')
local npcall = vim.F.npcall
@@ -181,7 +181,7 @@ end
function M.format(options)
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({
id = options.id,
bufnr = bufnr,
@@ -242,7 +242,7 @@ function M.formatting(options)
vim.log.levels.WARN
)
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)
if client == nil then
return
@@ -270,7 +270,7 @@ function M.formatting_sync(options, timeout_ms)
vim.log.levels.WARN
)
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)
if client == nil then
return
@@ -307,7 +307,7 @@ function M.formatting_seq_sync(options, timeout_ms, order)
vim.log.levels.WARN
)
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`
for _, client_name in pairs(order or {}) do
@@ -328,7 +328,7 @@ function M.formatting_seq_sync(options, timeout_ms, order)
'textDocument/formatting',
params,
timeout_ms,
- a.nvim_get_current_buf()
+ api.nvim_get_current_buf()
)
if result and result.result then
util.apply_text_edits(result.result, bufnr, client.offset_encoding)
@@ -374,7 +374,7 @@ end
--- this field.
function M.rename(new_name, options)
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({
bufnr = bufnr,
name = options.name,
@@ -392,14 +392,14 @@ function M.rename(new_name, options)
vim.notify('[LSP] Rename, no matching language servers with rename capability.')
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
local cword = vim.fn.expand('<cword>')
---@private
local function get_text_at_range(range, offset_encoding)
- return a.nvim_buf_get_text(
+ return api.nvim_buf_get_text(
bufnr,
range.start.line,
util._get_line_byte_from_position(bufnr, range.start, offset_encoding),
@@ -584,7 +584,7 @@ end
function M.add_workspace_folder(workspace_folder)
workspace_folder = workspace_folder
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
return
end
@@ -621,7 +621,7 @@ end
function M.remove_workspace_folder(workspace_folder)
workspace_folder = workspace_folder
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
return
end
@@ -818,7 +818,7 @@ end
--- with all aggregated results
---@private
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'
vim.lsp.buf_request_all(bufnr, method, params, function(results)
local ctx = { bufnr = bufnr, method = method, params = params }
@@ -855,7 +855,7 @@ function M.code_action(options)
end
local context = options.context or {}
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)
end
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 } })
context = context or {}
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)
end
local params = util.make_given_range_params(start_pos, end_pos)
diff --git a/runtime/lua/vim/lsp/codelens.lua b/runtime/lua/vim/lsp/codelens.lua
index 0e48bd85f8..4fa02c8db2 100644
--- a/runtime/lua/vim/lsp/codelens.lua
+++ b/runtime/lua/vim/lsp/codelens.lua
@@ -1,6 +1,6 @@
local util = require('vim.lsp.util')
local log = require('vim.lsp.log')
-local a = vim.api
+local api = vim.api
local M = {}
--- bufnr → true|nil
@@ -10,14 +10,14 @@ local active_refreshes = {}
--- bufnr -> client_id -> lenses
local lens_cache_by_buf = setmetatable({}, {
__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)
end,
})
local namespaces = setmetatable({}, {
__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)
return value
end,
@@ -29,7 +29,7 @@ M.__namespaces = namespaces
---@private
local function execute_lens(lens, bufnr, client_id)
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)
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
---
function M.run()
- local line = a.nvim_win_get_cursor(0)[1]
- local bufnr = a.nvim_get_current_buf()
+ local line = api.nvim_win_get_cursor(0)[1]
+ local bufnr = api.nvim_get_current_buf()
local options = {}
local lenses_by_client = lens_cache_by_buf[bufnr] or {}
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)
end
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
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 num_line_lenses = #line_lenses
table.sort(line_lenses, function(a, b)
@@ -144,7 +144,7 @@ function M.display(lenses, bufnr, client_id)
end
end
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,
hl_mode = 'combine',
})
@@ -163,12 +163,12 @@ function M.save(lenses, bufnr, client_id)
lenses_by_client = {}
lens_cache_by_buf[bufnr] = lenses_by_client
local ns = namespaces[client_id]
- a.nvim_buf_attach(bufnr, false, {
+ api.nvim_buf_attach(bufnr, false, {
on_detach = function(b)
lens_cache_by_buf[b] = nil
end,
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
@@ -203,7 +203,7 @@ local function resolve_lenses(lenses, bufnr, client_id, callback)
-- Eager display to have some sort of incremental feedback
-- Once all lenses got resolved there will be a full redraw for all lenses
-- So that multiple lens per line are properly displayed
- a.nvim_buf_set_extmark(
+ api.nvim_buf_set_extmark(
bufnr,
ns,
lens.range.start.line,
@@ -249,7 +249,7 @@ function M.refresh()
local 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
return
end
diff --git a/runtime/lua/vim/lsp/handlers.lua b/runtime/lua/vim/lsp/handlers.lua
index 3c43676850..68e1f59aaf 100644
--- a/runtime/lua/vim/lsp/handlers.lua
+++ b/runtime/lua/vim/lsp/handlers.lua
@@ -2,7 +2,7 @@ local log = require('vim.lsp.log')
local protocol = require('vim.lsp.protocol')
local util = require('vim.lsp.util')
local vim = vim
-local a = vim.api
+local api = vim.api
local M = {}
@@ -13,7 +13,7 @@ local M = {}
---@param ... (table of strings) Will be concatenated before being written
local function err_message(...)
vim.notify(table.concat(vim.tbl_flatten({ ... })), vim.log.levels.ERROR)
- a.nvim_command('redraw')
+ api.nvim_command('redraw')
end
--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
end
- a.nvim_command('doautocmd <nomodeline> User LspProgressUpdate')
+ api.nvim_command('doautocmd <nomodeline> User LspProgressUpdate')
end
--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),
context = ctx,
})
- a.nvim_command('lopen')
+ api.nvim_command('lopen')
else
vim.fn.setqflist({}, ' ', {
title = 'References',
items = util.locations_to_items(result, client.offset_encoding),
context = ctx,
})
- a.nvim_command('botright copen')
+ api.nvim_command('botright copen')
end
end
end
@@ -230,14 +230,14 @@ local function response_to_list(map_result, entity, title_fn)
items = map_result(result, ctx.bufnr),
context = ctx,
})
- a.nvim_command('lopen')
+ api.nvim_command('lopen')
else
vim.fn.setqflist({}, ' ', {
title = title_fn(ctx),
items = map_result(result, ctx.bufnr),
context = ctx,
})
- a.nvim_command('botright copen')
+ api.nvim_command('botright copen')
end
end
end
@@ -290,8 +290,8 @@ M['textDocument/completion'] = function(_, result, _, _)
if vim.tbl_isempty(result or {}) then
return
end
- local row, col = unpack(a.nvim_win_get_cursor(0))
- local line = assert(a.nvim_buf_get_lines(0, row - 1, row, false)[1])
+ local row, col = unpack(api.nvim_win_get_cursor(0))
+ local line = assert(api.nvim_buf_get_lines(0, row - 1, row, false)[1])
local line_to_cursor = line:sub(col + 1)
local textMatch = vim.fn.match(line_to_cursor, '\\k*$')
local prefix = line_to_cursor:sub(textMatch + 1)
@@ -358,7 +358,7 @@ local function location_handler(_, result, ctx, config)
title = 'LSP locations',
items = util.locations_to_items(result, client.offset_encoding),
})
- a.nvim_command('botright copen')
+ api.nvim_command('botright copen')
end
else
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 triggers =
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)
lines = util.trim_empty_lines(lines)
if vim.tbl_isempty(lines) then
@@ -413,7 +413,7 @@ function M.signature_help(_, result, ctx, config)
end
local fbuf, fwin = util.open_floating_preview(lines, 'markdown', config)
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
return fbuf, fwin
end
@@ -459,7 +459,7 @@ local make_call_hierarchy_handler = function(direction)
end
end
vim.fn.setqflist({}, ' ', { title = 'LSP call hierarchy', items = items })
- a.nvim_command('botright copen')
+ api.nvim_command('botright copen')
end
end
@@ -505,7 +505,7 @@ M['window/showMessage'] = function(_, result, ctx, _)
err_message('LSP[', client_name, '] ', message)
else
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
return result
end
diff --git a/runtime/lua/vim/lsp/util.lua b/runtime/lua/vim/lsp/util.lua
index 5e3f95cf94..89b2301aa7 100644
--- a/runtime/lua/vim/lsp/util.lua
+++ b/runtime/lua/vim/lsp/util.lua
@@ -2,7 +2,7 @@ local protocol = require('vim.lsp.protocol')
local snippet = require('vim.lsp._snippet')
local vim = vim
local validate = vim.validate
-local a = vim.api
+local api = vim.api
local list_extend = vim.list_extend
local highlight = require('vim.highlight')
local uv = vim.loop
@@ -238,14 +238,14 @@ local function get_lines(bufnr, rows)
-- This is needed for bufload and bufloaded
if bufnr == 0 then
- bufnr = a.nvim_get_current_buf()
+ bufnr = api.nvim_get_current_buf()
end
---@private
local function buf_lines()
local lines = {}
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
return lines
end
@@ -264,7 +264,7 @@ local function get_lines(bufnr, rows)
return buf_lines()
end
- local filename = a.nvim_buf_get_name(bufnr)
+ local filename = api.nvim_buf_get_name(bufnr)
-- get the data from the file
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
return
end
- if not a.nvim_buf_is_loaded(bufnr) then
+ if not api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
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
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.
-- 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()
if not is_current_buf then
return {
@@ -435,7 +435,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
col = -1,
}
end
- local cursor = a.nvim_win_get_cursor(0)
+ local cursor = api.nvim_win_get_cursor(0)
return {
row = cursor[1] - 1,
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.
- 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
local len = #(get_line(bufnr, max - 1) or '')
if max <= e.start_row then
@@ -473,7 +473,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
end
has_eol_text_edit = true
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.
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
- local max = a.nvim_buf_line_count(bufnr)
+ local max = api.nvim_buf_line_count(bufnr)
-- Apply fixed cursor position.
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.col <= #(get_line(bufnr, max - 1) or '')
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
@@ -506,12 +506,12 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
local fix_eol = has_eol_text_edit
fix_eol = fix_eol
and (
- a.nvim_buf_get_option(bufnr, 'eol')
- or (a.nvim_buf_get_option(bufnr, 'fixeol') and not a.nvim_buf_get_option(bufnr, 'binary'))
+ api.nvim_buf_get_option(bufnr, 'eol')
+ 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) == ''
if fix_eol then
- a.nvim_buf_set_lines(bufnr, -2, -1, false, {})
+ api.nvim_buf_set_lines(bufnr, -2, -1, false, {})
end
end
@@ -710,8 +710,8 @@ end
---@private
--- Like vim.fn.bufwinid except it works across tabpages.
local function bufwinid(bufnr)
- for _, win in ipairs(a.nvim_list_wins()) do
- if a.nvim_win_get_buf(win) == bufnr then
+ for _, win in ipairs(api.nvim_list_wins()) do
+ if api.nvim_win_get_buf(win) == bufnr then
return win
end
end
@@ -733,7 +733,7 @@ function M.rename(old_fname, new_fname, opts)
vim.fn.bufload(oldbuf)
-- The there may be pending changes in the buffer
- a.nvim_buf_call(oldbuf, function()
+ api.nvim_buf_call(oldbuf, function()
vim.cmd('w!')
end)
@@ -743,9 +743,9 @@ function M.rename(old_fname, new_fname, opts)
local newbuf = vim.fn.bufadd(new_fname)
local win = bufwinid(oldbuf)
if win then
- a.nvim_win_set_buf(win, newbuf)
+ api.nvim_win_set_buf(win, newbuf)
end
- a.nvim_buf_delete(oldbuf, { force = true })
+ api.nvim_buf_delete(oldbuf, { force = true })
end
---@private
@@ -778,7 +778,7 @@ local function delete_file(change)
local bufnr = vim.fn.bufadd(fname)
local result = tonumber(vim.fn.delete(fname, flags))
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
--- Applies a `WorkspaceEdit`.
@@ -1013,7 +1013,7 @@ function M.make_floating_popup_options(width, height, opts)
row = 0
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'
col = 0
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)
local win = reuse_win and bufwinid(bufnr)
if win then
- a.nvim_set_current_win(win)
+ api.nvim_set_current_win(win)
else
- a.nvim_buf_set_option(bufnr, 'buflisted', true)
- a.nvim_set_current_buf(bufnr)
+ api.nvim_buf_set_option(bufnr, 'buflisted', true)
+ api.nvim_set_current_buf(bufnr)
end
local range = location.range or location.targetSelectionRange
local row = range.start.line
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
vim.cmd('normal! zv')
return true
@@ -1094,17 +1094,17 @@ function M.preview_location(location, opts)
return
end
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)
end
local range = location.targetRange or location.range
- local contents = a.nvim_buf_get_lines(bufnr, range.start.line, range['end'].line + 1, false)
- local syntax = a.nvim_buf_get_option(bufnr, 'syntax')
+ local contents = api.nvim_buf_get_lines(bufnr, range.start.line, range['end'].line + 1, false)
+ local syntax = api.nvim_buf_get_option(bufnr, 'syntax')
if syntax == '' then
-- 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.
-- 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
opts = opts or {}
opts.focus_id = 'location'
@@ -1113,8 +1113,8 @@ end
---@private
local function find_window_by_var(name, value)
- for _, win in ipairs(a.nvim_list_wins()) do
- if npcall(a.nvim_win_get_var, win, name) == value then
+ for _, win in ipairs(api.nvim_list_wins()) do
+ if npcall(api.nvim_win_get_var, win, name) == value then
return win
end
end
@@ -1279,7 +1279,7 @@ function M.stylize_markdown(bufnr, contents, opts)
end
-- 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 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
- a.nvim_buf_set_lines(bufnr, 0, -1, false, stripped)
+ api.nvim_buf_set_lines(bufnr, 0, -1, false, stripped)
local idx = 1
---@private
@@ -1315,7 +1315,7 @@ function M.stylize_markdown(bufnr, contents, opts)
local lang = '@' .. ft:upper()
if not langs[lang] then
-- 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.
if not pcall(vim.cmd, string.format('syntax include %s syntax/%s.vim', lang, ft)) then
return
@@ -1334,7 +1334,7 @@ function M.stylize_markdown(bufnr, contents, opts)
end
-- 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
-- markdown regions can "bleed" through the other syntax regions
-- and mess up the formatting
@@ -1362,13 +1362,13 @@ end
local function close_preview_window(winnr, bufnrs)
vim.schedule(function()
-- 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
end
local augroup = 'preview_window_' .. winnr
- pcall(a.nvim_del_augroup_by_name, augroup)
- pcall(a.nvim_win_close, winnr, true)
+ pcall(api.nvim_del_augroup_by_name, augroup)
+ pcall(api.nvim_win_close, winnr, true)
end)
end
@@ -1380,13 +1380,13 @@ end
---@param bufnrs table list of buffers where the preview window will remain visible
---@see |autocmd-events|
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,
})
-- close the preview window when entered a buffer that is not
-- the floating window buffer or the buffer that spawned it
- a.nvim_create_autocmd('BufEnter', {
+ api.nvim_create_autocmd('BufEnter', {
group = augroup,
callback = function()
close_preview_window(winnr, bufnrs)
@@ -1394,7 +1394,7 @@ local function close_preview_autocmd(events, winnr, bufnrs)
})
if #events > 0 then
- a.nvim_create_autocmd(events, {
+ api.nvim_create_autocmd(events, {
buffer = bufnrs[2],
callback = function()
close_preview_window(winnr)
@@ -1438,7 +1438,7 @@ function M._make_floating_popup_size(contents, opts)
end
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)
-- 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.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
if opts.focus_id and opts.focusable ~= false and opts.focus then
-- Go back to previous window if we are in a focusable one
- local current_winnr = a.nvim_get_current_win()
- if npcall(a.nvim_win_get_var, current_winnr, opts.focus_id) then
- a.nvim_command('wincmd p')
+ local current_winnr = api.nvim_get_current_win()
+ if npcall(api.nvim_win_get_var, current_winnr, opts.focus_id) then
+ api.nvim_command('wincmd p')
return bufnr, current_winnr
end
do
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
- a.nvim_set_current_win(win)
- a.nvim_command('stopinsert')
- return a.nvim_win_get_buf(win), win
+ api.nvim_set_current_win(win)
+ api.nvim_command('stopinsert')
+ return api.nvim_win_get_buf(win), win
end
end
end
-- check if another floating preview already exists for this buffer
-- and close it if needed
- local existing_float = npcall(a.nvim_buf_get_var, bufnr, 'lsp_floating_preview')
- if existing_float and a.nvim_win_is_valid(existing_float) then
- a.nvim_win_close(existing_float, true)
+ local existing_float = npcall(api.nvim_buf_get_var, bufnr, 'lsp_floating_preview')
+ if existing_float and api.nvim_win_is_valid(existing_float) then
+ api.nvim_win_close(existing_float, true)
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
-- 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)
else
if syntax then
- a.nvim_buf_set_option(floating_bufnr, 'syntax', syntax)
+ api.nvim_buf_set_option(floating_bufnr, 'syntax', syntax)
end
- a.nvim_buf_set_lines(floating_bufnr, 0, -1, true, contents)
+ api.nvim_buf_set_lines(floating_bufnr, 0, -1, true, contents)
end
-- Compute size of float needed to show (wrapped) lines
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
opts.wrap_at = nil
end
local width, height = M._make_floating_popup_size(contents, 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
- a.nvim_win_set_option(floating_winnr, 'conceallevel', 2)
- a.nvim_win_set_option(floating_winnr, 'concealcursor', 'n')
+ api.nvim_win_set_option(floating_winnr, 'conceallevel', 2)
+ api.nvim_win_set_option(floating_winnr, 'concealcursor', 'n')
end
-- disable folding
- a.nvim_win_set_option(floating_winnr, 'foldenable', false)
+ api.nvim_win_set_option(floating_winnr, 'foldenable', false)
-- 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)
- a.nvim_buf_set_option(floating_bufnr, 'bufhidden', 'wipe')
- a.nvim_buf_set_keymap(
+ api.nvim_buf_set_option(floating_bufnr, 'modifiable', false)
+ api.nvim_buf_set_option(floating_bufnr, 'bufhidden', 'wipe')
+ api.nvim_buf_set_keymap(
floating_bufnr,
'n',
'q',
@@ -1587,22 +1587,22 @@ function M.open_floating_preview(contents, syntax, opts)
-- save focus_id
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
- 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
end
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.
---
---@param bufnr number Buffer id
function M.buf_clear_references(bufnr)
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
--- 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)
table.insert(_items, {
-- bufnr = _bufnr,
- filename = a.nvim_buf_get_name(_bufnr),
+ filename = api.nvim_buf_get_name(_bufnr),
lnum = symbol.selectionRange.start.line + 1,
col = symbol.selectionRange.start.character + 1,
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`
local function make_position_param(window, offset_encoding)
window = window or 0
- local buf = a.nvim_win_get_buf(window)
- local row, col = unpack(a.nvim_win_get_cursor(window))
+ local buf = api.nvim_win_get_buf(window)
+ local row, col = unpack(api.nvim_win_get_cursor(window))
offset_encoding = offset_encoding or M._get_offset_encoding(buf)
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
return { line = 0, character = 0 }
end
@@ -1846,7 +1846,7 @@ end
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocumentPositionParams
function M.make_position_params(window, offset_encoding)
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)
return {
textDocument = M.make_text_document_params(buf),
@@ -1898,7 +1898,7 @@ end
---@returns { textDocument = { uri = `current_file_uri` }, range = { start =
---`current_position`, end = `current_position` } }
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)
local position = make_position_param(window, offset_encoding)
return {
@@ -1924,10 +1924,10 @@ function M.make_given_range_params(start_pos, end_pos, bufnr, offset_encoding)
end_pos = { end_pos, 't', 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)
- local A = list_extend({}, start_pos or a.nvim_buf_get_mark(bufnr, '<'))
- local B = list_extend({}, end_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 api.nvim_buf_get_mark(bufnr, '>'))
-- convert to 0-index
A[1] = A[1] - 1
B[1] = B[1] - 1