| local api = vim.api |
| local validate = vim.validate |
|
|
| local lsp = vim._defer_require('vim.lsp', { |
| _changetracking = ..., |
| _folding_range = ..., |
| _snippet_grammar = ..., |
| _tagfunc = ..., |
| _watchfiles = ..., |
| buf = ..., |
| client = ..., |
| codelens = ..., |
| completion = ..., |
| diagnostic = ..., |
| handlers = ..., |
| inlay_hint = ..., |
| log = ..., |
| protocol = ..., |
| rpc = ..., |
| semantic_tokens = ..., |
| util = ..., |
| }) |
|
|
| local log = lsp.log |
| local protocol = lsp.protocol |
| local ms = protocol.Methods |
| local util = lsp.util |
| local changetracking = lsp._changetracking |
|
|
| |
| |
| lsp.rpc_response_error = lsp.rpc.rpc_response_error |
|
|
| lsp._resolve_to_request = { |
| [ms.codeAction_resolve] = ms.textDocument_codeAction, |
| [ms.codeLens_resolve] = ms.textDocument_codeLens, |
| [ms.documentLink_resolve] = ms.textDocument_documentLink, |
| [ms.inlayHint_resolve] = ms.textDocument_inlayHint, |
| } |
|
|
| |
|
|
| |
| |
| |
| |
| function lsp._unsupported_method(method) |
| local msg = string.format( |
| 'method %s is not supported by any of the servers registered for the current buffer', |
| method |
| ) |
| log.warn(msg) |
| return msg |
| end |
|
|
| |
| |
| |
| function lsp._get_workspace_folders(workspace_folders) |
| if type(workspace_folders) == 'table' then |
| return workspace_folders |
| elseif type(workspace_folders) == 'string' then |
| return { |
| { |
| uri = vim.uri_from_fname(workspace_folders), |
| name = workspace_folders, |
| }, |
| } |
| end |
| end |
|
|
| local wait_result_reason = { [-1] = 'timeout', [-2] = 'interrupted', [-3] = 'error' } |
|
|
| local format_line_ending = { |
| ['unix'] = '\n', |
| ['dos'] = '\r\n', |
| ['mac'] = '\r', |
| } |
|
|
| |
| |
| |
| function lsp._buf_get_line_ending(bufnr) |
| return format_line_ending[vim.bo[bufnr].fileformat] or '\n' |
| end |
|
|
| |
| local all_clients = {} |
|
|
| local client_errors_base = table.maxn(lsp.rpc.client_errors) |
| local client_errors_offset = 0 |
|
|
| local function client_error(name) |
| client_errors_offset = client_errors_offset + 1 |
| local index = client_errors_base + client_errors_offset |
| return { [name] = index, [index] = name } |
| end |
|
|
| |
| |
| |
| |
| lsp.client_errors = vim.tbl_extend( |
| 'error', |
| lsp.rpc.client_errors, |
| client_error('BEFORE_INIT_CALLBACK_ERROR'), |
| client_error('ON_INIT_CALLBACK_ERROR'), |
| client_error('ON_ATTACH_ERROR'), |
| client_error('ON_EXIT_CALLBACK_ERROR') |
| ) |
|
|
| |
| |
| |
| |
| |
| function lsp._buf_get_full_text(bufnr) |
| local line_ending = lsp._buf_get_line_ending(bufnr) |
| local text = table.concat(api.nvim_buf_get_lines(bufnr, 0, -1, true), line_ending) |
| if vim.bo[bufnr].eol then |
| text = text .. line_ending |
| end |
| return text |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| local function once(fn) |
| local value |
| local ran = false |
| return function(...) |
| if not ran then |
| value = fn(...) |
| ran = true |
| end |
| return value |
| end |
| end |
|
|
| |
| |
| |
| local function reuse_client_default(client, config) |
| if client.name ~= config.name or client:is_stopped() then |
| return false |
| end |
|
|
| local config_folders = lsp._get_workspace_folders(config.workspace_folders or config.root_dir) |
|
|
| if not config_folders or not next(config_folders) then |
| |
| local client_config_folders = |
| lsp._get_workspace_folders(client.config.workspace_folders or client.config.root_dir) |
| return not client_config_folders or not next(client_config_folders) |
| end |
|
|
| for _, config_folder in ipairs(config_folders) do |
| local found = false |
| for _, client_folder in ipairs(client.workspace_folders or {}) do |
| if config_folder.uri == client_folder.uri then |
| found = true |
| break |
| end |
| end |
| if not found then |
| return false |
| end |
| end |
|
|
| return true |
| end |
|
|
| |
| |
| local function reset_defaults(bufnr) |
| if vim.bo[bufnr].tagfunc == 'v:lua.vim.lsp.tagfunc' then |
| vim.bo[bufnr].tagfunc = nil |
| end |
| if vim.bo[bufnr].omnifunc == 'v:lua.vim.lsp.omnifunc' then |
| vim.bo[bufnr].omnifunc = nil |
| end |
| if vim.bo[bufnr].formatexpr == 'v:lua.vim.lsp.formatexpr()' then |
| vim.bo[bufnr].formatexpr = nil |
| end |
| vim._with({ buf = bufnr }, function() |
| local keymap = vim.fn.maparg('K', 'n', false, true) |
| if keymap and keymap.callback == vim.lsp.buf.hover and keymap.buffer == 1 then |
| vim.keymap.del('n', 'K', { buffer = bufnr }) |
| end |
| end) |
| end |
|
|
| |
| |
| |
| local function on_client_exit(code, signal, client_id) |
| local client = all_clients[client_id] |
|
|
| vim.schedule(function() |
| for bufnr in pairs(client.attached_buffers) do |
| if client and client.attached_buffers[bufnr] and api.nvim_buf_is_valid(bufnr) then |
| api.nvim_exec_autocmds('LspDetach', { |
| buffer = bufnr, |
| modeline = false, |
| data = { client_id = client_id }, |
| }) |
| end |
|
|
| client.attached_buffers[bufnr] = nil |
|
|
| if #lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) == 0 then |
| reset_defaults(bufnr) |
| end |
| end |
|
|
| local namespace = vim.lsp.diagnostic.get_namespace(client_id) |
| vim.diagnostic.reset(namespace) |
| end) |
|
|
| local name = client.name or 'unknown' |
|
|
| |
| |
| vim.schedule(function() |
| all_clients[client_id] = nil |
|
|
| |
| |
| if client then |
| changetracking.reset(client) |
| end |
| if code ~= 0 or (signal ~= 0 and signal ~= 15) then |
| local msg = string.format( |
| 'Client %s quit with exit code %s and signal %s. Check log for errors: %s', |
| name, |
| code, |
| signal, |
| lsp.get_log_path() |
| ) |
| vim.notify(msg, vim.log.levels.WARN) |
| end |
| end) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| local function create_and_init_client(config) |
| local ok, res = pcall(require('vim.lsp.client').create, config) |
| if not ok then |
| return nil, res |
| end |
|
|
| local client = assert(res) |
|
|
| |
| table.insert(client._on_exit_cbs, on_client_exit) |
|
|
| all_clients[client.id] = client |
|
|
| client:initialize() |
|
|
| return client.id, nil |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.config(name, cfg) |
| local _, _ = name, cfg |
| |
| end |
|
|
| lsp._enabled_configs = {} |
|
|
| |
| |
| local function invalidate_enabled_config(name) |
| if name == '*' then |
| for _, v in pairs(lsp._enabled_configs) do |
| v.resolved_config = nil |
| end |
| elseif lsp._enabled_configs[name] then |
| lsp._enabled_configs[name].resolved_config = nil |
| end |
| end |
|
|
| |
| local function validate_config_name(name) |
| validate('name', name, function(value) |
| if type(value) ~= 'string' then |
| return false |
| end |
| if value ~= '*' and value:match('%*') then |
| return false, 'LSP config name cannot contain wildcard ("*")' |
| end |
| return true |
| end, 'non-wildcard string') |
| end |
|
|
| |
| |
| |
| |
| lsp.config = setmetatable({ _configs = {} }, { |
| |
| |
| |
| __index = function(self, name) |
| validate_config_name(name) |
|
|
| local rconfig = lsp._enabled_configs[name] or {} |
|
|
| if not rconfig.resolved_config then |
| if name == '*' then |
| rconfig.resolved_config = lsp.config._configs['*'] or {} |
| return rconfig.resolved_config |
| end |
|
|
| |
| |
| local rtp_config |
| for _, v in ipairs(api.nvim_get_runtime_file(('lsp/%s.lua'):format(name), true)) do |
| local config = assert(loadfile(v))() |
| if type(config) == 'table' then |
| |
| rtp_config = vim.tbl_deep_extend('force', rtp_config or {}, config) |
| else |
| log.warn(('%s does not return a table, ignoring'):format(v)) |
| end |
| end |
|
|
| if not rtp_config and not self._configs[name] then |
| log.warn(('%s does not have a configuration'):format(name)) |
| return |
| end |
|
|
| rconfig.resolved_config = vim.tbl_deep_extend( |
| 'force', |
| lsp.config._configs['*'] or {}, |
| rtp_config or {}, |
| self._configs[name] or {} |
| ) |
| rconfig.resolved_config.name = name |
| end |
|
|
| return rconfig.resolved_config |
| end, |
|
|
| |
| |
| |
| __newindex = function(self, name, cfg) |
| validate_config_name(name) |
| local msg = ('table (hint: to resolve a config, use vim.lsp.config["%s"])'):format(name) |
| validate('cfg', cfg, 'table', msg) |
| invalidate_enabled_config(name) |
| self._configs[name] = cfg |
| end, |
|
|
| |
| |
| |
| __call = function(self, name, cfg) |
| validate_config_name(name) |
| local msg = ('table (hint: to resolve a config, use vim.lsp.config["%s"])'):format(name) |
| validate('cfg', cfg, 'table', msg) |
| invalidate_enabled_config(name) |
| self[name] = vim.tbl_deep_extend('force', self._configs[name] or {}, cfg) |
| end, |
| }) |
|
|
| local lsp_enable_autocmd_id |
|
|
| local function validate_cmd(v) |
| if type(v) == 'table' then |
| if vim.fn.executable(v[1]) == 0 then |
| return false, v[1] .. ' is not executable' |
| end |
| return true |
| end |
| return type(v) == 'function' |
| end |
|
|
| |
| local function validate_config(config) |
| validate('cmd', config.cmd, validate_cmd, 'expected function or table with executable command') |
| validate('reuse_client', config.reuse_client, 'function', true) |
| validate('filetypes', config.filetypes, 'table', true) |
| end |
|
|
| |
| |
| |
| local function can_start(bufnr, name, config) |
| local config_ok, err = pcall(validate_config, config) |
| if not config_ok then |
| log.error(('cannot start %s due to config error: %s'):format(name, err)) |
| return false |
| end |
|
|
| if config.filetypes and not vim.tbl_contains(config.filetypes, vim.bo[bufnr].filetype) then |
| return false |
| end |
|
|
| return true |
| end |
|
|
| |
| |
| local function start_config(bufnr, config) |
| return vim.lsp.start(config, { |
| bufnr = bufnr, |
| reuse_client = config.reuse_client, |
| _root_markers = config.root_markers, |
| }) |
| end |
|
|
| |
| local function lsp_enable_callback(bufnr) |
| |
| if vim.bo[bufnr].buftype ~= '' then |
| return |
| end |
|
|
| |
| local clients = lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) |
| for _, client in ipairs(clients) do |
| if |
| lsp.is_enabled(client.name) and not can_start(bufnr, client.name, lsp.config[client.name]) |
| then |
| lsp.buf_detach_client(bufnr, client.id) |
| end |
| end |
|
|
| |
| for name in vim.spairs(lsp._enabled_configs) do |
| local config = lsp.config[name] |
| if config and can_start(bufnr, name, config) then |
| |
| |
| config = vim.deepcopy(config) |
|
|
| if type(config.root_dir) == 'function' then |
| |
| config.root_dir(bufnr, function(root_dir) |
| config.root_dir = root_dir |
| vim.schedule(function() |
| start_config(bufnr, config) |
| end) |
| end) |
| else |
| start_config(bufnr, config) |
| end |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.enable(name, enable) |
| validate('name', name, { 'string', 'table' }) |
|
|
| local names = vim._ensure_list(name) |
| for _, nm in ipairs(names) do |
| if nm == '*' then |
| error('Invalid name') |
| end |
| lsp._enabled_configs[nm] = enable ~= false and {} or nil |
| end |
|
|
| if not next(lsp._enabled_configs) then |
| |
| if lsp_enable_autocmd_id then |
| api.nvim_del_autocmd(lsp_enable_autocmd_id) |
| lsp_enable_autocmd_id = nil |
| end |
| else |
| |
| lsp_enable_autocmd_id = lsp_enable_autocmd_id |
| or api.nvim_create_autocmd('FileType', { |
| group = api.nvim_create_augroup('nvim.lsp.enable', {}), |
| callback = function(args) |
| lsp_enable_callback(args.buf) |
| end, |
| }) |
| end |
|
|
| |
| if enable ~= false then |
| if vim.v.vim_did_enter == 1 then |
| vim.cmd.doautoall('nvim.lsp.enable FileType') |
| end |
| else |
| for _, nm in ipairs(names) do |
| for _, client in ipairs(lsp.get_clients({ name = nm })) do |
| client:stop() |
| end |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function lsp.is_enabled(name) |
| return lsp._enabled_configs[name] ~= nil |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.start(config, opts) |
| opts = opts or {} |
| local reuse_client = opts.reuse_client or reuse_client_default |
| local bufnr = vim._resolve_bufnr(opts.bufnr) |
|
|
| if not config.root_dir and opts._root_markers then |
| validate('root_markers', opts._root_markers, 'table') |
| config = vim.deepcopy(config) |
|
|
| config.root_dir = vim.fs.root(bufnr, opts._root_markers) |
| end |
|
|
| if |
| not config.root_dir |
| and (not config.workspace_folders or #config.workspace_folders == 0) |
| and config.workspace_required |
| then |
| log.info( |
| ('skipping config "%s": workspace_required=true, no workspace found'):format(config.name) |
| ) |
| return |
| end |
|
|
| for _, client in pairs(all_clients) do |
| if reuse_client(client, config) then |
| if opts.attach == false then |
| return client.id |
| end |
|
|
| if lsp.buf_attach_client(bufnr, client.id) then |
| return client.id |
| end |
| return |
| end |
| end |
|
|
| local client_id, err = create_and_init_client(config) |
| if err then |
| if not opts.silent then |
| vim.notify(err, vim.log.levels.WARN) |
| end |
| return |
| end |
|
|
| if opts.attach == false then |
| return client_id |
| end |
|
|
| if client_id and lsp.buf_attach_client(bufnr, client_id) then |
| return client_id |
| end |
| end |
|
|
| |
| |
| |
| |
| function lsp.status() |
| local percentage = nil |
| local messages = {} |
| for _, client in ipairs(vim.lsp.get_clients()) do |
| |
| for progress in client.progress do |
| |
| local value = progress.value |
| if type(value) == 'table' and value.kind then |
| local message = value.message and (value.title .. ': ' .. value.message) or value.title |
| messages[#messages + 1] = message |
| if value.percentage then |
| percentage = math.max(percentage or 0, value.percentage) |
| end |
| end |
| |
| |
| end |
| end |
| local message = table.concat(messages, ', ') |
| if percentage then |
| return string.format('%3d%%: %s', percentage, message) |
| end |
| return message |
| end |
|
|
| |
| |
| |
| |
| local function is_empty_or_default(bufnr, option) |
| if vim.bo[bufnr][option] == '' then |
| return true |
| end |
|
|
| local info = api.nvim_get_option_info2(option, { buf = bufnr }) |
| |
| local scriptinfo = vim.tbl_filter(function(e) |
| return e.sid == info.last_set_sid |
| end, vim.fn.getscriptinfo()) |
|
|
| if #scriptinfo ~= 1 then |
| return false |
| end |
|
|
| return vim.startswith(scriptinfo[1].name, vim.fn.expand('$VIMRUNTIME')) |
| end |
|
|
| |
| |
| |
| function lsp._set_defaults(client, bufnr) |
| if |
| client:supports_method(ms.textDocument_definition) and is_empty_or_default(bufnr, 'tagfunc') |
| then |
| vim.bo[bufnr].tagfunc = 'v:lua.vim.lsp.tagfunc' |
| end |
| if |
| client:supports_method(ms.textDocument_completion) and is_empty_or_default(bufnr, 'omnifunc') |
| then |
| vim.bo[bufnr].omnifunc = 'v:lua.vim.lsp.omnifunc' |
| end |
| if |
| client:supports_method(ms.textDocument_rangeFormatting) |
| and is_empty_or_default(bufnr, 'formatprg') |
| and is_empty_or_default(bufnr, 'formatexpr') |
| then |
| vim.bo[bufnr].formatexpr = 'v:lua.vim.lsp.formatexpr()' |
| end |
| vim._with({ buf = bufnr }, function() |
| if |
| client:supports_method(ms.textDocument_hover) |
| and is_empty_or_default(bufnr, 'keywordprg') |
| and vim.fn.maparg('K', 'n', false, false) == '' |
| then |
| vim.keymap.set('n', 'K', function() |
| vim.lsp.buf.hover() |
| end, { buffer = bufnr, desc = 'vim.lsp.buf.hover()' }) |
| end |
| end) |
| if client:supports_method(ms.textDocument_diagnostic) then |
| lsp.diagnostic._enable(bufnr) |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| function lsp.start_client(config) |
| vim.deprecate('vim.lsp.start_client()', 'vim.lsp.start()', '0.13') |
| return create_and_init_client(config) |
| end |
|
|
| |
| |
| local function text_document_did_save_handler(bufnr) |
| bufnr = vim._resolve_bufnr(bufnr) |
| local uri = vim.uri_from_bufnr(bufnr) |
| local text = once(lsp._buf_get_full_text) |
| for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do |
| local name = api.nvim_buf_get_name(bufnr) |
| local old_name = changetracking._get_and_set_name(client, bufnr, name) |
| if old_name and name ~= old_name then |
| client:notify(ms.textDocument_didClose, { |
| textDocument = { |
| uri = vim.uri_from_fname(old_name), |
| }, |
| }) |
| client:notify(ms.textDocument_didOpen, { |
| textDocument = { |
| version = 0, |
| uri = uri, |
| languageId = client.get_language_id(bufnr, vim.bo[bufnr].filetype), |
| text = lsp._buf_get_full_text(bufnr), |
| }, |
| }) |
| util.buf_versions[bufnr] = 0 |
| end |
| local save_capability = vim.tbl_get(client.server_capabilities, 'textDocumentSync', 'save') |
| if save_capability then |
| local included_text |
| if type(save_capability) == 'table' and save_capability.includeText then |
| included_text = text(bufnr) |
| end |
| client:notify(ms.textDocument_didSave, { |
| textDocument = { |
| uri = uri, |
| }, |
| text = included_text, |
| }) |
| end |
| end |
| end |
|
|
| |
| |
| local function buf_detach_client(bufnr, client) |
| api.nvim_exec_autocmds('LspDetach', { |
| buffer = bufnr, |
| modeline = false, |
| data = { client_id = client.id }, |
| }) |
|
|
| changetracking.reset_buf(client, bufnr) |
|
|
| if client:supports_method(ms.textDocument_didClose) then |
| local uri = vim.uri_from_bufnr(bufnr) |
| local params = { textDocument = { uri = uri } } |
| client:notify(ms.textDocument_didClose, params) |
| end |
|
|
| client.attached_buffers[bufnr] = nil |
|
|
| local namespace = lsp.diagnostic.get_namespace(client.id) |
| vim.diagnostic.reset(namespace, bufnr) |
| end |
|
|
| |
| local attached_buffers = {} |
|
|
| |
| local function buf_attach(bufnr) |
| if attached_buffers[bufnr] then |
| return |
| end |
| attached_buffers[bufnr] = true |
|
|
| local uri = vim.uri_from_bufnr(bufnr) |
| local augroup = ('nvim.lsp.b_%d_save'):format(bufnr) |
| local group = api.nvim_create_augroup(augroup, { clear = true }) |
| api.nvim_create_autocmd('BufWritePre', { |
| group = group, |
| buffer = bufnr, |
| desc = 'vim.lsp: textDocument/willSave', |
| callback = function(ctx) |
| for _, client in ipairs(lsp.get_clients({ bufnr = ctx.buf })) do |
| local params = { |
| textDocument = { |
| uri = uri, |
| }, |
| reason = protocol.TextDocumentSaveReason.Manual, |
| } |
| if client:supports_method(ms.textDocument_willSave) then |
| client:notify(ms.textDocument_willSave, params) |
| end |
| if client:supports_method(ms.textDocument_willSaveWaitUntil) then |
| local result, err = |
| client:request_sync(ms.textDocument_willSaveWaitUntil, params, 1000, ctx.buf) |
| if result and result.result then |
| util.apply_text_edits(result.result, ctx.buf, client.offset_encoding) |
| elseif err then |
| log.error(vim.inspect(err)) |
| end |
| end |
| end |
| end, |
| }) |
| api.nvim_create_autocmd('BufWritePost', { |
| group = group, |
| buffer = bufnr, |
| desc = 'vim.lsp: textDocument/didSave handler', |
| callback = function(ctx) |
| text_document_did_save_handler(ctx.buf) |
| end, |
| }) |
| |
| api.nvim_buf_attach(bufnr, false, { |
| on_lines = function(_, _, changedtick, firstline, lastline, new_lastline) |
| if #lsp.get_clients({ bufnr = bufnr }) == 0 then |
| |
| return #lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) == 0 |
| end |
| util.buf_versions[bufnr] = changedtick |
| changetracking.send_changes(bufnr, firstline, lastline, new_lastline) |
| end, |
|
|
| on_reload = function() |
| local clients = lsp.get_clients({ bufnr = bufnr }) |
| local params = { textDocument = { uri = uri } } |
| for _, client in ipairs(clients) do |
| changetracking.reset_buf(client, bufnr) |
| if client:supports_method(ms.textDocument_didClose) then |
| client:notify(ms.textDocument_didClose, params) |
| end |
| end |
| for _, client in ipairs(clients) do |
| client:_text_document_did_open_handler(bufnr) |
| end |
| end, |
|
|
| on_detach = function() |
| local clients = lsp.get_clients({ bufnr = bufnr, _uninitialized = true }) |
| for _, client in ipairs(clients) do |
| buf_detach_client(bufnr, client) |
| end |
| attached_buffers[bufnr] = nil |
| util.buf_versions[bufnr] = nil |
| end, |
|
|
| |
| |
| |
| utf_sizes = true, |
| }) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.buf_attach_client(bufnr, client_id) |
| validate('bufnr', bufnr, 'number', true) |
| validate('client_id', client_id, 'number') |
| bufnr = vim._resolve_bufnr(bufnr) |
| if not api.nvim_buf_is_loaded(bufnr) then |
| log.warn(string.format('buf_attach_client called on unloaded buffer (id: %d): ', bufnr)) |
| return false |
| end |
|
|
| local client = lsp.get_client_by_id(client_id) |
| if not client then |
| return false |
| end |
|
|
| buf_attach(bufnr) |
|
|
| if client.attached_buffers[bufnr] then |
| return true |
| end |
|
|
| client.attached_buffers[bufnr] = true |
|
|
| |
| |
| |
| if client.initialized then |
| client:on_attach(bufnr) |
| end |
| return true |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function lsp.buf_detach_client(bufnr, client_id) |
| validate('bufnr', bufnr, 'number', true) |
| validate('client_id', client_id, 'number') |
| bufnr = vim._resolve_bufnr(bufnr) |
|
|
| local client = all_clients[client_id] |
| if not client or not client.attached_buffers[bufnr] then |
| vim.notify( |
| string.format( |
| 'Buffer (id: %d) is not attached to client (id: %d). Cannot detach.', |
| bufnr, |
| client_id |
| ) |
| ) |
| return |
| else |
| buf_detach_client(bufnr, client) |
| end |
| end |
|
|
| |
| |
| |
| |
| function lsp.buf_is_attached(bufnr, client_id) |
| return lsp.get_clients({ bufnr = bufnr, id = client_id, _uninitialized = true })[1] ~= nil |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function lsp.get_client_by_id(client_id) |
| return all_clients[client_id] |
| end |
|
|
| |
| |
| |
| |
| function lsp.get_buffers_by_client_id(client_id) |
| local client = all_clients[client_id] |
| return client and vim.tbl_keys(client.attached_buffers) or {} |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.stop_client(client_id, force) |
| |
| local ids = type(client_id) == 'table' and client_id or { client_id } |
| for _, id in ipairs(ids) do |
| if type(id) == 'table' then |
| if id.stop then |
| id:stop(force) |
| end |
| else |
| |
| local client = all_clients[id] |
| if client then |
| client:stop(force) |
| end |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| function lsp.get_clients(filter) |
| validate('filter', filter, 'table', true) |
|
|
| filter = filter or {} |
|
|
| local clients = {} |
|
|
| local bufnr = filter.bufnr and vim._resolve_bufnr(filter.bufnr) |
|
|
| for _, client in pairs(all_clients) do |
| if |
| client |
| and (filter.id == nil or client.id == filter.id) |
| and (filter.bufnr == nil or client.attached_buffers[bufnr]) |
| and (filter.name == nil or client.name == filter.name) |
| and (filter.method == nil or client:supports_method(filter.method, filter.bufnr)) |
| and (filter._uninitialized or client.initialized) |
| then |
| clients[#clients + 1] = client |
| end |
| end |
| return clients |
| end |
|
|
| |
| |
| function lsp.get_active_clients(filter) |
| vim.deprecate('vim.lsp.get_active_clients()', 'vim.lsp.get_clients()', '0.12') |
| return lsp.get_clients(filter) |
| end |
|
|
| api.nvim_create_autocmd('VimLeavePre', { |
| desc = 'vim.lsp: exit handler', |
| callback = function() |
| local active_clients = lsp.get_clients() |
| log.info('exit_handler', active_clients) |
| for _, client in pairs(all_clients) do |
| client:stop() |
| end |
|
|
| local timeouts = {} |
| local max_timeout = 0 |
| local send_kill = false |
|
|
| for client_id, client in pairs(active_clients) do |
| local timeout = client.flags.exit_timeout |
| if timeout then |
| send_kill = true |
| timeouts[client_id] = timeout |
| max_timeout = math.max(timeout, max_timeout) |
| end |
| end |
|
|
| local poll_time = 50 |
|
|
| local function check_clients_closed() |
| for client_id, timeout in pairs(timeouts) do |
| timeouts[client_id] = timeout - poll_time |
| end |
|
|
| for client_id, _ in pairs(active_clients) do |
| if timeouts[client_id] ~= nil and timeouts[client_id] > 0 then |
| return false |
| end |
| end |
| return true |
| end |
|
|
| if send_kill then |
| if not vim.wait(max_timeout, check_clients_closed, poll_time) then |
| for client_id, client in pairs(active_clients) do |
| if timeouts[client_id] ~= nil then |
| client:stop(true) |
| end |
| end |
| end |
| end |
| end, |
| }) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.buf_request(bufnr, method, params, handler, on_unsupported) |
| validate('bufnr', bufnr, 'number', true) |
| validate('method', method, 'string') |
| validate('handler', handler, 'function', true) |
| validate('on_unsupported', on_unsupported, 'function', true) |
|
|
| bufnr = vim._resolve_bufnr(bufnr) |
| local method_supported = false |
| local clients = lsp.get_clients({ bufnr = bufnr }) |
| local client_request_ids = {} |
| for _, client in ipairs(clients) do |
| if client:supports_method(method, bufnr) then |
| method_supported = true |
|
|
| local cparams = type(params) == 'function' and params(client, bufnr) or params |
| local request_success, request_id = client:request(method, cparams, handler, bufnr) |
| |
| |
| if request_success then |
| client_request_ids[client.id] = request_id |
| end |
| end |
| end |
|
|
| |
| if next(clients) and not method_supported then |
| if on_unsupported == nil then |
| vim.notify(lsp._unsupported_method(method), vim.log.levels.ERROR) |
| else |
| on_unsupported() |
| end |
| vim.cmd.redraw() |
| return {}, function() end |
| end |
|
|
| local function _cancel_all_requests() |
| for client_id, request_id in pairs(client_request_ids) do |
| local client = all_clients[client_id] |
| if client.requests[request_id] then |
| client:cancel_request(request_id) |
| end |
| end |
| end |
|
|
| return client_request_ids, _cancel_all_requests |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.buf_request_all(bufnr, method, params, handler) |
| local results = {} |
| local remaining |
|
|
| local _, cancel = lsp.buf_request(bufnr, method, params, function(err, result, ctx, config) |
| if not remaining then |
| |
| remaining = #lsp.get_clients({ bufnr = bufnr, method = method }) |
| end |
|
|
| |
| results[ctx.client_id] = { err = err, error = err, result = result } |
| remaining = remaining - 1 |
|
|
| if remaining == 0 then |
| handler(results, ctx, config) |
| end |
| end) |
|
|
| return cancel |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.buf_request_sync(bufnr, method, params, timeout_ms) |
| local request_results |
|
|
| local cancel = lsp.buf_request_all(bufnr, method, params, function(it) |
| request_results = it |
| end) |
|
|
| local wait_result, reason = vim.wait(timeout_ms or 1000, function() |
| return request_results ~= nil |
| end, 10) |
|
|
| if not wait_result then |
| cancel() |
| return nil, wait_result_reason[reason] |
| end |
|
|
| return request_results |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function lsp.buf_notify(bufnr, method, params) |
| validate('bufnr', bufnr, 'number', true) |
| validate('method', method, 'string') |
| local resp = false |
| for _, client in ipairs(lsp.get_clients({ bufnr = bufnr })) do |
| if client.rpc.notify(method, params) then |
| resp = true |
| end |
| end |
| return resp |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.omnifunc(findstart, base) |
| return vim.lsp.completion._omnifunc(findstart, base) |
| end |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| function lsp.formatexpr(opts) |
| opts = opts or {} |
| local timeout_ms = opts.timeout_ms or 500 |
|
|
| if vim.list_contains({ 'i', 'R', 'ic', 'ix' }, vim.fn.mode()) then |
| |
| |
| return 1 |
| end |
|
|
| local start_lnum = vim.v.lnum |
| local end_lnum = start_lnum + vim.v.count - 1 |
|
|
| if start_lnum <= 0 or end_lnum <= 0 then |
| return 0 |
| end |
| local bufnr = api.nvim_get_current_buf() |
| for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do |
| if client:supports_method(ms.textDocument_rangeFormatting) then |
| local params = util.make_formatting_params() |
| local end_line = vim.fn.getline(end_lnum) |
| local end_col = vim.str_utfindex(end_line, client.offset_encoding) |
| |
| params.range = { |
| start = { |
| line = start_lnum - 1, |
| character = 0, |
| }, |
| ['end'] = { |
| line = end_lnum - 1, |
| character = end_col, |
| }, |
| } |
| local response = |
| client:request_sync(ms.textDocument_rangeFormatting, params, timeout_ms, bufnr) |
| if response and response.result then |
| lsp.util.apply_text_edits(response.result, bufnr, client.offset_encoding) |
| return 0 |
| end |
| end |
| end |
|
|
| |
| return 0 |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.tagfunc(pattern, flags) |
| return vim.lsp._tagfunc(pattern, flags) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.foldexpr(lnum) |
| return vim.lsp._folding_range.foldexpr(lnum) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.foldclose(kind, winid) |
| return vim.lsp._folding_range.foldclose(kind, winid) |
| end |
|
|
| |
| |
| function lsp.foldtext() |
| return vim.lsp._folding_range.foldtext() |
| end |
|
|
| |
| |
| |
| |
| function lsp.client_is_stopped(client_id) |
| assert(client_id, 'missing client_id param') |
| return not all_clients[client_id] |
| end |
|
|
| |
| |
| |
| |
| |
| |
| function lsp.buf_get_clients(bufnr) |
| vim.deprecate('vim.lsp.buf_get_clients()', 'vim.lsp.get_clients()', '0.12') |
| local result = {} |
| for _, client in ipairs(lsp.get_clients({ bufnr = vim._resolve_bufnr(bufnr) })) do |
| result[client.id] = client |
| end |
| return result |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| lsp.log_levels = log.levels |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.set_log_level(level) |
| if type(level) == 'string' or type(level) == 'number' then |
| log.set_level(level) |
| else |
| error(string.format('Invalid log level: %q', level)) |
| end |
| end |
|
|
| |
| |
| function lsp.get_log_path() |
| return log.get_filename() |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function lsp.for_each_buffer_client(bufnr, fn) |
| vim.deprecate( |
| 'vim.lsp.for_each_buffer_client()', |
| 'lsp.get_clients({ bufnr = bufnr }) with regular loop', |
| '0.12' |
| ) |
| bufnr = vim._resolve_bufnr(bufnr) |
|
|
| for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do |
| fn(client, client.id, bufnr) |
| end |
| end |
|
|
| |
| |
| |
| |
| function lsp.with(handler, override_config) |
| return function(err, result, ctx, config) |
| return handler(err, result, ctx, vim.tbl_deep_extend('force', config or {}, override_config)) |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| lsp.commands = setmetatable({}, { |
| __newindex = function(tbl, key, value) |
| assert(type(key) == 'string', 'The key for commands in `vim.lsp.commands` must be a string') |
| assert(type(value) == 'function', 'Command added to `vim.lsp.commands` must be a function') |
| rawset(tbl, key, value) |
| end, |
| }) |
|
|
| return lsp |
|
|