| |
| |
|
|
| local api = vim.api |
| local lsp = vim.lsp |
| local validate = vim.validate |
| local util = require('vim.lsp.util') |
| local npcall = vim.F.npcall |
| local ms = require('vim.lsp.protocol').Methods |
|
|
| local M = {} |
|
|
| |
| |
| local function client_positional_params(params) |
| local win = api.nvim_get_current_win() |
| return function(client) |
| local ret = util.make_position_params(win, client.offset_encoding) |
| if params then |
| ret = vim.tbl_extend('force', ret, params) |
| end |
| return ret |
| end |
| end |
|
|
| local hover_ns = api.nvim_create_namespace('nvim.lsp.hover_range') |
|
|
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.hover(config) |
| config = config or {} |
| config.focus_id = ms.textDocument_hover |
|
|
| lsp.buf_request_all(0, ms.textDocument_hover, client_positional_params(), function(results, ctx) |
| local bufnr = assert(ctx.bufnr) |
| if api.nvim_get_current_buf() ~= bufnr then |
| |
| return |
| end |
|
|
| |
| local results1 = {} |
|
|
| for client_id, resp in pairs(results) do |
| local err, result = resp.err, resp.result |
| if err then |
| lsp.log.error(err.code, err.message) |
| elseif result then |
| results1[client_id] = result |
| end |
| end |
|
|
| if vim.tbl_isempty(results1) then |
| if config.silent ~= true then |
| vim.notify('No information available', vim.log.levels.INFO) |
| end |
| return |
| end |
|
|
| local contents = {} |
|
|
| local nresults = #vim.tbl_keys(results1) |
|
|
| local format = 'markdown' |
|
|
| for client_id, result in pairs(results1) do |
| local client = assert(lsp.get_client_by_id(client_id)) |
| if nresults > 1 then |
| |
| contents[#contents + 1] = string.format('# %s', client.name) |
| end |
| if type(result.contents) == 'table' and result.contents.kind == 'plaintext' then |
| if #results1 == 1 then |
| format = 'plaintext' |
| contents = vim.split(result.contents.value or '', '\n', { trimempty = true }) |
| else |
| |
| contents[#contents + 1] = '```' |
| vim.list_extend( |
| contents, |
| vim.split(result.contents.value or '', '\n', { trimempty = true }) |
| ) |
| contents[#contents + 1] = '```' |
| end |
| else |
| vim.list_extend(contents, util.convert_input_to_markdown_lines(result.contents)) |
| end |
| local range = result.range |
| if range then |
| local start = range.start |
| local end_ = range['end'] |
| local start_idx = util._get_line_byte_from_position(bufnr, start, client.offset_encoding) |
| local end_idx = util._get_line_byte_from_position(bufnr, end_, client.offset_encoding) |
|
|
| vim.hl.range( |
| bufnr, |
| hover_ns, |
| 'LspReferenceTarget', |
| { start.line, start_idx }, |
| { end_.line, end_idx }, |
| { priority = vim.hl.priorities.user } |
| ) |
| end |
| contents[#contents + 1] = '---' |
| end |
|
|
| |
| contents[#contents] = nil |
|
|
| if vim.tbl_isempty(contents) then |
| if config.silent ~= true then |
| vim.notify('No information available') |
| end |
| return |
| end |
|
|
| local _, winid = lsp.util.open_floating_preview(contents, format, config) |
|
|
| api.nvim_create_autocmd('WinClosed', { |
| pattern = tostring(winid), |
| once = true, |
| callback = function() |
| api.nvim_buf_clear_namespace(bufnr, hover_ns, 0, -1) |
| return true |
| end, |
| }) |
| end) |
| end |
|
|
| local function request_with_opts(name, params, opts) |
| local req_handler |
| if opts then |
| req_handler = function(err, result, ctx, config) |
| local client = assert(lsp.get_client_by_id(ctx.client_id)) |
| local handler = client.handlers[name] or lsp.handlers[name] |
| handler(err, result, ctx, vim.tbl_extend('force', config or {}, opts)) |
| end |
| end |
| lsp.buf_request(0, name, params, req_handler) |
| end |
|
|
| |
| |
| local function get_locations(method, opts) |
| opts = opts or {} |
| local bufnr = api.nvim_get_current_buf() |
| local clients = lsp.get_clients({ method = method, bufnr = bufnr }) |
| if not next(clients) then |
| vim.notify(lsp._unsupported_method(method), vim.log.levels.WARN) |
| return |
| end |
| local win = api.nvim_get_current_win() |
| local from = vim.fn.getpos('.') |
| from[1] = bufnr |
| local tagname = vim.fn.expand('<cword>') |
| local remaining = #clients |
|
|
| |
| local all_items = {} |
|
|
| |
| |
| local function on_response(_, result, client) |
| local locations = {} |
| if result then |
| locations = vim.islist(result) and result or { result } |
| end |
| local items = util.locations_to_items(locations, client.offset_encoding) |
| vim.list_extend(all_items, items) |
| remaining = remaining - 1 |
| if remaining == 0 then |
| if vim.tbl_isempty(all_items) then |
| vim.notify('No locations found', vim.log.levels.INFO) |
| return |
| end |
|
|
| local title = 'LSP locations' |
| if opts.on_list then |
| assert(vim.is_callable(opts.on_list), 'on_list is not a function') |
| opts.on_list({ |
| title = title, |
| items = all_items, |
| context = { bufnr = bufnr, method = method }, |
| }) |
| return |
| end |
|
|
| if #all_items == 1 then |
| local item = all_items[1] |
| local b = item.bufnr or vim.fn.bufadd(item.filename) |
|
|
| |
| vim.cmd("normal! m'") |
| |
| local tagstack = { { tagname = tagname, from = from } } |
| vim.fn.settagstack(vim.fn.win_getid(win), { items = tagstack }, 't') |
|
|
| vim.bo[b].buflisted = true |
| local w = win |
| if opts.reuse_win then |
| w = vim.fn.win_findbuf(b)[1] or w |
| if w ~= win then |
| api.nvim_set_current_win(w) |
| end |
| end |
| api.nvim_win_set_buf(w, b) |
| api.nvim_win_set_cursor(w, { item.lnum, item.col - 1 }) |
| vim._with({ win = w }, function() |
| |
| vim.cmd('normal! zv') |
| end) |
| return |
| end |
| if opts.loclist then |
| vim.fn.setloclist(0, {}, ' ', { title = title, items = all_items }) |
| vim.cmd.lopen() |
| else |
| vim.fn.setqflist({}, ' ', { title = title, items = all_items }) |
| vim.cmd('botright copen') |
| end |
| end |
| end |
| for _, client in ipairs(clients) do |
| local params = util.make_position_params(win, client.offset_encoding) |
| client:request(method, params, function(_, result) |
| on_response(_, result, client) |
| end) |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| function M.declaration(opts) |
| get_locations(ms.textDocument_declaration, opts) |
| end |
|
|
| |
| |
| function M.definition(opts) |
| get_locations(ms.textDocument_definition, opts) |
| end |
|
|
| |
| |
| function M.type_definition(opts) |
| get_locations(ms.textDocument_typeDefinition, opts) |
| end |
|
|
| |
| |
| |
| function M.implementation(opts) |
| get_locations(ms.textDocument_implementation, opts) |
| end |
|
|
| |
| local function process_signature_help_results(results) |
| local signatures = {} |
| local active_signature = 1 |
|
|
| |
| for client_id, r in pairs(results) do |
| local err = r.err |
| local client = assert(lsp.get_client_by_id(client_id)) |
| if err then |
| vim.notify( |
| client.name .. ': ' .. tostring(err.code) .. ': ' .. err.message, |
| vim.log.levels.ERROR |
| ) |
| api.nvim_command('redraw') |
| else |
| local result = r.result |
| if result and result.signatures and result.signatures[1] then |
| for i, sig in ipairs(result.signatures) do |
| sig.activeParameter = sig.activeParameter or result.activeParameter |
| local idx = #signatures + 1 |
| if (result.activeSignature or 0) + 1 == i then |
| active_signature = idx |
| end |
| signatures[idx] = { client, sig } |
| end |
| end |
| end |
| end |
|
|
| return signatures, active_signature |
| end |
|
|
| local sig_help_ns = api.nvim_create_namespace('nvim.lsp.signature_help') |
|
|
| |
| |
|
|
| |
| |
| |
| function M.signature_help(config) |
| local method = ms.textDocument_signatureHelp |
|
|
| config = config and vim.deepcopy(config) or {} |
| config.focus_id = method |
| local user_title = config.title |
|
|
| lsp.buf_request_all(0, method, client_positional_params(), function(results, ctx) |
| if api.nvim_get_current_buf() ~= ctx.bufnr then |
| |
| return |
| end |
|
|
| local signatures, active_signature = process_signature_help_results(results) |
|
|
| if not next(signatures) then |
| if config.silent ~= true then |
| vim.notify('No signature help available', vim.log.levels.INFO) |
| end |
| return |
| end |
|
|
| local ft = vim.bo[ctx.bufnr].filetype |
| local total = #signatures |
| local can_cycle = total > 1 and config.focusable ~= false |
| local idx = active_signature - 1 |
|
|
| |
| local function show_signature(update_win) |
| idx = (idx % total) + 1 |
| local client, result = signatures[idx][1], signatures[idx][2] |
| |
| local triggers = |
| vim.tbl_get(client.server_capabilities, 'signatureHelpProvider', 'triggerCharacters') |
| local lines, hl = |
| util.convert_signature_help_to_markdown_lines({ signatures = { result } }, ft, triggers) |
| if not lines then |
| return |
| end |
|
|
| |
| if total > 1 then |
| local sfx = total > 1 |
| and string.format(' (%d/%d)%s', idx, total, can_cycle and ' (<C-s> to cycle)' or '') |
| or '' |
| config.title = user_title or string.format('Signature Help: %s%s', client.name, sfx) |
| |
| if not (config.border or vim.o.winborder ~= '') then |
| table.insert(lines, 1, '# ' .. config.title) |
| if hl then |
| hl[1] = hl[1] + 1 |
| hl[3] = hl[3] + 1 |
| end |
| end |
| end |
|
|
| config._update_win = update_win |
|
|
| local buf, win = util.open_floating_preview(lines, 'markdown', config) |
|
|
| if hl then |
| vim.api.nvim_buf_clear_namespace(buf, sig_help_ns, 0, -1) |
| vim.hl.range( |
| buf, |
| sig_help_ns, |
| 'LspSignatureActiveParameter', |
| { hl[1], hl[2] }, |
| { hl[3], hl[4] } |
| ) |
| end |
| return buf, win |
| end |
|
|
| local fbuf, fwin = show_signature() |
|
|
| if can_cycle then |
| vim.keymap.set('n', '<C-s>', function() |
| show_signature(fwin) |
| end, { |
| buffer = fbuf, |
| desc = 'Cycle next signature', |
| }) |
| end |
| end) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.completion(context) |
| vim.deprecate('vim.lsp.buf.completion', 'vim.lsp.completion.trigger', '0.12') |
| return lsp.buf_request( |
| 0, |
| ms.textDocument_completion, |
| client_positional_params({ |
| context = context, |
| }) |
| ) |
| end |
|
|
| |
| |
| |
| local function range_from_selection(bufnr, mode) |
| |
|
|
| |
| local start = vim.fn.getpos('v') |
| local end_ = vim.fn.getpos('.') |
| local start_row = start[2] |
| local start_col = start[3] |
| local end_row = end_[2] |
| local end_col = end_[3] |
|
|
| |
| |
| if start_row == end_row and end_col < start_col then |
| end_col, start_col = start_col, end_col |
| elseif end_row < start_row then |
| start_row, end_row = end_row, start_row |
| start_col, end_col = end_col, start_col |
| end |
| if mode == 'V' then |
| start_col = 1 |
| local lines = api.nvim_buf_get_lines(bufnr, end_row - 1, end_row, true) |
| end_col = #lines[1] |
| end |
| return { |
| ['start'] = { start_row, start_col - 1 }, |
| ['end'] = { end_row, end_col - 1 }, |
| } |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| function M.format(opts) |
| opts = opts or {} |
| local bufnr = vim._resolve_bufnr(opts.bufnr) |
| local mode = api.nvim_get_mode().mode |
| local range = opts.range |
| |
| if not range and mode == 'v' or mode == 'V' then |
| range = range_from_selection(bufnr, mode) |
| end |
|
|
| local passed_multiple_ranges = (range and #range ~= 0 and type(range[1]) == 'table') |
| local method |
| if passed_multiple_ranges then |
| method = ms.textDocument_rangesFormatting |
| elseif range then |
| method = ms.textDocument_rangeFormatting |
| else |
| method = ms.textDocument_formatting |
| end |
|
|
| local clients = lsp.get_clients({ |
| id = opts.id, |
| bufnr = bufnr, |
| name = opts.name, |
| method = method, |
| }) |
| if opts.filter then |
| clients = vim.tbl_filter(opts.filter, clients) |
| end |
|
|
| if #clients == 0 then |
| vim.notify('[LSP] Format request failed, no matching language servers.') |
| end |
|
|
| |
| |
| |
| local function set_range(client, params) |
| |
| local function to_lsp_range(r) |
| return util.make_given_range_params(r.start, r['end'], bufnr, client.offset_encoding).range |
| end |
|
|
| local ret = params |
| if passed_multiple_ranges then |
| ret = params |
| |
| ret.ranges = vim.tbl_map(to_lsp_range, range) |
| elseif range then |
| ret = params |
| ret.range = to_lsp_range(range) |
| end |
| return ret |
| end |
|
|
| if opts.async then |
| |
| |
| local function do_format(idx, client) |
| if not idx or not client then |
| return |
| end |
| local params = set_range(client, util.make_formatting_params(opts.formatting_options)) |
| client:request(method, params, function(...) |
| local handler = client.handlers[method] or lsp.handlers[method] |
| handler(...) |
| do_format(next(clients, idx)) |
| end, bufnr) |
| end |
| do_format(next(clients)) |
| else |
| local timeout_ms = opts.timeout_ms or 1000 |
| for _, client in pairs(clients) do |
| local params = set_range(client, util.make_formatting_params(opts.formatting_options)) |
| local result, err = client:request_sync(method, params, timeout_ms, bufnr) |
| if result and result.result then |
| util.apply_text_edits(result.result, bufnr, client.offset_encoding) |
| elseif err then |
| vim.notify(string.format('[LSP][%s] %s', client.name, err), vim.log.levels.WARN) |
| end |
| end |
| end |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| function M.rename(new_name, opts) |
| opts = opts or {} |
| local bufnr = vim._resolve_bufnr(opts.bufnr) |
| local clients = lsp.get_clients({ |
| bufnr = bufnr, |
| name = opts.name, |
| |
| method = ms.textDocument_rename, |
| }) |
| if opts.filter then |
| clients = vim.tbl_filter(opts.filter, clients) |
| end |
|
|
| if #clients == 0 then |
| vim.notify('[LSP] Rename, no matching language servers with rename capability.') |
| end |
|
|
| local win = api.nvim_get_current_win() |
|
|
| |
| local cword = vim.fn.expand('<cword>') |
|
|
| |
| |
| local function get_text_at_range(range, position_encoding) |
| return api.nvim_buf_get_text( |
| bufnr, |
| range.start.line, |
| util._get_line_byte_from_position(bufnr, range.start, position_encoding), |
| range['end'].line, |
| util._get_line_byte_from_position(bufnr, range['end'], position_encoding), |
| {} |
| )[1] |
| end |
|
|
| |
| |
| local function try_use_client(idx, client) |
| if not idx or not client then |
| return |
| end |
|
|
| |
| local function rename(name) |
| local params = util.make_position_params(win, client.offset_encoding) |
| params.newName = name |
| local handler = client.handlers[ms.textDocument_rename] |
| or lsp.handlers[ms.textDocument_rename] |
| client:request(ms.textDocument_rename, params, function(...) |
| handler(...) |
| try_use_client(next(clients, idx)) |
| end, bufnr) |
| end |
|
|
| if client:supports_method(ms.textDocument_prepareRename) then |
| local params = util.make_position_params(win, client.offset_encoding) |
| client:request(ms.textDocument_prepareRename, params, function(err, result) |
| if err or result == nil then |
| if next(clients, idx) then |
| try_use_client(next(clients, idx)) |
| else |
| local msg = err and ('Error on prepareRename: ' .. (err.message or '')) |
| or 'Nothing to rename' |
| vim.notify(msg, vim.log.levels.INFO) |
| end |
| return |
| end |
|
|
| if new_name then |
| rename(new_name) |
| return |
| end |
|
|
| local prompt_opts = { |
| prompt = 'New Name: ', |
| } |
| |
| if result.placeholder then |
| prompt_opts.default = result.placeholder |
| elseif result.start then |
| prompt_opts.default = get_text_at_range(result, client.offset_encoding) |
| elseif result.range then |
| prompt_opts.default = get_text_at_range(result.range, client.offset_encoding) |
| else |
| prompt_opts.default = cword |
| end |
| vim.ui.input(prompt_opts, function(input) |
| if not input or #input == 0 then |
| return |
| end |
| rename(input) |
| end) |
| end, bufnr) |
| else |
| assert( |
| client:supports_method(ms.textDocument_rename), |
| 'Client must support textDocument/rename' |
| ) |
| if new_name then |
| rename(new_name) |
| return |
| end |
|
|
| local prompt_opts = { |
| prompt = 'New Name: ', |
| default = cword, |
| } |
| vim.ui.input(prompt_opts, function(input) |
| if not input or #input == 0 then |
| return |
| end |
| rename(input) |
| end) |
| end |
| end |
|
|
| try_use_client(next(clients)) |
| end |
|
|
| |
| |
| |
| |
| |
| function M.references(context, opts) |
| validate('context', context, 'table', true) |
| local bufnr = api.nvim_get_current_buf() |
| local clients = lsp.get_clients({ method = ms.textDocument_references, bufnr = bufnr }) |
| if not next(clients) then |
| return |
| end |
| local win = api.nvim_get_current_win() |
| opts = opts or {} |
|
|
| local all_items = {} |
| local title = 'References' |
|
|
| local function on_done() |
| if not next(all_items) then |
| vim.notify('No references found') |
| else |
| local list = { |
| title = title, |
| items = all_items, |
| context = { |
| method = ms.textDocument_references, |
| bufnr = bufnr, |
| }, |
| } |
| if opts.loclist then |
| vim.fn.setloclist(0, {}, ' ', list) |
| vim.cmd.lopen() |
| elseif opts.on_list then |
| assert(vim.is_callable(opts.on_list), 'on_list is not a function') |
| opts.on_list(list) |
| else |
| vim.fn.setqflist({}, ' ', list) |
| vim.cmd('botright copen') |
| end |
| end |
| end |
|
|
| local remaining = #clients |
| for _, client in ipairs(clients) do |
| local params = util.make_position_params(win, client.offset_encoding) |
|
|
| |
| params.context = context or { |
| includeDeclaration = true, |
| } |
| client:request(ms.textDocument_references, params, function(_, result) |
| local items = util.locations_to_items(result or {}, client.offset_encoding) |
| vim.list_extend(all_items, items) |
| remaining = remaining - 1 |
| if remaining == 0 then |
| on_done() |
| end |
| end) |
| end |
| end |
|
|
| |
| |
| function M.document_symbol(opts) |
| opts = vim.tbl_deep_extend('keep', opts or {}, { loclist = true }) |
| local params = { textDocument = util.make_text_document_params() } |
| request_with_opts(ms.textDocument_documentSymbol, params, opts) |
| end |
|
|
| |
| |
| |
| |
| |
| local function request_with_id(client_id, method, params, handler, bufnr) |
| local client = lsp.get_client_by_id(client_id) |
| if not client then |
| vim.notify( |
| string.format('Client with id=%d disappeared during hierarchy request', client_id), |
| vim.log.levels.WARN |
| ) |
| return |
| end |
| client:request(method, params, handler, bufnr) |
| end |
|
|
| |
| local function format_hierarchy_item(item) |
| if not item.detail or #item.detail == 0 then |
| return item.name |
| end |
| return string.format('%s %s', item.name, item.detail) |
| end |
|
|
| local hierarchy_methods = { |
| [ms.typeHierarchy_subtypes] = 'type', |
| [ms.typeHierarchy_supertypes] = 'type', |
| [ms.callHierarchy_incomingCalls] = 'call', |
| [ms.callHierarchy_outgoingCalls] = 'call', |
| } |
|
|
| |
| local function hierarchy(method) |
| local kind = hierarchy_methods[method] |
| if not kind then |
| error('unsupported method ' .. method) |
| end |
|
|
| local prepare_method = kind == 'type' and ms.textDocument_prepareTypeHierarchy |
| or ms.textDocument_prepareCallHierarchy |
|
|
| local bufnr = api.nvim_get_current_buf() |
| local clients = lsp.get_clients({ bufnr = bufnr, method = prepare_method }) |
| if not next(clients) then |
| vim.notify(lsp._unsupported_method(method), vim.log.levels.WARN) |
| return |
| end |
|
|
| local win = api.nvim_get_current_win() |
|
|
| |
| local function on_response(results) |
| if #results == 0 then |
| vim.notify('No item resolved', vim.log.levels.WARN) |
| elseif #results == 1 then |
| local client_id, item = results[1][1], results[1][2] |
| request_with_id(client_id, method, { item = item }, nil, bufnr) |
| else |
| vim.ui.select(results, { |
| prompt = string.format('Select a %s hierarchy item:', kind), |
| kind = kind .. 'hierarchy', |
| format_item = function(x) |
| return format_hierarchy_item(x[2]) |
| end, |
| }, function(x) |
| if x then |
| local client_id, item = x[1], x[2] |
| request_with_id(client_id, method, { item = item }, nil, bufnr) |
| end |
| end) |
| end |
| end |
|
|
| local results = {} |
|
|
| local remaining = #clients |
|
|
| for _, client in ipairs(clients) do |
| local params = util.make_position_params(win, client.offset_encoding) |
| |
| client:request(prepare_method, params, function(err, result, ctx) |
| if err then |
| vim.notify(err.message, vim.log.levels.WARN) |
| elseif result then |
| for _, item in ipairs(result) do |
| results[#results + 1] = { ctx.client_id, item } |
| end |
| end |
|
|
| remaining = remaining - 1 |
| if remaining == 0 then |
| on_response(results) |
| end |
| end, bufnr) |
| end |
| end |
|
|
| |
| |
| |
| function M.incoming_calls() |
| hierarchy(ms.callHierarchy_incomingCalls) |
| end |
|
|
| |
| |
| |
| function M.outgoing_calls() |
| hierarchy(ms.callHierarchy_outgoingCalls) |
| end |
|
|
| |
| |
| |
| |
| function M.typehierarchy(kind) |
| local method = kind == 'subtypes' and ms.typeHierarchy_subtypes or ms.typeHierarchy_supertypes |
| hierarchy(method) |
| end |
|
|
| |
| |
| function M.list_workspace_folders() |
| local workspace_folders = {} |
| for _, client in pairs(lsp.get_clients({ bufnr = 0 })) do |
| for _, folder in pairs(client.workspace_folders or {}) do |
| table.insert(workspace_folders, folder.name) |
| end |
| end |
| return workspace_folders |
| 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') |
| api.nvim_command('redraw') |
| if not (workspace_folder and #workspace_folder > 0) then |
| return |
| end |
| if vim.fn.isdirectory(workspace_folder) == 0 then |
| vim.notify(workspace_folder .. ' is not a valid directory') |
| return |
| end |
| local bufnr = api.nvim_get_current_buf() |
| for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do |
| client:_add_workspace_folder(workspace_folder) |
| end |
| end |
|
|
| |
| |
| |
| |
| function M.remove_workspace_folder(workspace_folder) |
| workspace_folder = workspace_folder |
| or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h')) |
| api.nvim_command('redraw') |
| if not workspace_folder or #workspace_folder == 0 then |
| return |
| end |
| local bufnr = api.nvim_get_current_buf() |
| for _, client in pairs(lsp.get_clients({ bufnr = bufnr })) do |
| client:_remove_workspace_folder(workspace_folder) |
| end |
| vim.notify(workspace_folder .. 'is not currently part of the workspace') |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function M.workspace_symbol(query, opts) |
| query = query or npcall(vim.fn.input, 'Query: ') |
| if query == nil then |
| return |
| end |
| local params = { query = query } |
| request_with_opts(ms.workspace_symbol, params, opts) |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function M.document_highlight() |
| lsp.buf_request(0, ms.textDocument_documentHighlight, client_positional_params()) |
| end |
|
|
| |
| function M.clear_references() |
| util.buf_clear_references() |
| end |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| local function on_code_action_results(results, opts) |
| |
| local function action_filter(a) |
| |
| if opts and opts.context and opts.context.only then |
| if not a.kind then |
| return false |
| end |
| local found = false |
| for _, o in ipairs(opts.context.only) do |
| |
| |
| if a.kind == o or vim.startswith(a.kind, o .. '.') then |
| found = true |
| break |
| end |
| end |
| if not found then |
| return false |
| end |
| end |
| |
| if opts and opts.filter and not opts.filter(a) then |
| return false |
| end |
| |
| return true |
| end |
|
|
| |
| local actions = {} |
| for _, result in pairs(results) do |
| for _, action in pairs(result.result or {}) do |
| if action_filter(action) then |
| table.insert(actions, { action = action, ctx = result.ctx }) |
| end |
| end |
| end |
| if #actions == 0 then |
| vim.notify('No code actions available', vim.log.levels.INFO) |
| return |
| end |
|
|
| |
| |
| |
| local function apply_action(action, client, ctx) |
| if action.edit then |
| util.apply_workspace_edit(action.edit, client.offset_encoding) |
| end |
| local a_cmd = action.command |
| if a_cmd then |
| local command = type(a_cmd) == 'table' and a_cmd or action |
| |
| client:exec_cmd(command, ctx) |
| end |
| end |
|
|
| |
| local function on_user_choice(choice) |
| if not choice then |
| return |
| end |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| local client = assert(lsp.get_client_by_id(choice.ctx.client_id)) |
| local action = choice.action |
| local bufnr = assert(choice.ctx.bufnr, 'Must have buffer number') |
|
|
| |
| if type(action.title) == 'string' and type(action.command) == 'string' then |
| apply_action(action, client, choice.ctx) |
| return |
| end |
|
|
| if not (action.edit and action.command) and client:supports_method(ms.codeAction_resolve) then |
| client:request(ms.codeAction_resolve, action, function(err, resolved_action) |
| if err then |
| |
| if action.edit or action.command then |
| apply_action(action, client, choice.ctx) |
| else |
| vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR) |
| end |
| else |
| apply_action(resolved_action, client, choice.ctx) |
| end |
| end, bufnr) |
| else |
| apply_action(action, client, choice.ctx) |
| end |
| end |
|
|
| |
| |
| if opts and opts.apply and #actions == 1 then |
| on_user_choice(actions[1]) |
| return |
| end |
|
|
| |
| local function format_item(item) |
| local clients = lsp.get_clients({ bufnr = item.ctx.bufnr }) |
| local title = item.action.title:gsub('\r\n', '\\r\\n'):gsub('\n', '\\n') |
|
|
| if #clients == 1 then |
| return title |
| end |
|
|
| local source = lsp.get_client_by_id(item.ctx.client_id).name |
| return ('%s [%s]'):format(title, source) |
| end |
|
|
| local select_opts = { |
| prompt = 'Code actions:', |
| kind = 'codeaction', |
| format_item = format_item, |
| } |
| vim.ui.select(actions, select_opts, on_user_choice) |
| end |
|
|
| |
| |
| |
| |
| |
| function M.code_action(opts) |
| validate('options', opts, 'table', true) |
| opts = opts or {} |
| |
| |
| |
| if opts.diagnostics or opts.only then |
| opts = { options = opts } |
| end |
| local context = opts.context and vim.deepcopy(opts.context) or {} |
| if not context.triggerKind then |
| context.triggerKind = lsp.protocol.CodeActionTriggerKind.Invoked |
| end |
| local mode = api.nvim_get_mode().mode |
| local bufnr = api.nvim_get_current_buf() |
| local win = api.nvim_get_current_win() |
| local clients = lsp.get_clients({ bufnr = bufnr, method = ms.textDocument_codeAction }) |
| local remaining = #clients |
| if remaining == 0 then |
| if next(lsp.get_clients({ bufnr = bufnr })) then |
| vim.notify(lsp._unsupported_method(ms.textDocument_codeAction), vim.log.levels.WARN) |
| end |
| return |
| end |
|
|
| |
| local results = {} |
|
|
| |
| |
| |
| local function on_result(err, result, ctx) |
| results[ctx.client_id] = { error = err, result = result, ctx = ctx } |
| remaining = remaining - 1 |
| if remaining == 0 then |
| on_code_action_results(results, opts) |
| end |
| end |
|
|
| for _, client in ipairs(clients) do |
| |
| local params |
|
|
| if opts.range then |
| assert(type(opts.range) == 'table', 'code_action range must be a table') |
| local start = assert(opts.range.start, 'range must have a `start` property') |
| local end_ = assert(opts.range['end'], 'range must have a `end` property') |
| params = util.make_given_range_params(start, end_, bufnr, client.offset_encoding) |
| elseif mode == 'v' or mode == 'V' then |
| local range = range_from_selection(bufnr, mode) |
| params = |
| util.make_given_range_params(range.start, range['end'], bufnr, client.offset_encoding) |
| else |
| params = util.make_range_params(win, client.offset_encoding) |
| end |
|
|
| |
|
|
| if context.diagnostics then |
| params.context = context |
| else |
| local ns_push = lsp.diagnostic.get_namespace(client.id, false) |
| local ns_pull = lsp.diagnostic.get_namespace(client.id, true) |
| local diagnostics = {} |
| local lnum = api.nvim_win_get_cursor(0)[1] - 1 |
| vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_pull, lnum = lnum })) |
| vim.list_extend(diagnostics, vim.diagnostic.get(bufnr, { namespace = ns_push, lnum = lnum })) |
| params.context = vim.tbl_extend('force', context, { |
| |
| diagnostics = vim.tbl_map(function(d) |
| return d.user_data.lsp |
| end, diagnostics), |
| }) |
| end |
|
|
| client:request(ms.textDocument_codeAction, params, on_result, bufnr) |
| end |
| end |
|
|
| |
| |
| |
| |
| function M.execute_command(command_params) |
| validate('command', command_params.command, 'string') |
| validate('arguments', command_params.arguments, 'table', true) |
| vim.deprecate('execute_command', 'client:exec_cmd', '0.12') |
| command_params = { |
| command = command_params.command, |
| arguments = command_params.arguments, |
| workDoneToken = command_params.workDoneToken, |
| } |
| lsp.buf_request(0, ms.workspace_executeCommand, command_params) |
| end |
|
|
| return M |
|
|