2022-07-15 09:26:47 -07:00
|
|
|
local api = vim.api
|
2019-11-20 15:21:57 -07:00
|
|
|
local validate = vim.validate
|
|
|
|
local util = require('vim.lsp.util')
|
2022-07-15 08:55:00 -07:00
|
|
|
local npcall = vim.F.npcall
|
2023-08-03 04:03:48 -07:00
|
|
|
local ms = require('vim.lsp.protocol').Methods
|
2019-11-20 15:21:57 -07:00
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Sends an async request to all active clients attached to the current
|
|
|
|
--- buffer.
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@param method (string) LSP method name
|
2022-07-28 10:19:07 -07:00
|
|
|
---@param params (table|nil) Parameters to send to the server
|
|
|
|
---@param handler (function|nil) See |lsp-handler|. Follows |lsp-handler-resolution|
|
2020-08-19 09:17:08 -07:00
|
|
|
--
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return table<integer, integer> client_request_ids Map of client-id:request-id pairs
|
|
|
|
---for all successful requests.
|
|
|
|
---@return function _cancel_all_requests Function which can be used to
|
|
|
|
---cancel all the requests. You could instead
|
|
|
|
---iterate all clients and call their `cancel_request()` methods.
|
2020-08-19 09:17:08 -07:00
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see |vim.lsp.buf_request()|
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
local function request(method, params, handler)
|
2019-11-20 17:16:36 -07:00
|
|
|
validate({
|
|
|
|
method = { method, 's' },
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
handler = { handler, 'f', true },
|
2019-11-20 17:16:36 -07:00
|
|
|
})
|
lsp: vim.lsp.diagnostic (#12655)
Breaking Changes:
- Deprecated all `vim.lsp.util.{*diagnostics*}()` functions.
- Instead, all functions must be found in vim.lsp.diagnostic
- For now, they issue a warning ONCE per neovim session. In a
"little while" we will remove them completely.
- `vim.lsp.callbacks` has moved to `vim.lsp.handlers`.
- For a "little while" we will just redirect `vim.lsp.callbacks` to
`vim.lsp.handlers`. However, we will remove this at some point, so
it is recommended that you change all of your references to
`callbacks` into `handlers`.
- This also means that for functions like |vim.lsp.start_client()|
and similar, keyword style arguments have moved from "callbacks"
to "handlers". Once again, these are currently being forward, but
will cease to be forwarded in a "little while".
- Changed the highlight groups for LspDiagnostic highlight as they were
inconsistently named.
- For more information, see |lsp-highlight-diagnostics|
- Changed the sign group names as well, to be consistent with
|lsp-highlight-diagnostics|
General Enhancements:
- Rewrote much of the getting started help document for lsp. It also
provides a much nicer configuration strategy, so as to not recommend
globally overwriting builtin neovim mappings.
LSP Enhancements:
- Introduced the concept of |lsp-handlers| which will allow much better
customization for users without having to copy & paste entire files /
functions / etc.
Diagnostic Enhancements:
- "goto next diagnostic" |vim.lsp.diagnostic.goto_next()|
- "goto prev diagnostic" |vim.lsp.diagnostic.goto_prev()|
- For each of the gotos, auto open diagnostics is available as a
configuration option
- Configurable diagnostic handling:
- See |vim.lsp.diagnostic.on_publish_diagnostics()|
- Delay display until after insert mode
- Configure signs
- Configure virtual text
- Configure underline
- Set the location list with the buffers diagnostics.
- See |vim.lsp.diagnostic.set_loclist()|
- Better performance for getting counts and line diagnostics
- They are now cached on save, to enhance lookups.
- Particularly useful for checking in statusline, etc.
- Actual testing :)
- See ./test/functional/plugin/lsp/diagnostic_spec.lua
- Added `guisp` for underline highlighting
NOTE: "a little while" means enough time to feel like most plugins and
plugin authors have had a chance to refactor their code to use the
updated calls. Then we will remove them completely. There is no need to
keep them, because we don't have any released version of neovim that
exposes these APIs. I'm trying to be nice to people following HEAD :)
Co-authored: [Twitch Chat 2020](https://twitch.tv/teej_dv)
2020-11-12 20:21:34 -07:00
|
|
|
return vim.lsp.buf_request(0, method, params, handler)
|
2019-11-20 16:35:18 -07:00
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Checks whether the language servers attached to the current buffer are
|
|
|
|
--- ready.
|
|
|
|
---
|
2023-07-10 04:38:15 -07:00
|
|
|
---@return boolean if server responds.
|
2023-05-13 02:27:05 -07:00
|
|
|
---@deprecated
|
2020-02-26 12:22:14 -07:00
|
|
|
function M.server_ready()
|
2023-05-13 02:27:05 -07:00
|
|
|
vim.deprecate('vim.lsp.buf.server_ready', nil, '0.10.0')
|
2020-02-26 12:22:14 -07:00
|
|
|
return not not vim.lsp.buf_notify(0, 'window/progress', {})
|
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Displays hover information about the symbol under the cursor in a floating
|
|
|
|
--- window. Calling the function twice will jump into the floating window.
|
2019-11-20 16:35:18 -07:00
|
|
|
function M.hover()
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request(ms.textDocument_hover, params)
|
2019-11-20 15:21:57 -07:00
|
|
|
end
|
|
|
|
|
2022-05-18 12:03:24 -07:00
|
|
|
local function request_with_options(name, params, options)
|
|
|
|
local req_handler
|
|
|
|
if options then
|
|
|
|
req_handler = function(err, result, ctx, config)
|
|
|
|
local client = 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))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
request(name, params, req_handler)
|
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Jumps to the declaration of the symbol under the cursor.
|
2021-08-22 13:55:28 -07:00
|
|
|
---@note Many servers do not implement this method. Generally, see |vim.lsp.buf.definition()| instead.
|
2020-07-19 14:16:12 -07:00
|
|
|
---
|
2022-05-18 12:03:24 -07:00
|
|
|
---@param options table|nil additional options
|
|
|
|
--- - reuse_win: (boolean) Jump to existing window if buffer is already open.
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
2022-05-18 12:03:24 -07:00
|
|
|
function M.declaration(options)
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.textDocument_declaration, params, options)
|
2019-11-20 16:35:18 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Jumps to the definition of the symbol under the cursor.
|
|
|
|
---
|
2022-05-18 12:03:24 -07:00
|
|
|
---@param options table|nil additional options
|
|
|
|
--- - reuse_win: (boolean) Jump to existing window if buffer is already open.
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
2022-05-18 12:03:24 -07:00
|
|
|
function M.definition(options)
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.textDocument_definition, params, options)
|
2019-11-20 15:21:57 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Jumps to the definition of the type of the symbol under the cursor.
|
|
|
|
---
|
2022-05-18 12:03:24 -07:00
|
|
|
---@param options table|nil additional options
|
|
|
|
--- - reuse_win: (boolean) Jump to existing window if buffer is already open.
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
2022-05-18 12:03:24 -07:00
|
|
|
function M.type_definition(options)
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.textDocument_typeDefinition, params, options)
|
2019-11-20 15:21:57 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Lists all the implementations for the symbol under the cursor in the
|
|
|
|
--- quickfix window.
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
---
|
|
|
|
---@param options table|nil additional options
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
function M.implementation(options)
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.textDocument_implementation, params, options)
|
2019-11-20 17:03:32 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Displays signature information about the symbol under the cursor in a
|
|
|
|
--- floating window.
|
2019-11-20 16:35:18 -07:00
|
|
|
function M.signature_help()
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request(ms.textDocument_signatureHelp, params)
|
2019-11-20 15:21:57 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Retrieves the completion items at the current cursor position. Can only be
|
|
|
|
--- called in Insert mode.
|
2020-08-19 09:17:08 -07:00
|
|
|
---
|
2023-07-10 04:38:15 -07:00
|
|
|
---@param context table (context support not yet implemented) Additional information
|
2020-08-19 09:17:08 -07:00
|
|
|
--- about the context in which a completion was triggered (how it was triggered,
|
|
|
|
--- and by which trigger character, if applicable)
|
|
|
|
---
|
2023-08-07 06:35:06 -07:00
|
|
|
---@see vim.lsp.protocol.CompletionTriggerKind
|
2019-11-20 16:35:18 -07:00
|
|
|
function M.completion(context)
|
2019-11-21 16:41:32 -07:00
|
|
|
local params = util.make_position_params()
|
2019-11-20 17:16:36 -07:00
|
|
|
params.context = context
|
2023-08-03 04:03:48 -07:00
|
|
|
return request(ms.textDocument_completion, params)
|
2019-11-20 15:21:57 -07:00
|
|
|
end
|
|
|
|
|
2023-03-12 01:45:28 -07:00
|
|
|
---@param bufnr integer
|
|
|
|
---@param mode "v"|"V"
|
2023-06-24 04:47:10 -07:00
|
|
|
---@return table {start={row,col}, end={row,col}} using (1, 0) indexing
|
2023-03-12 01:45:28 -07:00
|
|
|
local function range_from_selection(bufnr, mode)
|
2022-09-08 02:33:04 -07:00
|
|
|
-- TODO: Use `vim.region()` instead https://github.com/neovim/neovim/pull/13896
|
|
|
|
|
|
|
|
-- [bufnum, lnum, col, off]; both row and column 1-indexed
|
|
|
|
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]
|
|
|
|
|
|
|
|
-- A user can start visual selection at the end and move backwards
|
|
|
|
-- Normalize the range to start < end
|
|
|
|
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
|
2023-03-12 01:45:28 -07:00
|
|
|
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
|
2022-09-08 02:33:04 -07:00
|
|
|
return {
|
|
|
|
['start'] = { start_row, start_col - 1 },
|
|
|
|
['end'] = { end_row, end_col - 1 },
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-04-30 06:36:40 -07:00
|
|
|
--- Formats a buffer using the attached (and optionally filtered) language
|
|
|
|
--- server clients.
|
|
|
|
---
|
|
|
|
--- @param options table|nil Optional table which holds the following optional fields:
|
|
|
|
--- - formatting_options (table|nil):
|
|
|
|
--- Can be used to specify FormattingOptions. Some unspecified options will be
|
2023-06-22 03:44:51 -07:00
|
|
|
--- automatically derived from the current Nvim options.
|
2022-07-28 10:41:30 -07:00
|
|
|
--- See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#formattingOptions
|
2022-04-30 06:36:40 -07:00
|
|
|
--- - timeout_ms (integer|nil, default 1000):
|
2022-04-30 08:23:50 -07:00
|
|
|
--- Time in milliseconds to block for formatting requests. No effect if async=true
|
2022-04-30 06:36:40 -07:00
|
|
|
--- - bufnr (number|nil):
|
|
|
|
--- Restrict formatting to the clients attached to the given buffer, defaults to the current
|
|
|
|
--- buffer (0).
|
2022-05-25 10:38:01 -07:00
|
|
|
---
|
2022-04-30 06:36:40 -07:00
|
|
|
--- - filter (function|nil):
|
2022-05-25 10:38:01 -07:00
|
|
|
--- Predicate used to filter clients. Receives a client as argument and must return a
|
|
|
|
--- boolean. Clients matching the predicate are included. Example:
|
2022-04-30 06:36:40 -07:00
|
|
|
---
|
2022-11-23 04:31:49 -07:00
|
|
|
--- <pre>lua
|
|
|
|
--- -- Never request typescript-language-server for formatting
|
|
|
|
--- vim.lsp.buf.format {
|
|
|
|
--- filter = function(client) return client.name ~= "tsserver" end
|
|
|
|
--- }
|
2022-04-30 06:36:40 -07:00
|
|
|
--- </pre>
|
|
|
|
---
|
2022-04-30 08:23:50 -07:00
|
|
|
--- - async boolean|nil
|
|
|
|
--- If true the method won't block. Defaults to false.
|
|
|
|
--- Editing the buffer while formatting asynchronous can lead to unexpected
|
|
|
|
--- changes.
|
|
|
|
---
|
2022-04-30 06:36:40 -07:00
|
|
|
--- - id (number|nil):
|
|
|
|
--- Restrict formatting to the client with ID (client.id) matching this field.
|
|
|
|
--- - name (string|nil):
|
|
|
|
--- Restrict formatting to the client with name (client.name) matching this field.
|
2022-09-08 02:33:04 -07:00
|
|
|
---
|
|
|
|
--- - range (table|nil) Range to format.
|
2023-06-24 04:47:10 -07:00
|
|
|
--- Table must contain `start` and `end` keys with {row,col} tuples using
|
2022-09-08 02:33:04 -07:00
|
|
|
--- (1,0) indexing.
|
|
|
|
--- Defaults to current selection in visual mode
|
|
|
|
--- Defaults to `nil` in other modes, formatting the full buffer
|
2022-04-30 06:36:40 -07:00
|
|
|
function M.format(options)
|
|
|
|
options = options or {}
|
2022-07-15 09:26:47 -07:00
|
|
|
local bufnr = options.bufnr or api.nvim_get_current_buf()
|
2023-01-23 23:15:43 -07:00
|
|
|
local mode = api.nvim_get_mode().mode
|
|
|
|
local range = options.range
|
|
|
|
if not range and mode == 'v' or mode == 'V' then
|
2023-03-12 01:45:28 -07:00
|
|
|
range = range_from_selection(bufnr, mode)
|
2023-01-23 23:15:43 -07:00
|
|
|
end
|
2023-08-03 04:03:48 -07:00
|
|
|
local method = range and ms.textDocument_rangeFormatting or ms.textDocument_formatting
|
2023-01-23 23:15:43 -07:00
|
|
|
|
2023-07-17 09:27:16 -07:00
|
|
|
local clients = vim.lsp.get_clients({
|
2023-07-12 05:48:21 -07:00
|
|
|
id = options.id,
|
|
|
|
bufnr = bufnr,
|
|
|
|
name = options.name,
|
|
|
|
method = method,
|
|
|
|
})
|
|
|
|
if options.filter then
|
|
|
|
clients = vim.tbl_filter(options.filter, clients)
|
|
|
|
end
|
2022-04-30 06:36:40 -07:00
|
|
|
|
|
|
|
if #clients == 0 then
|
|
|
|
vim.notify('[LSP] Format request failed, no matching language servers.')
|
|
|
|
end
|
|
|
|
|
2022-09-08 02:33:04 -07:00
|
|
|
local function set_range(client, params)
|
|
|
|
if range then
|
|
|
|
local range_params =
|
|
|
|
util.make_given_range_params(range.start, range['end'], bufnr, client.offset_encoding)
|
|
|
|
params.range = range_params.range
|
|
|
|
end
|
|
|
|
return params
|
|
|
|
end
|
|
|
|
|
2022-04-30 08:23:50 -07:00
|
|
|
if options.async then
|
|
|
|
local do_format
|
|
|
|
do_format = function(idx, client)
|
|
|
|
if not client then
|
|
|
|
return
|
|
|
|
end
|
2022-09-08 02:33:04 -07:00
|
|
|
local params = set_range(client, util.make_formatting_params(options.formatting_options))
|
|
|
|
client.request(method, params, function(...)
|
|
|
|
local handler = client.handlers[method] or vim.lsp.handlers[method]
|
2022-04-30 08:23:50 -07:00
|
|
|
handler(...)
|
|
|
|
do_format(next(clients, idx))
|
|
|
|
end, bufnr)
|
|
|
|
end
|
|
|
|
do_format(next(clients))
|
|
|
|
else
|
|
|
|
local timeout_ms = options.timeout_ms or 1000
|
|
|
|
for _, client in pairs(clients) do
|
2022-09-08 02:33:04 -07:00
|
|
|
local params = set_range(client, util.make_formatting_params(options.formatting_options))
|
|
|
|
local result, err = client.request_sync(method, params, timeout_ms, bufnr)
|
2022-04-30 08:23:50 -07:00
|
|
|
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
|
2022-04-30 06:36:40 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Renames all references to the symbol under the cursor.
|
|
|
|
---
|
2022-05-05 14:56:00 -07:00
|
|
|
---@param new_name string|nil If not provided, the user will be prompted for a new
|
|
|
|
--- name using |vim.ui.input()|.
|
|
|
|
---@param options table|nil additional options
|
|
|
|
--- - filter (function|nil):
|
2022-05-26 03:28:50 -07:00
|
|
|
--- Predicate used to filter clients. Receives a client as argument and
|
|
|
|
--- must return a boolean. Clients matching the predicate are included.
|
2022-05-05 14:56:00 -07:00
|
|
|
--- - name (string|nil):
|
|
|
|
--- Restrict clients used for rename to ones where client.name matches
|
|
|
|
--- this field.
|
|
|
|
function M.rename(new_name, options)
|
|
|
|
options = options or {}
|
2022-07-15 09:26:47 -07:00
|
|
|
local bufnr = options.bufnr or api.nvim_get_current_buf()
|
2023-07-17 09:27:16 -07:00
|
|
|
local clients = vim.lsp.get_clients({
|
2022-05-26 03:28:50 -07:00
|
|
|
bufnr = bufnr,
|
|
|
|
name = options.name,
|
2023-07-12 05:48:21 -07:00
|
|
|
-- Clients must at least support rename, prepareRename is optional
|
2023-08-03 04:03:48 -07:00
|
|
|
method = ms.textDocument_rename,
|
2022-05-26 03:28:50 -07:00
|
|
|
})
|
2022-05-05 14:56:00 -07:00
|
|
|
if options.filter then
|
2022-05-26 03:28:50 -07:00
|
|
|
clients = vim.tbl_filter(options.filter, clients)
|
2021-11-07 08:13:53 -07:00
|
|
|
end
|
|
|
|
|
2022-05-05 14:56:00 -07:00
|
|
|
if #clients == 0 then
|
2022-05-06 09:57:08 -07:00
|
|
|
vim.notify('[LSP] Rename, no matching language servers with rename capability.')
|
2022-05-05 14:56:00 -07:00
|
|
|
end
|
|
|
|
|
2022-07-15 09:26:47 -07:00
|
|
|
local win = api.nvim_get_current_win()
|
2022-05-05 14:56:00 -07:00
|
|
|
|
|
|
|
-- Compute early to account for cursor movements after going async
|
2022-07-09 16:57:35 -07:00
|
|
|
local cword = vim.fn.expand('<cword>')
|
2022-05-05 14:56:00 -07:00
|
|
|
|
2022-06-01 09:56:18 -07:00
|
|
|
local function get_text_at_range(range, offset_encoding)
|
2022-07-15 09:26:47 -07:00
|
|
|
return api.nvim_buf_get_text(
|
2022-05-05 14:56:00 -07:00
|
|
|
bufnr,
|
|
|
|
range.start.line,
|
2022-06-01 09:56:18 -07:00
|
|
|
util._get_line_byte_from_position(bufnr, range.start, offset_encoding),
|
2022-05-05 14:56:00 -07:00
|
|
|
range['end'].line,
|
2022-06-01 09:56:18 -07:00
|
|
|
util._get_line_byte_from_position(bufnr, range['end'], offset_encoding),
|
2022-05-05 14:56:00 -07:00
|
|
|
{}
|
|
|
|
)[1]
|
|
|
|
end
|
|
|
|
|
|
|
|
local try_use_client
|
|
|
|
try_use_client = function(idx, client)
|
|
|
|
if not client then
|
2021-09-08 08:00:15 -07:00
|
|
|
return
|
|
|
|
end
|
2022-05-05 14:56:00 -07:00
|
|
|
|
|
|
|
local function rename(name)
|
|
|
|
local params = util.make_position_params(win, client.offset_encoding)
|
|
|
|
params.newName = name
|
2023-08-03 04:03:48 -07:00
|
|
|
local handler = client.handlers[ms.textDocument_rename]
|
|
|
|
or vim.lsp.handlers[ms.textDocument_rename]
|
|
|
|
client.request(ms.textDocument_rename, params, function(...)
|
2022-05-05 14:56:00 -07:00
|
|
|
handler(...)
|
|
|
|
try_use_client(next(clients, idx))
|
|
|
|
end, bufnr)
|
|
|
|
end
|
|
|
|
|
2023-08-03 04:03:48 -07:00
|
|
|
if client.supports_method(ms.textDocument_prepareRename) then
|
2022-05-05 14:56:00 -07:00
|
|
|
local params = util.make_position_params(win, client.offset_encoding)
|
2023-08-03 04:03:48 -07:00
|
|
|
client.request(ms.textDocument_prepareRename, params, function(err, result)
|
2022-05-05 14:56:00 -07:00
|
|
|
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: ',
|
|
|
|
}
|
|
|
|
-- result: Range | { range: Range, placeholder: string }
|
|
|
|
if result.placeholder then
|
|
|
|
prompt_opts.default = result.placeholder
|
|
|
|
elseif result.start then
|
2022-06-01 09:56:18 -07:00
|
|
|
prompt_opts.default = get_text_at_range(result, client.offset_encoding)
|
2022-05-05 14:56:00 -07:00
|
|
|
elseif result.range then
|
2022-06-01 09:56:18 -07:00
|
|
|
prompt_opts.default = get_text_at_range(result.range, client.offset_encoding)
|
2022-05-05 14:56:00 -07:00
|
|
|
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)
|
2022-05-06 09:57:08 -07:00
|
|
|
else
|
|
|
|
assert(
|
2023-08-03 04:03:48 -07:00
|
|
|
client.supports_method(ms.textDocument_rename),
|
2022-05-06 09:57:08 -07:00
|
|
|
'Client must support textDocument/rename'
|
|
|
|
)
|
2022-05-05 14:56:00 -07:00
|
|
|
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)
|
2021-09-08 08:00:15 -07:00
|
|
|
end
|
|
|
|
end
|
2022-05-05 14:56:00 -07:00
|
|
|
|
|
|
|
try_use_client(next(clients))
|
2019-11-20 17:03:32 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Lists all the references to the symbol under the cursor in the quickfix window.
|
|
|
|
---
|
2023-01-04 04:48:41 -07:00
|
|
|
---@param context (table|nil) Context for the request
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
---@param options table|nil additional options
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
function M.references(context, options)
|
2019-11-24 04:01:18 -07:00
|
|
|
validate({ context = { context, 't', true } })
|
|
|
|
local params = util.make_position_params()
|
|
|
|
params.context = context or {
|
|
|
|
includeDeclaration = true,
|
|
|
|
}
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.textDocument_references, params, options)
|
2019-11-24 04:01:18 -07:00
|
|
|
end
|
|
|
|
|
2020-07-19 14:16:12 -07:00
|
|
|
--- Lists all symbols in the current buffer in the quickfix window.
|
|
|
|
---
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
---@param options table|nil additional options
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
function M.document_symbol(options)
|
2020-02-22 05:14:10 -07:00
|
|
|
local params = { textDocument = util.make_text_document_params() }
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.textDocument_documentSymbol, params, options)
|
2020-02-22 05:14:10 -07:00
|
|
|
end
|
|
|
|
|
2020-07-18 12:10:09 -07:00
|
|
|
local function pick_call_hierarchy_item(call_hierarchy_items)
|
|
|
|
if not call_hierarchy_items then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if #call_hierarchy_items == 1 then
|
|
|
|
return call_hierarchy_items[1]
|
|
|
|
end
|
|
|
|
local items = {}
|
2021-07-11 11:34:26 -07:00
|
|
|
for i, item in pairs(call_hierarchy_items) do
|
2020-07-18 12:10:09 -07:00
|
|
|
local entry = item.detail or item.name
|
|
|
|
table.insert(items, string.format('%d. %s', i, entry))
|
|
|
|
end
|
|
|
|
local choice = vim.fn.inputlist(items)
|
|
|
|
if choice < 1 or choice > #items then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return choice
|
|
|
|
end
|
|
|
|
|
2021-05-18 14:32:40 -07:00
|
|
|
local function call_hierarchy(method)
|
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request(ms.textDocument_prepareCallHierarchy, params, function(err, result, ctx)
|
2021-05-18 14:32:40 -07:00
|
|
|
if err then
|
|
|
|
vim.notify(err.message, vim.log.levels.WARN)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local call_hierarchy_item = pick_call_hierarchy_item(result)
|
2021-09-21 15:05:49 -07:00
|
|
|
local client = vim.lsp.get_client_by_id(ctx.client_id)
|
|
|
|
if client then
|
|
|
|
client.request(method, { item = call_hierarchy_item }, nil, ctx.bufnr)
|
|
|
|
else
|
|
|
|
vim.notify(
|
|
|
|
string.format('Client with id=%d disappeared during call hierarchy request', ctx.client_id),
|
|
|
|
vim.log.levels.WARN
|
|
|
|
)
|
|
|
|
end
|
2021-05-18 14:32:40 -07:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Lists all the call sites of the symbol under the cursor in the
|
|
|
|
--- |quickfix| window. If the symbol can resolve to multiple
|
2022-09-25 16:58:27 -07:00
|
|
|
--- items, the user can pick one in the |inputlist()|.
|
2020-07-18 12:10:09 -07:00
|
|
|
function M.incoming_calls()
|
2023-08-05 02:03:57 -07:00
|
|
|
call_hierarchy(ms.callHierarchy_incomingCalls)
|
2020-07-18 12:10:09 -07:00
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Lists all the items that are called by the symbol under the
|
|
|
|
--- cursor in the |quickfix| window. If the symbol can resolve to
|
2022-09-25 16:58:27 -07:00
|
|
|
--- multiple items, the user can pick one in the |inputlist()|.
|
2020-07-18 12:10:09 -07:00
|
|
|
function M.outgoing_calls()
|
2023-08-05 02:03:57 -07:00
|
|
|
call_hierarchy(ms.callHierarchy_outgoingCalls)
|
2020-07-18 12:10:09 -07:00
|
|
|
end
|
2020-07-02 04:09:17 -07:00
|
|
|
|
2020-11-25 13:07:02 -07:00
|
|
|
--- List workspace folders.
|
2020-12-08 19:09:33 -07:00
|
|
|
---
|
2020-11-25 13:07:02 -07:00
|
|
|
function M.list_workspace_folders()
|
|
|
|
local workspace_folders = {}
|
2023-07-17 09:27:16 -07:00
|
|
|
for _, client in pairs(vim.lsp.get_clients({ bufnr = 0 })) do
|
2021-11-22 07:52:24 -07:00
|
|
|
for _, folder in pairs(client.workspace_folders or {}) do
|
2020-11-25 13:07:02 -07:00
|
|
|
table.insert(workspace_folders, folder.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return workspace_folders
|
|
|
|
end
|
|
|
|
|
2020-12-08 19:09:33 -07:00
|
|
|
--- Add the folder at path to the workspace folders. If {path} is
|
|
|
|
--- not provided, the user will be prompted for a path using |input()|.
|
2020-11-25 13:07:02 -07:00
|
|
|
function M.add_workspace_folder(workspace_folder)
|
2021-06-25 05:23:53 -07:00
|
|
|
workspace_folder = workspace_folder
|
2022-07-09 16:57:35 -07:00
|
|
|
or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'), 'dir')
|
2022-07-15 09:26:47 -07:00
|
|
|
api.nvim_command('redraw')
|
2020-11-25 13:07:02 -07:00
|
|
|
if not (workspace_folder and #workspace_folder > 0) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if vim.fn.isdirectory(workspace_folder) == 0 then
|
|
|
|
print(workspace_folder, ' is not a valid directory')
|
|
|
|
return
|
|
|
|
end
|
2023-07-25 07:57:19 -07:00
|
|
|
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()
|
|
|
|
for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do
|
2020-11-25 13:07:02 -07:00
|
|
|
local found = false
|
2021-11-22 07:52:24 -07:00
|
|
|
for _, folder in pairs(client.workspace_folders or {}) do
|
2020-11-25 13:07:02 -07:00
|
|
|
if folder.name == workspace_folder then
|
|
|
|
found = true
|
|
|
|
print(workspace_folder, 'is already part of this workspace')
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not found then
|
2023-08-03 04:03:48 -07:00
|
|
|
client.notify(ms.workspace_didChangeWorkspaceFolders, params)
|
2021-11-22 07:52:24 -07:00
|
|
|
if not client.workspace_folders then
|
|
|
|
client.workspace_folders = {}
|
2021-11-11 02:15:59 -07:00
|
|
|
end
|
2023-07-25 07:57:19 -07:00
|
|
|
table.insert(client.workspace_folders, new_workspace)
|
2020-11-25 13:07:02 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-08 19:09:33 -07:00
|
|
|
--- Remove the folder at path from the workspace folders. If
|
|
|
|
--- {path} is not provided, the user will be prompted for
|
|
|
|
--- a path using |input()|.
|
2020-11-25 13:07:02 -07:00
|
|
|
function M.remove_workspace_folder(workspace_folder)
|
|
|
|
workspace_folder = workspace_folder
|
2022-07-09 16:57:35 -07:00
|
|
|
or npcall(vim.fn.input, 'Workspace Folder: ', vim.fn.expand('%:p:h'))
|
2022-07-15 09:26:47 -07:00
|
|
|
api.nvim_command('redraw')
|
2020-11-25 13:07:02 -07:00
|
|
|
if not (workspace_folder and #workspace_folder > 0) then
|
|
|
|
return
|
|
|
|
end
|
2023-07-25 07:57:19 -07:00
|
|
|
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()
|
|
|
|
for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do
|
|
|
|
for idx, folder in pairs(client.workspace_folders) do
|
2020-11-25 13:07:02 -07:00
|
|
|
if folder.name == workspace_folder then
|
2023-08-03 04:03:48 -07:00
|
|
|
client.notify(ms.workspace_didChangeWorkspaceFolders, params)
|
2021-11-22 07:52:24 -07:00
|
|
|
client.workspace_folders[idx] = nil
|
2020-11-25 13:07:02 -07:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print(workspace_folder, 'is not currently part of the workspace')
|
|
|
|
end
|
|
|
|
|
2020-07-02 04:09:17 -07:00
|
|
|
--- Lists all symbols in the current workspace in the quickfix window.
|
|
|
|
---
|
2020-08-19 09:17:08 -07:00
|
|
|
--- The list is filtered against {query}; if the argument is omitted from the
|
|
|
|
--- call, the user is prompted to enter a string on the command line. An empty
|
|
|
|
--- string means no filtering is done.
|
|
|
|
---
|
2023-07-10 04:38:15 -07:00
|
|
|
---@param query string|nil optional
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
---@param options table|nil additional options
|
2022-08-17 03:39:38 -07:00
|
|
|
--- - on_list: (function) handler for list results. See |lsp-on-list-handler|
|
feat(lsp): allow passing custom list handler to LSP functions that return lists (#19213)
Currently LSP allows only using loclist or quickfix list window. I
normally prefer to review all quickfix items without opening quickfix
window. This fix allows passing `on_list` option which allows full
control what to do with list.
Here is example how to use it with quick fix list:
```lua
local function on_list(options)
vim.fn.setqflist({}, ' ', options)
vim.api.nvim_command('cfirst')
end
local bufopts = { noremap=true, silent=true, buffer=bufnr }
vim.keymap.set('n', '<leader>ad', function() vim.lsp.buf.declaration{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>d', function() vim.lsp.buf.definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>ai', function() vim.lsp.buf.implementation{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>at', function() vim.lsp.buf.type_definition{on_list=on_list} end, bufopts)
vim.keymap.set('n', '<leader>af', function() vim.lsp.buf.references(nil, {on_list=on_list}) end, bufopts)
```
If you prefer loclist do something like this:
```lua
local function on_list(options)
vim.fn.setloclist(0, {}, ' ', options)
vim.api.nvim_command('lopen')
end
```
close #19182
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
2022-07-25 14:02:51 -07:00
|
|
|
function M.workspace_symbol(query, options)
|
2022-07-09 16:57:35 -07:00
|
|
|
query = query or npcall(vim.fn.input, 'Query: ')
|
2022-01-16 00:08:35 -07:00
|
|
|
if query == nil then
|
|
|
|
return
|
|
|
|
end
|
2020-05-02 08:56:05 -07:00
|
|
|
local params = { query = query }
|
2023-08-03 04:03:48 -07:00
|
|
|
request_with_options(ms.workspace_symbol, params, options)
|
2020-05-02 08:56:05 -07:00
|
|
|
end
|
|
|
|
|
2021-02-24 09:23:47 -07:00
|
|
|
--- Send request to the server to resolve document highlights for the current
|
|
|
|
--- text document position. This request can be triggered by a key mapping or
|
|
|
|
--- by events such as `CursorHold`, e.g.:
|
2022-11-23 04:31:49 -07:00
|
|
|
--- <pre>vim
|
|
|
|
--- autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
|
|
|
|
--- autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()
|
|
|
|
--- autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
|
2020-02-26 12:10:16 -07:00
|
|
|
--- </pre>
|
2021-02-24 09:23:47 -07:00
|
|
|
---
|
|
|
|
--- Note: Usage of |vim.lsp.buf.document_highlight()| requires the following highlight groups
|
|
|
|
--- to be defined or you won't be able to see the actual highlights.
|
2022-09-25 16:58:27 -07:00
|
|
|
--- |hl-LspReferenceText|
|
|
|
|
--- |hl-LspReferenceRead|
|
|
|
|
--- |hl-LspReferenceWrite|
|
2020-02-26 12:10:16 -07:00
|
|
|
function M.document_highlight()
|
|
|
|
local params = util.make_position_params()
|
2023-08-03 04:03:48 -07:00
|
|
|
request(ms.textDocument_documentHighlight, params)
|
2020-02-26 12:10:16 -07:00
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Removes document highlights from current buffer.
|
2023-07-04 05:30:31 -07:00
|
|
|
function M.clear_references()
|
|
|
|
util.buf_clear_references()
|
2020-02-26 12:10:16 -07:00
|
|
|
end
|
|
|
|
|
2021-09-28 14:04:01 -07:00
|
|
|
--- This is not public because the main extension point is
|
|
|
|
--- vim.ui.select which can be overridden independently.
|
|
|
|
---
|
|
|
|
--- Can't call/use vim.lsp.handlers['textDocument/codeAction'] because it expects
|
|
|
|
--- `(err, CodeAction[] | Command[], ctx)`, but we want to aggregate the results
|
|
|
|
--- from multiple clients to have 1 single UI prompt for the user, yet we still
|
|
|
|
--- need to be able to link a `CodeAction|Command` to the right client for
|
|
|
|
--- `codeAction/resolve`
|
2022-04-30 01:14:31 -07:00
|
|
|
local function on_code_action_results(results, ctx, options)
|
2021-09-28 14:04:01 -07:00
|
|
|
local action_tuples = {}
|
2022-05-12 09:48:02 -07:00
|
|
|
|
|
|
|
local function action_filter(a)
|
|
|
|
-- filter by specified action kind
|
|
|
|
if options and options.context and options.context.only then
|
|
|
|
if not a.kind then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local found = false
|
|
|
|
for _, o in ipairs(options.context.only) do
|
2023-06-16 23:01:31 -07:00
|
|
|
-- action kinds are hierarchical with . as a separator: when requesting only 'type-annotate'
|
|
|
|
-- this filter allows both 'type-annotate' and 'type-annotate.foo', for example
|
|
|
|
if a.kind == o or vim.startswith(a.kind, o .. '.') then
|
2022-05-12 09:48:02 -07:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not found then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- filter by user function
|
|
|
|
if options and options.filter and not options.filter(a) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
-- no filter removed this action
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-09-28 14:04:01 -07:00
|
|
|
for client_id, result in pairs(results) do
|
|
|
|
for _, action in pairs(result.result or {}) do
|
2022-05-12 09:48:02 -07:00
|
|
|
if action_filter(action) then
|
2022-04-30 01:14:31 -07:00
|
|
|
table.insert(action_tuples, { client_id, action })
|
|
|
|
end
|
2021-09-28 14:04:01 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if #action_tuples == 0 then
|
|
|
|
vim.notify('No code actions available', vim.log.levels.INFO)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local function apply_action(action, client)
|
|
|
|
if action.edit then
|
2022-01-13 03:34:04 -07:00
|
|
|
util.apply_workspace_edit(action.edit, client.offset_encoding)
|
2021-09-28 14:04:01 -07:00
|
|
|
end
|
|
|
|
if action.command then
|
|
|
|
local command = type(action.command) == 'table' and action.command or action
|
2023-06-20 09:36:18 -07:00
|
|
|
client._exec_cmd(command, ctx)
|
2021-09-28 14:04:01 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function on_user_choice(action_tuple)
|
|
|
|
if not action_tuple then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
-- textDocument/codeAction can return either Command[] or CodeAction[]
|
|
|
|
--
|
|
|
|
-- CodeAction
|
|
|
|
-- ...
|
|
|
|
-- edit?: WorkspaceEdit -- <- must be applied before command
|
|
|
|
-- command?: Command
|
|
|
|
--
|
|
|
|
-- Command:
|
|
|
|
-- title: string
|
|
|
|
-- command: string
|
|
|
|
-- arguments?: any[]
|
|
|
|
--
|
2023-05-30 10:15:07 -07:00
|
|
|
---@type lsp.Client
|
2021-09-28 14:04:01 -07:00
|
|
|
local client = vim.lsp.get_client_by_id(action_tuple[1])
|
|
|
|
local action = action_tuple[2]
|
2023-05-30 10:15:07 -07:00
|
|
|
|
2023-08-03 04:03:48 -07:00
|
|
|
local reg = client.dynamic_capabilities:get(ms.textDocument_codeAction, { bufnr = ctx.bufnr })
|
2023-05-30 10:15:07 -07:00
|
|
|
|
|
|
|
local supports_resolve = vim.tbl_get(reg or {}, 'registerOptions', 'resolveProvider')
|
2023-08-05 02:03:57 -07:00
|
|
|
or client.supports_method(ms.codeAction_resolve)
|
2023-05-30 10:15:07 -07:00
|
|
|
|
|
|
|
if not action.edit and client and supports_resolve then
|
2023-08-05 02:03:57 -07:00
|
|
|
client.request(ms.codeAction_resolve, action, function(err, resolved_action)
|
2021-09-28 14:04:01 -07:00
|
|
|
if err then
|
|
|
|
vim.notify(err.code .. ': ' .. err.message, vim.log.levels.ERROR)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
apply_action(resolved_action, client)
|
2023-06-20 09:36:18 -07:00
|
|
|
end, ctx.bufnr)
|
2021-09-28 14:04:01 -07:00
|
|
|
else
|
|
|
|
apply_action(action, client)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-30 01:14:31 -07:00
|
|
|
-- If options.apply is given, and there are just one remaining code action,
|
|
|
|
-- apply it directly without querying the user.
|
|
|
|
if options and options.apply and #action_tuples == 1 then
|
|
|
|
on_user_choice(action_tuples[1])
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-09-28 14:04:01 -07:00
|
|
|
vim.ui.select(action_tuples, {
|
|
|
|
prompt = 'Code actions:',
|
2021-10-31 17:15:09 -07:00
|
|
|
kind = 'codeaction',
|
2021-09-28 14:04:01 -07:00
|
|
|
format_item = function(action_tuple)
|
|
|
|
local title = action_tuple[2].title:gsub('\r\n', '\\r\\n')
|
|
|
|
return title:gsub('\n', '\\n')
|
|
|
|
end,
|
|
|
|
}, on_user_choice)
|
|
|
|
end
|
|
|
|
|
2021-07-18 01:58:35 -07:00
|
|
|
--- Requests code actions from all clients and calls the handler exactly once
|
|
|
|
--- with all aggregated results
|
2022-04-30 01:14:31 -07:00
|
|
|
local function code_action_request(params, options)
|
2022-07-15 09:26:47 -07:00
|
|
|
local bufnr = api.nvim_get_current_buf()
|
2023-08-03 04:03:48 -07:00
|
|
|
vim.lsp.buf_request_all(bufnr, ms.textDocument_codeAction, params, function(results)
|
|
|
|
local ctx = { bufnr = bufnr, method = ms.textDocument_codeAction, params = params }
|
2022-04-30 01:14:31 -07:00
|
|
|
on_code_action_results(results, ctx, options)
|
2021-07-18 01:58:35 -07:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-09-26 14:40:28 -07:00
|
|
|
--- Selects a code action available at the current
|
2020-08-19 09:17:08 -07:00
|
|
|
--- cursor position.
|
2021-08-22 13:55:28 -07:00
|
|
|
---
|
2022-04-30 01:14:31 -07:00
|
|
|
---@param options table|nil Optional table which holds the following optional fields:
|
2022-07-28 10:19:07 -07:00
|
|
|
--- - context: (table|nil)
|
|
|
|
--- Corresponds to `CodeActionContext` of the LSP specification:
|
|
|
|
--- - diagnostics (table|nil):
|
|
|
|
--- LSP `Diagnostic[]`. Inferred from the current
|
|
|
|
--- position if not provided.
|
|
|
|
--- - only (table|nil):
|
|
|
|
--- List of LSP `CodeActionKind`s used to filter the code actions.
|
|
|
|
--- Most language servers support values like `refactor`
|
|
|
|
--- or `quickfix`.
|
2023-01-21 00:22:34 -07:00
|
|
|
--- - triggerKind (number|nil): The reason why code actions were requested.
|
2022-07-28 10:19:07 -07:00
|
|
|
--- - filter: (function|nil)
|
|
|
|
--- Predicate taking an `CodeAction` and returning a boolean.
|
|
|
|
--- - apply: (boolean|nil)
|
|
|
|
--- When set to `true`, and there is just one remaining action
|
|
|
|
--- (after filtering), the action is applied without user query.
|
|
|
|
---
|
|
|
|
--- - range: (table|nil)
|
|
|
|
--- Range for which code actions should be requested.
|
|
|
|
--- If in visual mode this defaults to the active selection.
|
2023-06-24 04:47:10 -07:00
|
|
|
--- Table must contain `start` and `end` keys with {row,col} tuples
|
2022-07-28 10:19:07 -07:00
|
|
|
--- using mark-like indexing. See |api-indexing|
|
|
|
|
---
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_codeAction
|
2023-08-07 06:35:06 -07:00
|
|
|
---@see vim.lsp.protocol.CodeActionTriggerKind
|
2022-04-30 01:14:31 -07:00
|
|
|
function M.code_action(options)
|
|
|
|
validate({ options = { options, 't', true } })
|
|
|
|
options = options or {}
|
|
|
|
-- Detect old API call code_action(context) which should now be
|
|
|
|
-- code_action({ context = context} )
|
|
|
|
if options.diagnostics or options.only then
|
|
|
|
options = { options = options }
|
|
|
|
end
|
|
|
|
local context = options.context or {}
|
2023-01-21 00:22:34 -07:00
|
|
|
if not context.triggerKind then
|
|
|
|
context.triggerKind = vim.lsp.protocol.CodeActionTriggerKind.Invoked
|
|
|
|
end
|
2021-09-26 14:40:28 -07:00
|
|
|
if not context.diagnostics then
|
2022-07-15 09:26:47 -07:00
|
|
|
local bufnr = api.nvim_get_current_buf()
|
2022-05-20 04:11:23 -07:00
|
|
|
context.diagnostics = vim.lsp.diagnostic.get_line_diagnostics(bufnr)
|
2021-09-26 14:40:28 -07:00
|
|
|
end
|
2022-07-28 10:19:07 -07:00
|
|
|
local params
|
|
|
|
local mode = api.nvim_get_mode().mode
|
|
|
|
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')
|
|
|
|
params = util.make_given_range_params(start, end_)
|
|
|
|
elseif mode == 'v' or mode == 'V' then
|
2023-03-12 01:45:28 -07:00
|
|
|
local range = range_from_selection(0, mode)
|
2022-09-08 02:33:04 -07:00
|
|
|
params = util.make_given_range_params(range.start, range['end'])
|
2022-07-28 10:19:07 -07:00
|
|
|
else
|
|
|
|
params = util.make_range_params()
|
|
|
|
end
|
2020-05-15 16:18:59 -07:00
|
|
|
params.context = context
|
2022-04-30 01:14:31 -07:00
|
|
|
code_action_request(params, options)
|
2020-05-15 16:18:59 -07:00
|
|
|
end
|
|
|
|
|
2020-08-19 09:17:08 -07:00
|
|
|
--- Executes an LSP server command.
|
|
|
|
---
|
2022-01-08 11:15:41 -07:00
|
|
|
---@param command_params table A valid `ExecuteCommandParams` object
|
2021-08-22 13:55:28 -07:00
|
|
|
---@see https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand
|
2022-01-08 11:15:41 -07:00
|
|
|
function M.execute_command(command_params)
|
2020-05-15 16:18:59 -07:00
|
|
|
validate({
|
2022-01-08 11:15:41 -07:00
|
|
|
command = { command_params.command, 's' },
|
|
|
|
arguments = { command_params.arguments, 't', true },
|
2020-05-15 16:18:59 -07:00
|
|
|
})
|
2022-01-08 11:15:41 -07:00
|
|
|
command_params = {
|
|
|
|
command = command_params.command,
|
|
|
|
arguments = command_params.arguments,
|
|
|
|
workDoneToken = command_params.workDoneToken,
|
|
|
|
}
|
2023-08-03 04:03:48 -07:00
|
|
|
request(ms.workspace_executeCommand, command_params)
|
2020-05-15 16:18:59 -07:00
|
|
|
end
|
|
|
|
|
2019-11-20 15:21:57 -07:00
|
|
|
return M
|