Merge #19309 from ii14/lsp_refactor_1

refactor(lsp): make the use of local aliases more consistent
This commit is contained in:
Justin M. Keyes
2022-07-17 11:43:44 +02:00
committed by GitHub
6 changed files with 63 additions and 83 deletions

View File

@@ -4,15 +4,15 @@ local lsp_rpc = require('vim.lsp.rpc')
local protocol = require('vim.lsp.protocol')
local util = require('vim.lsp.util')
local sync = require('vim.lsp.sync')
local api = vim.api
local vim = vim
local api = vim.api
local nvim_err_writeln, nvim_buf_get_lines, nvim_command, nvim_buf_get_option, nvim_exec_autocmds =
vim.api.nvim_err_writeln,
vim.api.nvim_buf_get_lines,
vim.api.nvim_command,
vim.api.nvim_buf_get_option,
vim.api.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 vim.api.nvim_get_current_buf()
return api.nvim_get_current_buf()
end
return bufnr
end
@@ -216,7 +216,7 @@ end
---@returns (string) the command
---@returns (list of strings) its arguments
function lsp._cmd_parts(input)
vim.validate({
validate({
cmd = {
input,
function()
@@ -230,7 +230,7 @@ function lsp._cmd_parts(input)
local cmd_args = {}
-- Don't mutate our input.
for i, v in ipairs(input) do
vim.validate({ ['cmd argument'] = { v, 's' } })
validate({ ['cmd argument'] = { v, 's' } })
if i > 1 then
table.insert(cmd_args, v)
end
@@ -505,7 +505,7 @@ do
end
buf_state.pending_change = nil
buf_state.last_flush = uv.hrtime()
if client.is_stopped() or not vim.api.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
@@ -522,7 +522,7 @@ do
if debounce == 0 then
buf_state.pending_change()
else
local timer = vim.loop.new_timer()
local timer = uv.new_timer()
buf_state.timer = timer
-- Must use schedule_wrap because `full_changes()` calls nvim_buf_get_lines
timer:start(debounce, 0, vim.schedule_wrap(buf_state.pending_change))
@@ -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 vim.api.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 vim.api.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
@@ -1433,7 +1433,7 @@ function lsp.buf_attach_client(bufnr, client_id)
client_id = { client_id, 'n' },
})
bufnr = resolve_bufnr(bufnr)
if not vim.api.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
]=]
vim.api.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.
vim.api.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 } }
@@ -1723,7 +1723,7 @@ function lsp.buf_request(bufnr, method, params, handler)
not tbl_isempty(all_buffer_active_clients[resolve_bufnr(bufnr)] or {}) and not method_supported
then
vim.notify(lsp._unsupported_method(method), vim.log.levels.ERROR)
vim.api.nvim_command('redraw')
nvim_command('redraw')
return {}, function() end
end
@@ -1888,8 +1888,8 @@ function lsp.omnifunc(findstart, base)
-- Then, perform standard completion request
local _ = log.info() and log.info('base ', base)
local pos = vim.api.nvim_win_get_cursor(0)
local line = vim.api.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)

View File

@@ -1,31 +1,11 @@
local vim = vim
local api = vim.api
local validate = vim.validate
local vfn = vim.fn
local util = require('vim.lsp.util')
local npcall = vim.F.npcall
local M = {}
---@private
--- Returns nil if {status} is false or nil, otherwise returns the rest of the
--- arguments.
local function ok_or_nil(status, ...)
if not status then
return
end
return ...
end
---@private
--- Swallows errors.
---
---@param fn Function to run
---@param ... Function arguments
---@returns Result of `fn(...)` if there are no errors, otherwise nil.
--- Returns nil if errors occur during {fn}, otherwise returns
local function npcall(fn, ...)
return ok_or_nil(pcall(fn, ...))
end
---@private
--- Sends an async request to all active clients attached to the current
--- buffer.
@@ -201,7 +181,7 @@ end
function M.format(options)
options = options or {}
local bufnr = options.bufnr or vim.api.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,
@@ -262,7 +242,7 @@ function M.formatting(options)
vim.log.levels.WARN
)
local params = util.make_formatting_params(options)
local bufnr = vim.api.nvim_get_current_buf()
local bufnr = api.nvim_get_current_buf()
select_client('textDocument/formatting', function(client)
if client == nil then
return
@@ -290,7 +270,7 @@ function M.formatting_sync(options, timeout_ms)
vim.log.levels.WARN
)
local params = util.make_formatting_params(options)
local bufnr = vim.api.nvim_get_current_buf()
local bufnr = api.nvim_get_current_buf()
select_client('textDocument/formatting', function(client)
if client == nil then
return
@@ -327,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 = vim.api.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
@@ -348,7 +328,7 @@ function M.formatting_seq_sync(options, timeout_ms, order)
'textDocument/formatting',
params,
timeout_ms,
vim.api.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)
@@ -394,7 +374,7 @@ end
--- this field.
function M.rename(new_name, options)
options = options or {}
local bufnr = options.bufnr or vim.api.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,
@@ -412,14 +392,14 @@ function M.rename(new_name, options)
vim.notify('[LSP] Rename, no matching language servers with rename capability.')
end
local win = vim.api.nvim_get_current_win()
local win = api.nvim_get_current_win()
-- Compute early to account for cursor movements after going async
local cword = vfn.expand('<cword>')
local cword = vim.fn.expand('<cword>')
---@private
local function get_text_at_range(range, offset_encoding)
return vim.api.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),
@@ -603,8 +583,8 @@ end
--- not provided, the user will be prompted for a path using |input()|.
function M.add_workspace_folder(workspace_folder)
workspace_folder = workspace_folder
or npcall(vfn.input, 'Workspace Folder: ', vfn.expand('%:p:h'), 'dir')
vim.api.nvim_command('redraw')
or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'), 'dir')
api.nvim_command('redraw')
if not (workspace_folder and #workspace_folder > 0) then
return
end
@@ -640,8 +620,8 @@ end
--- a path using |input()|.
function M.remove_workspace_folder(workspace_folder)
workspace_folder = workspace_folder
or npcall(vfn.input, 'Workspace Folder: ', vfn.expand('%:p:h'))
vim.api.nvim_command('redraw')
or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'))
api.nvim_command('redraw')
if not (workspace_folder and #workspace_folder > 0) then
return
end
@@ -669,7 +649,7 @@ end
---
---@param query (string, optional)
function M.workspace_symbol(query)
query = query or npcall(vfn.input, 'Query: ')
query = query or npcall(vim.fn.input, 'Query: ')
if query == nil then
return
end
@@ -838,7 +818,7 @@ end
--- with all aggregated results
---@private
local function code_action_request(params, options)
local bufnr = vim.api.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 }
@@ -875,7 +855,7 @@ function M.code_action(options)
end
local context = options.context or {}
if not context.diagnostics then
local bufnr = vim.api.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()
@@ -902,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 = vim.api.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)

