aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/lsp/buf.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/lsp/buf.lua')
-rw-r--r--runtime/lua/vim/lsp/buf.lua242
1 files changed, 146 insertions, 96 deletions
diff --git a/runtime/lua/vim/lsp/buf.lua b/runtime/lua/vim/lsp/buf.lua
index 50121f30b2..49833eaeec 100644
--- a/runtime/lua/vim/lsp/buf.lua
+++ b/runtime/lua/vim/lsp/buf.lua
@@ -35,13 +35,13 @@ function M.hover()
request(ms.textDocument_hover, params)
end
-local function request_with_options(name, params, options)
+local function request_with_opts(name, params, opts)
local req_handler --- @type function?
- if options then
+ if opts then
req_handler = function(err, result, ctx, config)
local client = assert(vim.lsp.get_client_by_id(ctx.client_id))
local handler = client.handlers[name] or vim.lsp.handlers[name]
- handler(err, result, ctx, vim.tbl_extend('force', config or {}, options))
+ handler(err, result, ctx, vim.tbl_extend('force', config or {}, opts))
end
end
request(name, params, req_handler)
@@ -62,14 +62,13 @@ end
--- vim.lsp.buf.references(nil, { on_list = on_list })
--- ```
---
---- If you prefer loclist do something like this:
+--- If you prefer loclist instead of qflist:
--- ```lua
---- local function on_list(options)
---- vim.fn.setloclist(0, {}, ' ', options)
---- vim.cmd.lopen()
---- end
+--- vim.lsp.buf.definition({ loclist = true })
+--- vim.lsp.buf.references(nil, { loclist = true })
--- ```
--- @field on_list? fun(t: vim.lsp.LocationOpts.OnList)
+--- @field loclist? boolean
--- @class vim.lsp.LocationOpts.OnList
--- @field items table[] Structured like |setqflist-what|
@@ -83,32 +82,32 @@ end
--- Jumps to the declaration of the symbol under the cursor.
--- @note Many servers do not implement this method. Generally, see |vim.lsp.buf.definition()| instead.
---- @param options? vim.lsp.LocationOpts
-function M.declaration(options)
+--- @param opts? vim.lsp.LocationOpts
+function M.declaration(opts)
local params = util.make_position_params()
- request_with_options(ms.textDocument_declaration, params, options)
+ request_with_opts(ms.textDocument_declaration, params, opts)
end
--- Jumps to the definition of the symbol under the cursor.
---- @param options? vim.lsp.LocationOpts
-function M.definition(options)
+--- @param opts? vim.lsp.LocationOpts
+function M.definition(opts)
local params = util.make_position_params()
- request_with_options(ms.textDocument_definition, params, options)
+ request_with_opts(ms.textDocument_definition, params, opts)
end
--- Jumps to the definition of the type of the symbol under the cursor.
---- @param options? vim.lsp.LocationOpts
-function M.type_definition(options)
+--- @param opts? vim.lsp.LocationOpts
+function M.type_definition(opts)
local params = util.make_position_params()
- request_with_options(ms.textDocument_typeDefinition, params, options)
+ request_with_opts(ms.textDocument_typeDefinition, params, opts)
end
--- Lists all the implementations for the symbol under the cursor in the
--- quickfix window.
---- @param options? vim.lsp.LocationOpts
-function M.implementation(options)
+--- @param opts? vim.lsp.LocationOpts
+function M.implementation(opts)
local params = util.make_position_params()
- request_with_options(ms.textDocument_implementation, params, options)
+ request_with_opts(ms.textDocument_implementation, params, opts)
end
--- Displays signature information about the symbol under the cursor in a
@@ -213,25 +212,25 @@ end
--- Formats a buffer using the attached (and optionally filtered) language
--- server clients.
---
---- @param options? vim.lsp.buf.format.Opts
-function M.format(options)
- options = options or {}
- local bufnr = options.bufnr or api.nvim_get_current_buf()
+--- @param opts? vim.lsp.buf.format.Opts
+function M.format(opts)
+ opts = opts or {}
+ local bufnr = opts.bufnr or api.nvim_get_current_buf()
local mode = api.nvim_get_mode().mode
- local range = options.range
+ local range = opts.range
if not range and mode == 'v' or mode == 'V' then
range = range_from_selection(bufnr, mode)
end
local method = range and ms.textDocument_rangeFormatting or ms.textDocument_formatting
local clients = vim.lsp.get_clients({
- id = options.id,
+ id = opts.id,
bufnr = bufnr,
- name = options.name,
+ name = opts.name,
method = method,
})
- if options.filter then
- clients = vim.tbl_filter(options.filter, clients)
+ if opts.filter then
+ clients = vim.tbl_filter(opts.filter, clients)
end
if #clients == 0 then
@@ -250,12 +249,12 @@ function M.format(options)
return params
end
- if options.async then
+ if opts.async then
local function do_format(idx, client)
if not client then
return
end
- local params = set_range(client, util.make_formatting_params(options.formatting_options))
+ local params = set_range(client, util.make_formatting_params(opts.formatting_options))
client.request(method, params, function(...)
local handler = client.handlers[method] or vim.lsp.handlers[method]
handler(...)
@@ -264,9 +263,9 @@ function M.format(options)
end
do_format(next(clients))
else
- local timeout_ms = options.timeout_ms or 1000
+ local timeout_ms = opts.timeout_ms or 1000
for _, client in pairs(clients) do
- local params = set_range(client, util.make_formatting_params(options.formatting_options))
+ 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)
@@ -295,18 +294,18 @@ end
---
---@param new_name string|nil If not provided, the user will be prompted for a new
--- name using |vim.ui.input()|.
----@param options? vim.lsp.buf.rename.Opts Additional options:
-function M.rename(new_name, options)
- options = options or {}
- local bufnr = options.bufnr or api.nvim_get_current_buf()
+---@param opts? vim.lsp.buf.rename.Opts Additional options:
+function M.rename(new_name, opts)
+ opts = opts or {}
+ local bufnr = opts.bufnr or api.nvim_get_current_buf()
local clients = vim.lsp.get_clients({
bufnr = bufnr,
- name = options.name,
+ name = opts.name,
-- Clients must at least support rename, prepareRename is optional
method = ms.textDocument_rename,
})
- if options.filter then
- clients = vim.tbl_filter(options.filter, clients)
+ if opts.filter then
+ clients = vim.tbl_filter(opts.filter, clients)
end
if #clients == 0 then
@@ -415,21 +414,21 @@ end
---
---@param context (table|nil) Context for the request
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
----@param options? vim.lsp.ListOpts
-function M.references(context, options)
+---@param opts? vim.lsp.ListOpts
+function M.references(context, opts)
validate({ context = { context, 't', true } })
local params = util.make_position_params()
params.context = context or {
includeDeclaration = true,
}
- request_with_options(ms.textDocument_references, params, options)
+ request_with_opts(ms.textDocument_references, params, opts)
end
--- Lists all symbols in the current buffer in the quickfix window.
---- @param options? vim.lsp.ListOpts
-function M.document_symbol(options)
+--- @param opts? vim.lsp.ListOpts
+function M.document_symbol(opts)
local params = { textDocument = util.make_text_document_params() }
- request_with_options(ms.textDocument_documentSymbol, params, options)
+ request_with_opts(ms.textDocument_documentSymbol, params, opts)
end
--- @param call_hierarchy_items lsp.CallHierarchyItem[]?
@@ -461,7 +460,14 @@ local function call_hierarchy(method)
vim.notify(err.message, vim.log.levels.WARN)
return
end
+ if not result then
+ vim.notify('No item resolved', vim.log.levels.WARN)
+ return
+ end
local call_hierarchy_item = pick_call_hierarchy_item(result)
+ if not call_hierarchy_item then
+ return
+ end
local client = vim.lsp.get_client_by_id(ctx.client_id)
if client then
client.request(method, { item = call_hierarchy_item }, nil, ctx.bufnr)
@@ -488,6 +494,80 @@ function M.outgoing_calls()
call_hierarchy(ms.callHierarchy_outgoingCalls)
end
+--- Lists all the subtypes or supertypes of the symbol under the
+--- cursor in the |quickfix| window. If the symbol can resolve to
+--- multiple items, the user can pick one using |vim.ui.select()|.
+---@param kind "subtypes"|"supertypes"
+function M.typehierarchy(kind)
+ local method = kind == 'subtypes' and ms.typeHierarchy_subtypes or ms.typeHierarchy_supertypes
+
+ --- Merge results from multiple clients into a single table. Client-ID is preserved.
+ ---
+ --- @param results table<integer, {error: lsp.ResponseError?, result: lsp.TypeHierarchyItem[]?}>
+ --- @return [integer, lsp.TypeHierarchyItem][]
+ local function merge_results(results)
+ local merged_results = {}
+ for client_id, client_result in pairs(results) do
+ if client_result.error then
+ vim.notify(client_result.error.message, vim.log.levels.WARN)
+ elseif client_result.result then
+ for _, item in pairs(client_result.result) do
+ table.insert(merged_results, { client_id, item })
+ end
+ end
+ end
+ return merged_results
+ end
+
+ local bufnr = api.nvim_get_current_buf()
+ local params = util.make_position_params()
+ --- @param results table<integer, {error: lsp.ResponseError?, result: lsp.TypeHierarchyItem[]?}>
+ vim.lsp.buf_request_all(bufnr, ms.textDocument_prepareTypeHierarchy, params, function(results)
+ local merged_results = merge_results(results)
+ if #merged_results == 0 then
+ vim.notify('No items resolved', vim.log.levels.INFO)
+ return
+ end
+
+ if #merged_results == 1 then
+ local item = merged_results[1]
+ local client = vim.lsp.get_client_by_id(item[1])
+ if client then
+ client.request(method, { item = item[2] }, nil, bufnr)
+ else
+ vim.notify(
+ string.format('Client with id=%d disappeared during call hierarchy request', item[1]),
+ vim.log.levels.WARN
+ )
+ end
+ else
+ local select_opts = {
+ prompt = 'Select a type hierarchy item:',
+ kind = 'typehierarchy',
+ format_item = function(item)
+ if not item[2].detail or #item[2].detail == 0 then
+ return item[2].name
+ end
+ return string.format('%s %s', item[2].name, item[2].detail)
+ end,
+ }
+
+ vim.ui.select(merged_results, select_opts, function(item)
+ local client = vim.lsp.get_client_by_id(item[1])
+ if client then
+ --- @type lsp.TypeHierarchyItem
+ client.request(method, { item = item[2] }, nil, bufnr)
+ else
+ vim.notify(
+ string.format('Client with id=%d disappeared during call hierarchy request', item[1]),
+ vim.log.levels.WARN
+ )
+ end
+ end)
+ end
+ end)
+end
+
--- List workspace folders.
---
function M.list_workspace_folders()
@@ -514,28 +594,9 @@ function M.add_workspace_folder(workspace_folder)
print(workspace_folder, ' is not a valid directory')
return
end
- local new_workspace = {
- uri = vim.uri_from_fname(workspace_folder),
- name = workspace_folder,
- }
- local params = { event = { added = { new_workspace }, removed = {} } }
- local bufnr = vim.api.nvim_get_current_buf()
+ local bufnr = api.nvim_get_current_buf()
for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do
- local found = false
- for _, folder in pairs(client.workspace_folders or {}) do
- if folder.name == workspace_folder then
- found = true
- print(workspace_folder, 'is already part of this workspace')
- break
- end
- end
- if not found then
- client.notify(ms.workspace_didChangeWorkspaceFolders, params)
- if not client.workspace_folders then
- client.workspace_folders = {}
- end
- table.insert(client.workspace_folders, new_workspace)
- end
+ client:_add_workspace_folder(workspace_folder)
end
end
@@ -547,23 +608,12 @@ 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 and #workspace_folder > 0) then
+ if not workspace_folder or #workspace_folder == 0 then
return
end
- local workspace = {
- uri = vim.uri_from_fname(workspace_folder),
- name = workspace_folder,
- }
- local params = { event = { added = {}, removed = { workspace } } }
- local bufnr = vim.api.nvim_get_current_buf()
+ local bufnr = api.nvim_get_current_buf()
for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do
- for idx, folder in pairs(client.workspace_folders) do
- if folder.name == workspace_folder then
- client.notify(ms.workspace_didChangeWorkspaceFolders, params)
- client.workspace_folders[idx] = nil
- return
- end
- end
+ client:_remove_workspace_folder(workspace_folder)
end
print(workspace_folder, 'is not currently part of the workspace')
end
@@ -575,14 +625,14 @@ end
--- string means no filtering is done.
---
--- @param query string? optional
---- @param options? vim.lsp.ListOpts
-function M.workspace_symbol(query, options)
+--- @param opts? vim.lsp.ListOpts
+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_options(ms.workspace_symbol, params, options)
+ request_with_opts(ms.workspace_symbol, params, opts)
end
--- Send request to the server to resolve document highlights for the current
@@ -774,19 +824,19 @@ end
--- Selects a code action available at the current
--- cursor position.
---
----@param options? vim.lsp.buf.code_action.Opts
+---@param opts? vim.lsp.buf.code_action.Opts
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction
---@see vim.lsp.protocol.CodeActionTriggerKind
-function M.code_action(options)
- validate({ options = { options, 't', true } })
- options = options or {}
+function M.code_action(opts)
+ validate({ options = { opts, 't', true } })
+ opts = opts or {}
-- Detect old API call code_action(context) which should now be
-- code_action({ context = context} )
--- @diagnostic disable-next-line:undefined-field
- if options.diagnostics or options.only then
- options = { options = options }
+ if opts.diagnostics or opts.only then
+ opts = { options = opts }
end
- local context = options.context or {}
+ local context = opts.context or {}
if not context.triggerKind then
context.triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Invoked
end
@@ -816,17 +866,17 @@ function M.code_action(options)
results[ctx.client_id] = { error = err, result = result, ctx = ctx }
remaining = remaining - 1
if remaining == 0 then
- on_code_action_results(results, options)
+ on_code_action_results(results, opts)
end
end
for _, client in ipairs(clients) do
---@type lsp.CodeActionParams
local params
- if options.range then
- assert(type(options.range) == 'table', 'code_action range must be a table')
- local start = assert(options.range.start, 'range must have a `start` property')
- local end_ = assert(options.range['end'], 'range must have a `end` property')
+ 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)