View File

@@ -61,7 +61,7 @@ local function progress_handler(_, result, ctx, _)
client.messages.progress[token].done = true
end
vim.api.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

View File

@@ -11,7 +11,7 @@ local is_win = uv.os_uname().version:find('Windows')
---@param filename (string) path to check
---@returns (bool)
local function is_dir(filename)
local stat = vim.loop.fs_stat(filename)
local stat = uv.fs_stat(filename)
return stat and stat.type == 'directory' or false
end

View File

@@ -1,5 +1,5 @@
local lsp = vim.lsp
local util = vim.lsp.util
local util = lsp.util
---@private
local function mk_tag_item(name, range, uri, offset_encoding)
@@ -15,7 +15,7 @@ end
---@private
local function query_definition(pattern)
local params = lsp.util.make_position_params()
local params = util.make_position_params()
local results_by_client, err = lsp.buf_request_sync(0, 'textDocument/definition', params, 1000)
if err then
return {}

View File

@@ -238,14 +238,14 @@ local function get_lines(bufnr, rows)
-- This is needed for bufload and bufloaded
if bufnr == 0 then
bufnr = vim.api.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] = (vim.api.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
@@ -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 = vim.api.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 = vim.api.nvim_win_get_cursor(0)
local cursor = api.nvim_win_get_cursor(0)
return {
row = cursor[1] - 1,
col = cursor[2],
@@ -455,11 +455,11 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
start_col = get_line_byte_from_position(bufnr, text_edit.range.start, offset_encoding),
end_row = text_edit.range['end'].line,
end_col = get_line_byte_from_position(bufnr, text_edit.range['end'], offset_encoding),
text = vim.split(text_edit.newText, '\n', true),
text = split(text_edit.newText, '\n', true),
}
-- 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 = vim.api.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
vim.api.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 = vim.api.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
vim.api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col })
api.nvim_win_set_cursor(0, { cursor.row + 1, cursor.col })
end
end
@@ -511,7 +511,7 @@ function M.apply_text_edits(text_edits, bufnr, offset_encoding)
)
fix_eol = fix_eol and get_line(bufnr, max - 1) == ''
if fix_eol then
vim.api.nvim_buf_set_lines(bufnr, -2, -1, false, {})
api.nvim_buf_set_lines(bufnr, -2, -1, false, {})
end
end
@@ -724,7 +724,7 @@ end
-- ignoreIfExists? bool
function M.rename(old_fname, new_fname, opts)
opts = opts or {}
local target_exists = vim.loop.fs_stat(new_fname) ~= nil
local target_exists = uv.fs_stat(new_fname) ~= nil
if target_exists and not opts.overwrite or opts.ignoreIfExists then
vim.notify('Rename target already exists. Skipping rename.')
return
@@ -764,7 +764,7 @@ end
local function delete_file(change)
local opts = change.options or {}
local fname = vim.uri_to_fname(change.uri)
local stat = vim.loop.fs_stat(fname)
local stat = uv.fs_stat(fname)
if opts.ignoreIfNotExists and not stat then
return
end
@@ -906,7 +906,7 @@ function M.convert_signature_help_to_markdown_lines(signature_help, ft, triggers
-- wrap inside a code block so stylize_markdown can render it properly
label = ('```%s\n%s\n```'):format(ft, label)
end
vim.list_extend(contents, vim.split(label, '\n', true))
list_extend(contents, split(label, '\n', true))
if signature.documentation then
M.convert_input_to_markdown_lines(signature.documentation, contents)
end
@@ -1290,7 +1290,7 @@ function M.stylize_markdown(bufnr, contents, opts)
end
end
vim.api.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(vim.api.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
@@ -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 = vim.api.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,
@@ -1788,7 +1788,7 @@ function M.trim_empty_lines(lines)
break
end
end
return vim.list_extend({}, lines, start, finish)
return list_extend({}, lines, start, finish)
end
--- Accepts markdown lines and tries to reduce them to a filetype if they
@@ -1824,7 +1824,7 @@ 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 = vim.api.nvim_win_get_buf(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
@@ -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 = vim.api.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 = vim.api.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,7 +1924,7 @@ 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 vim.api.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 api.nvim_buf_get_mark(bufnr, '<'))
local B = list_extend({}, end_pos or api.nvim_buf_get_mark(bufnr, '>'))